[R] Can grid lines color in a plot be specified?

Marc Schwartz MSchwartz at MedAnalytics.com
Fri Oct 1 17:14:55 CEST 2004


On Fri, 2004-10-01 at 09:31, davidr at rhotrading.com wrote:
> I usually use something like
> 
> abline(h=seq(...),col="green")
> abline(v=seq(...),col="green")
> 
> This allows you to have irregularly spaced grid lines if you want. (Say
> for futures expiration dates in my case.)
> 
> Also, as Marc pointed out, you may want to draw the lines or points
> after the grid lines.

<snip>

Also, again with the aid of further caffeine, as noted in the help for
grid(), one can use the 'panel.first' argument in plot() to enable the
creation of the grid prior to the plotting of the data. 

For example:

plot(rnorm(10), panel.first = grid(), pch = 19)

yields the same results and plotting sequence as:

x <- rnorm(10)
plot(x, type = "n")
grid()
points(x, pch = 19)

As an example of using this approach with irregularly space grid lines
and axis tick marks, as per David's point:

plot(rnorm(10), panel.first = abline(v = c(1, 2, 3, 7), col = "grey", 
     lty = "dotted"), 
     pch = 19, xaxt = "n")

axis(1, at = c(1, 2, 3, 7))


There is also a 'panel.last' argument available which, of course, is
executed after all other plotting is done. More information is available
from ?plot.default.

I have not done an exhaustive review of all plotting functions/methods,
but I suspect not all of them support the panel.first and panel.last
arguments.

HTH,

Marc




More information about the R-help mailing list