Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

r

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.

like image 483
maxwelljd Avatar asked Aug 31 '11 21:08

maxwelljd


People also ask

Which command is correct to add gridlines in the graph?

Click the chart, and then click the Chart Design tab. Click Add Chart Element > Gridlines. Choose the axis that you want to apply the gridlines to or click More Gridline Options to open the Format Major Gridlines pane.

What is the purpose of a gridline in a graph?

Grid lines are lines that cross the chart plot to show axis divisions. Grid lines help viewers of the chart see what value is represented by an unlabeled data point. Especially for large or complicated charts, grid lines give valuable cues to the viewer.


1 Answers

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'))) 
like image 157
John Avatar answered Sep 19 '22 05:09

John