1

How can I add a background grid to an R plot such that background lines are evenly spaced one unit apart from each other?

Here is what I've tried so far:

x = 1:10
y = 21:30
plot(x, y, yaxt="n")
axis(2, at=20:30, las=2)
grid(NULL, NULL)

x vs y

Here, I see that this call to grid aligns that grid with the default axis. (In this case, 22, 24, 26, 28, and 30.) However, I wish to align the grid with the new axis, i.e. 21, 22, ... 30. Furthermore, I see that this imposes the grid on top of the points. I'd like to see the grid in the background.

Note: I don't want a solution with absline(), unless it's possible to coerce those lines to the background.

Note 2: This is not a dupe of this question. They just want to add a grid. My question is different in that I want to add the grid in the background, and with custom-spaced lines.

Community
  • 1
  • 1
Everyone_Else
  • 3,206
  • 4
  • 32
  • 55
  • I've battled with this as well. The solutions I tend to use involve starting with `plot(NA, type = "n", xlim = c(...), ylim = c(...))` with known limits, adding the lines, and then adding the appropriate graphing elements to the canvas. This works fine when basic tools like `points`, `lines`, and `polygon` fit the bill, not so much with more complex plotting needs. – r2evans Jun 13 '16 at 23:49
  • Thank you, I'll try that out – Everyone_Else Jun 13 '16 at 23:50
  • Not a dupe - they just want to add a grid. My question is different in that I want to add the grid in the background, and with custom-spaced lines. – Everyone_Else Jun 13 '16 at 23:56
  • 2
    Could also be a duplicate of this: [grid line consistent with ticks on axis](http://stackoverflow.com/questions/8081931/grid-line-consistent-with-ticks-on-axis). This one is related, too: [how do i draw gridlines using abline that are behind the data](http://stackoverflow.com/questions/7263743/how-do-i-draw-gridlines-using-abline-that-are-behind-the-data). Make sure to have a look at the `panel.first` argument to `plot.default`. These related questions and answers on plotting custom grid lines and plotting grid lines behind data should allow you to work out a solution. – Jota Jun 14 '16 at 00:06
  • Those are related, but don't quite answer what I'm looking for. – Everyone_Else Jun 14 '16 at 00:15
  • Actually, the are related in the sense that everything can be put together to get to a solution, but any one of those answers would not suffice on its own. Because of this, I'd appreciate removal of the dupe flag. – Everyone_Else Jun 14 '16 at 00:22
  • I think `pch = 21, bg = "white"` is useful to you. – cuttlefish44 Jun 14 '16 at 15:34

1 Answers1

1

The way to do this is as follows:

x = 1:10
y = 21:30
plot(x, y, yaxt="n", panel.first = c(abline(h = 21:30, col="grey")), pch=19)
axis(2, at=20:30, las=2)

Custom intervals can be specified by changing what gets filled into c in the panel.first argument. Meanwhile, the call to axis adds the labels. Finally, this ensures that the grid lines are indeed in the background of the image.

This is the image produced by this code: enter image description here

Everyone_Else
  • 3,206
  • 4
  • 32
  • 55