[R] Zoo panel function

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 1 13:35:55 CEST 2012


On Wed, Aug 1, 2012 at 7:31 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Wed, Aug 1, 2012 at 12:40 AM, Gene Leynes <gleynes at gmail.com> wrote:
>> Achim,
>>
>> Thank you for your response, and for your work on the zoo package in
>> general.  Also, that implementation of the color scheme looks great.
>>
>> Frankly, I can't remember exactly what I was originally trying to
>> accomplish for this particular problem, but I think that your suggestion of
>> reviewing the plotting section of the zoo vignette would solve the problem.
>>  Calculating the colors ahead of time and passing them in as a list is
>> great solution that I didn't know was possible.
>>
>> I still have some lingering questions about the panel function, which has
>> been one of those things in R that I would like to understand better.
>>
>> Using your example, I would like to be able to pass in an additional
>> variable to make each set of hues like this:
>>
>> mycol <- function(i, v)  hcl(
>>   h = ifelse(i > 0, v, 0),
>>   c = 80 * abs(i)^1.5,
>>   l = 90 - 60 * abs(i)^1.5)
>>
>> mypanel <- function(x, y, v, ...) {
>>   lines(x, y, col = "gray")
>>   points(x, y, col = mycol(y / max(abs(y)), v), pch = 19)
>> }
>> plot(zobj, panel = mypanel, v=0)
>> plot(zobj, panel = mypanel, v=50)
>> plot(zobj, panel = mypanel, v=100)
>> plot(zobj, panel = mypanel, v=150)
>>
>>
>> This runs, but generates warnings. I was trying to find the right way to
>> pass additional arguments to panel.
>>
>> I was trying things like this:
>> plot(zobj, panel = mypanel(x, y, v=150))
>> plot(zobj, panel = mypanel(..., v=150))
>>
>>
>> I was surprised at how many ways I could call panel, but how unpredictable
>> (to me) the results were!  Things that I didn't think would work were ok,
>> and other things that seemed correct would threw errors.
>>
>> If you have some insight I would like to hear it, but this isn't that
>> important because there are obviously other approaches for making the same
>> / similar output.
>>
>
> As documented in ?plot.zoo the ... argument consists of graphical
> parameters so its not just passed to the panel function -- you can
> expect warnings if, as here, the parameters are not graphical (as they
> are passed to other functions and not just the panel function).
>
> What you can do is to create a panel constructor that uses lexical
> scoping to encapsulate the additional parameters:
>
> make.mypanel <- function(v) function(...) mypanel(..., v)
> plot(zobj, panel = make.mypanel(v=0) )
>
> Here make.mypanel constructs a mypanel function with the v argument
> filled in so you don't have to separately pass it via ... .

One further idea that will save you from defining a constructor
function is to use Curry from the functional package like this:

library(functional)
plot(zobj, panel = Curry(mypanel, v=0) )

Here Curry returns mypanel but with v filled in already.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list