How do I draw gridlines using abline() that are behind the data?

前端 未结 4 1604
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 06:57

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

4条回答
  •  清歌不尽
    2020-12-05 07:25

    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.

提交回复
热议问题