36

When I draw grid lines on a plot using abline() the grid lines are drawn over the data.

Is there a way to draw the abline() lines behind the data? I feel this would look better.

Example:

x <- seq(0, 10)
y <- x
plot(x, y, col = 'red', type = 'o', lwd = 3, pch = 15)
abline(h = seq(0, 10, .5), col = 'lightgray', lty = 3)
abline(v = seq(0, 10, .5), col = 'lightgray', lty = 3)

The plot produced has the gray grid lines going over the data (red line). I would like the red line to be on top of the gray lines.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
maxwelljd
  • 363
  • 1
  • 3
  • 5

4 Answers4

47

The panel.first argument of plot() can take a list or vector of functions so you can put your abline() calls in there.

plot(1:4, panel.first = 
       c(abline(h = 1:4, lty = 2, col = 'grey') 
        ,abline(v = 1:4, lty = 2, col = 'grey')))
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
John
  • 23,360
  • 7
  • 57
  • 83
16

Use plot() to set up the plotting window, but use type = "n" to not plot any data. Then do your abline() calls, or use grid(), and then plot the data using whatever low-level function is appropriate (here points() is fine).

x <- seq(0, 10)
y <- x
plot(x, y, type = "n")
abline(h = seq(0, 10, .5), col = 'lightgray', lty = 3)
abline(v = seq(0, 10, .5), col = 'lightgray', lty = 3)
points(x, y, col = 'red', type = 'o', lwd = 3, pch = 15)

or

## using `grid()`
plot(x, y, type = "n")
grid()
points(x, y, col = 'red', type = 'o', lwd = 3, pch = 15)

See ?grid for details of how to specify the grid as per your abline() version.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
8

Plot first with type="n" to establish coordinates. Then put in the grid lines, then plot again with your regular plot type:

plot(x, y, col = 'red', type = 'n', lwd = 3, pch = 15)
abline(h = seq(0, 10, .5), col = 'lightgray', lty = 3)
abline(v = seq(0, 10, .5), col = 'lightgray', lty = 3)
par(new=TRUE)
plot(x, y, col = 'red', type = 'o', lwd = 3, pch = 15)

I admit that I have always thought the name for that par parameter was "backwards."

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • backwards, and not really needed if you plot the data using `points()` for example. – Gavin Simpson Aug 31 '11 at 21:46
  • I didn't realize that 'type' argument was available in `points`. – IRTFM Aug 31 '11 at 21:50
  • It's not necessary with points(). I think Gavin was implying that you would make your initial type = 'n' plot() call as you did but then just change your last line to points( instead of plot(. Then the par() call isn't needed. BTW, type is available in points and lines I believe, I know that I sometimes want both and just call lines() with type = 'b'. – John Aug 31 '11 at 22:15
  • I understood his point. I was just explaining why I didn't use two calls to points and lines. – IRTFM Aug 31 '11 at 22:41
1

Another way of creating grid lines is to set tck=1 when plotting or in the axis function (you may still want to plot the points using points after creating the grid lines.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110