Adding arrows to plotrix gap.plot() with y-axis breaks - r

I'm trying to add confidence intervals to a gap.plot made with the plotrix package when there is a y-axis break. Here is a MWE:
twogrp=c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22)
gap.plot(twogrp,gap=c(8,16),
xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
main="Test two gap plot with the lot",xtics=seq(0,25,by=5),
ytics=c(4,6,18,20,22,24),
lty=c(rep(1,10),rep(2,10)),
pch=c(rep(2,10),rep(3,10)),
col=c(rep(2,10),rep(3,10)),
type="b")
arrows(2,18,2,24,length=0.05,angle=90,code=3)
This doesn't add the arrow, presumably because the upper plotting region has been closed.
This works though:
gap.plot(twogrp,gap=c(8,16),
xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,25),
main="Test two gap plot with the lot",xtics=seq(0,25,by=5),
ytics=c(4,6,18,20,22,24),
lty=c(rep(1,10),rep(2,10)),
pch=c(rep(2,10),rep(3,10)),
col=c(rep(2,10),rep(3,10)),
type="b")
arrows(2,4,2,6,length=0.05,angle=90,code=3)
Anybody know of a work around that doesn't involve using another package. I know how to do this in ggplot2, I just prefer to use plotrix.

You need to subtract the gap value from y value when you put arrows in the upper plot area.
arrows(2,18,2,24,length=0.05,angle=90,code=3)
should be as follows.
arrows(2,18 - 8,2,24 - 8,length=0.05,angle=90,code=3)

Related

R ggplot2: Draw diagonal lines on log-scale

I had a graph created with default R-plot functionality but now want to change to ggplot2 mainly because I want to use ggrepel to place labels correctly and non-overlapping.
My old plot contains diagonal lines which I need to keep. They are ploted like this:
for (i in -5:10) {
abline(a= i, b= 1, lty = 5)
}
The issues I have now are:
How do I do this for-loop with ggplot2 so I don't need to add all the lines expliclty?
How do I actually created the lines correctly?
geom_abline(slope=1, intercept=10)
Does not work as expected, probably due to log10 scale. So how can I draw diagonal lines on log10 scales correctly?
It actually works fine. This issue is directly related to my other issue about x and y axis limits. Per default the plot draws a bigger area than the x and y limits define (who thought this was a good idea???). And therefore the intercepts look wrong but the actually are ok.
If I set expand = c(0, 0) for both axis, then the intercept is also looks fine because that only draws to the limits.
The solution for multiple lines is a intercept list:
geom_abline(slope=1, intercept=(-3):(5)

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

Y-Axis positions of barplot and base plot do not match

I was trying to plot a climate diagram and ran into the following problem:
After using barplot(...) for precipitation I superimposed another plot for the temperature. It is necessary for climate diagrams that the two y-axes (mm, °C) align at zero and that the precipitation/temperature ratio is 2:1 (e.g. 20mm precipitation corresponds to 10°C).
The problem: barplot(...) draws the axis to the plot's box while plot(...) leaves some space between the box and the axis margins.
Here is a simplified example. From the grid lines you see that the 0-values do not align:
barplot(0:10)
grid(col=1)
par(new=TRUE)
plot(0:10, xlim=c(-2,14), axes=FALSE)
axis(4,at=c(0:10), labels=c(0:10))
How can I get the right position and scaling of the two axes?
Don't use par(new = TRUE):
barplot(0:10)
grid(col=1)
lines(0:10, type = "p")
axis(4,at = c(0:10), labels = seq(0,20,2))
The function lines() is the right one here. The argument type = p is needed to plot points.
You need to adjust the y-values for the temperature, but now the second y-axis is in the right way, I think.

Two y-axis plot without overlapping

I am trying to make a graphic with to y-axis that the values of each y-axis start at the middle of the box so the 2 lines that I am plotting do not overlap. I have search for some time but have not found any way to do that with the basic X-Y graphics from R and i am adding some other things to the graphic so i don't want to do it with other package.
With the graphic in mind what I need it that the top one "merges" with the bottom and that the values on the left y-axis of the top graph change to the right but start at the middle, that way the lines wont overlap.
Do you mean something like this?
d1 <- data.frame(x=1:10, y=sin(1:10))
d2 <- data.frame(x=1:10, y=sin(1:10)+10)
plot(rbind(d1,d2), t="n", yaxt="n")
lines(d1)
lines(d2)
axis(2, at=pretty(d1$y))
axis(4, at=pretty(d2$y))

R: multiple x axis with annotations

Is it possible to add more than one x-axis to a plot in R? And to put an annotation next to each scale?
Edit > here's the result of Nick Sabbe idea. For the annotation (a little text at the left of each axis), is it possible ?
You can use the line argument of axis() to place an axis higher or lower, this way you can make multiple axes. With mtext() you can then add a label to the side. Do note that the plot itself is only on one scale so you need to rescale the points and labels of the other scale accordingly:
# Plot and first axis:
plot(1:10,1:10,bty="n",col="red",pch=16,axes=FALSE,xlab="",ylab="")
axis(2,0:11,las=1)
axis(1,0:11,line=1,col="red",col.ticks="red",col.axis="red")
mtext("Label 1",1,line=1,at=0.2,col="red")
# Secondary points and axis:
points(rnorm(10,50,20)/10, rnorm(10,5,2),pch=16, col="blue" )
axis(1,0:11,labels=0:11*10,line=3,col="blue",col.ticks="blue",col.axis="blue")
mtext("Label 2",1,line=3,at=0.2,col="blue")
You can use ?axis for that. Parameter at is in the scale of the original axis of the plot, and you can pass labels to show other values.
You have to scale the axess labels yourself, though.
A very simple/silly example:
plot(1:10,1:10)
axis(side=4, at=c(3,7), labels=c(30,70))
Finally, note that most people consider adding multiple axes to a plot bad form...

Resources