Capsicum Capsicum - 1 year ago 107
R Question

R: two axes and grid lines

I would like to plot two graphs in the same plotting region with horizontal grid lines. Each side of the grid lines should give the value for one graph or the other. There should be no y-axis.


function allows me to simply set the number of bins using the
argument. How do I get the corresponding labels to the grid lines? Usually, I would use
axis(..., lwd=0)
to get the labels. However, the function requires label positions with
and does not feature a
argument. Is there a way to automatically set the locations from the number of bins?

Based on Miff's hint below, this should solve the problem.

plot(1:10, axes=FALSE, ylim=c(0,10), ylab="")
par(yaxp=c(0, 10, 5))
axis(2, lwd=0, col.axis="gray")
plot(60:50, axes=FALSE, ylim=c(50,60), ylab="")
par(yaxp=c(50, 60, 5))
axis(4, lwd=0, col.axis="gray")
grid(NA, NULL)

Answer Source

grid() gets its locations for gridlines from axTicks(), which in turn uses numbers from par("yaxp"). If you modify this parameter (rather than explicitly passing it to grid), the result will then apply to both the grid drawn and the axis. For example:

plot(1:10, axes=FALSE)
axis(2) #Default 4 sections between ticks
par(yaxp=c(par("yaxp")[1:2], 7)) #Lets have seven instead
grid() #Grid now matches with right rather than left

Obviously similar works for the x axis.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download