[R] panel.first problem when plotting with formula

Peter Ehlers ehlers at ucalgary.ca
Tue May 24 10:13:17 CEST 2011


On 2011-05-23 16:54, Gene Leynes wrote:
> I wrote a little function called bgfun that adds gridlines and a background,
> but it's not working with I plot using the formula.
>
> I have some theories on what's happening, but even if my theory is right, I
> don't know how to fix it.
>
> Someone have a straightforward silver bullet?

No silver bullet, but this seems to work:

   plot(y ~ x, data=dat, type="n")
   points(y ~ x, data=dat, panel.first=bgfun())

(I think that plot.formula may need a fix but
offhand I can't see whether that's easy or hard.)

Peter Ehlers

>
> Thank you,
>
> Gene
>
>
>
> bgfun = function(color='honeydew2',linecolor='grey45', addgridlines=TRUE){
>      tmp=par("usr")
>      rect(tmp[1], tmp[3], tmp[2], tmp[4], col=color)
>      if(addgridlines){
>          ylimits=par()$usr[c(3,4)]
>          abline(h=pretty(ylimits,10), lty=2, col=linecolor)
>      }
> }
> dat = data.frame(x=1:10,y=1:10)
>
> ## Works
> plot(dat$x, dat$y, panel.first=bgfun())
>
> ## Why doesn't this work?
> plot(y ~ x, data=dat, panel.first=bgfun())
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list