user3639557
user3639557

Reputation: 5291

Grid doesn't match the axis

I am using the following couple of lines to produce the below plot from rows of two 4X10 Matrix d1, and d2 in one graph:

plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="red", 
ann=FALSE, pch=17, log = 'y',lty=4, axes=FALSE, las=2) +
lines(as.matrix(d1[1,]),as.matrix(d2[1,]), type="o", col="blue",
ann=FALSE, pch=15, lty=4)

x_axis_labels <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 
axis(1,labels = x_axis_labels, at = x_axis_labels)
y_axis_labels <- c(3e+4, 6e+4,2e+5,3e+5, 6e+5, 2e+6,5e+6) 
axis(2,labels = y_axis_labels, at = y_axis_labels, las=2)

grid()

Which produces the following: enter image description here

But what I like to have is to have the grid to start from all the labels on each axis. At the moment it only starts from some of the labels on the x-axis, and is not aligned with any of the y-axis labels.

Upvotes: 1

Views: 336

Answers (1)

jbaums
jbaums

Reputation: 27388

This will probably be easier to control with abline:

abline(v=x_axis_labels, h=y_axis_labels, lty=2, col='lightgray')

If you want the gridlines behind points etc., then try the panel.first argument to plot:

plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="red", 
     ann=FALSE, pch=17, log = 'y',lty=4, axes=FALSE, las=2,
     panel.first=abline(v=x_axis_labels, h=y_axis_labels, lty=2, col='lightgray'))

Upvotes: 2

Related Questions