An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110523/1217f92f/attachment.pl>
panel.first problem when plotting with formula
6 messages · Peter Ehlers, Gene Leynes, David Winsemius
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110524/680779ce/attachment.pl>
On May 24, 2011, at 11:42 PM, Gene Leynes wrote:
Peter, Good idea! (why didn't I think of that?) If it stumped the r-list, I think there is probably a slight bug with the plot formula. Problems like this make me realize how amazingly full featured and relatively bug free R is. A problem like this would never happen in Excel, because this level of functionality does not exist. However, if it did, it would probably never be fixed... and you could substitute "Excel" with "Any commercial software".
plot(dat, panel.first=bgfun() ) # succeeds So the problem is not with plot.data.frame. So someplace in the processing of dots and the handoff to do.call(funname, c(list(mf[[i]], y, ylab = yl, xlab = xl), dots)) ... where funname = "plot", the dot identities do not get honored. The 'plot" function is where it all started, but the first argument is now mf[[i]], and is that is now a numeric vector. So I think it gets handed off to plot.default, which sets panel.first to NULL.
David.
>
> Gene
>
>
> On Tue, May 24, 2011 at 3:13 AM, Peter Ehlers <ehlers at ucalgary.ca>
> wrote:
>
>> 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.
>>>
>>
>>
>
> [[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.
David Winsemius, MD
West Hartford, CT
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110525/66c862c0/attachment.pl>
On May 25, 2011, at 5:56 PM, Gene Leynes wrote:
David, Peter (and others), If you're interested, I submitted this as a bug, and was informed of the error of my ways by Professor Ripley * His informative reply is copied below. * The short answer is that panel.first is not a documented function of "plot.formula", which is called by the generic "plot".
Apparently not the first time he has been called upon to do so. Here is a similar question, albeit with no answer (at least in Baron's archive) at that time. http://finzi.psych.upenn.edu/Rhelp10/2009-September/210328.html (... the link to the ancient bug is broken.) But plot.formula promises to pass "..." arguments to later "hand offs" and apparently it munges up the 'dots' in a manner that plot.data.frame does not. In fact, plot.formula gets handed back to generic `plot`. Prof Ripley obviously has an understanding of the term `expression` that surpasses mine. Does your understaning of his reply extend to explaining why plot.data.frame works with our naive invocation of panel.first while his suggested syntax does not: plot(dat, panel.first=quote( bgfun() ) ) # Fails. plot(dat, panel.first= bgfun() ) # Succeeds. So I it still appears there is a demonstrable degree of inconsistency, even if there is no "bug".
The solution gives me some insight into how the lazy evaluation works. ## Note: It's still not a documented use of the function! plot(y ~ x, data=dat, panel.first=quote(bgfun())) On Wed, May 25, 2011 at 2:13 AM, <r-bugs at r-project.org> wrote: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14591 Brian Ripley <ripley at stats.ox.ac.uk> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |CLOSED Resolution| |INVALID --- Comment #1 from Brian Ripley <ripley at stats.ox.ac.uk> 2011-05-25 03:13:34 EDT --- panel.first is not a documented argument to plot.formula: please do read the help.
Yes, I did read the help page. I also looked at the code (of plot.formula, plot.data.frame, and plot.default) and made a good faith effort at following the flow of data through that code by inserting print and str statements at what appeared to be critical points so I could see where plot.formula was "going" and what it was being given to work with.
It is a documented argument to plot.default(), as
panel.first: an expression to be evaluated after the plot axes are set
^^^^^^^^^^
but you passed an evaluated function call. It first ran bgfun() and
then the
plot call. It worked for plot.default() by lazy evaluation.
I also tried using just panel.first=bgfun as I would have with lattice calls, and it did not succeed in any application.
You needed plot(y ~ x, data=dat, panel.first=quote(bgfun()))
David Winsemius, MD West Hartford, CT