Grid line consistent with ticks on axis

2019-01-21 23:24发布

I am embarrassed to ask this simple question, but has been in kicking my mind for several days whenever I create a plot:

plot (x = 1:10, y = rnorm (10, 5, 2))
grid (10,10, lty = 6, col = "cornsilk2")

I want to position the grids right at where axis are labelled, i.e. at 2, 4, 6, 8, 10 in x axis and similarly 3, 4, 5, 6, 7, 8 in y axis.

enter image description here

I want to automate the process as whenever the plot size changes the default label behaviour changes. See the following plot:

enter image description here

4条回答
小情绪 Triste *
2楼-- · 2019-01-21 23:26

For reference, there is a way to control the grid and axes parameters directly from the plot() command, if we are not defining a custom tick interval:

plot(x = 1:10, y = rnorm(10, 5, 2), xlim=c(1, 10), ylim=c(1, 10), panel.first=grid())

The plot.default() documentation gives more information about these parameters.

When using a custom ticks interval, the easiest is to draw the grid using abline:

plot(x = 1:10, y = rnorm(10, 5, 2), xaxp=c(1, 10, 10), yaxp=c(1, 10, 10), axes=FALSE)
axis(1, 1:10)
axis(2, 1:10)
abline(h=1:10, v=1:10, col="gray", lty=3)

grid example

More information about custom tick intervals in this thread and here for grid alignment.

查看更多
Melony?
3楼-- · 2019-01-21 23:32

For posterity, here is the long-winded way of doing it manually:

plot (x = 1:10, y = rnorm (10, 5, 2))
grid (lty = 6, col = "cornsilk2")

xaxp <- par("xaxp")
yaxp <- par("yaxp")

abline(v=seq(xaxp[1], xaxp[2], (xaxp[2]-xaxp[1])/xaxp[3]), lty=6, col = "cornsilk2")
abline(h=seq(yaxp[1], yaxp[2], (yaxp[2]-yaxp[1])/yaxp[3]), lty=6, col = "cornsilk2")
查看更多
三岁会撩人
4楼-- · 2019-01-21 23:41

From ?grid description of the nx and ny arguments:

When NULL, as per default, the grid aligns with the tick marks on the corresponding default axis (i.e., tickmarks as computed by axTicks)

plot (x = 1:10, y = rnorm (10, 5, 2)) 
grid (NULL,NULL, lty = 6, col = "cornsilk2") 
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-21 23:46

The answer provided here is much more straightforward, although you may dislike the lack of "free space" at each end of the axes. In brief,

The problem is that grid is putting nx grid lines in the user space, but plot is adding 4% extra space on each side. You can take control of this. Adding xaxs="i", yaxs="i" to your plot will turn off the extra space. But then your upper right point will be cut off, so you need to change the xlim and ylim values and change nx to match

查看更多
登录 后发表回答