[R] Zoo panel function

Achim Zeileis Achim.Zeileis at uibk.ac.at
Tue Jul 31 10:24:50 CEST 2012


On Sun, 29 Jul 2012, Gene Leynes wrote:

> I would really like some help with understanding the panel function, in 
> zoo.  Thank you.

Have you looked at ?plot.zoo. Some of the features you ask about are 
explained there. In particular, it is explained that arguments like col, 
lty, etc. are expanded to the number of series. See also the "Plotting" 
section of vignette("zoo", package = "zoo").

In your case you want the color selection based on the response anyway, so 
you could do something like

mycol <- function(i) hcl(
   h = ifelse(i > 0, 260, 0),
   c = 80 * abs(i)^1.5,
   l = 90 - 60 * abs(i)^1.5
)
mypanel <- function(x, y, ...) {
   lines(x, y, col = "gray")
   points(x, y, col = mycol(y / max(abs(y))), pch = 19)
}
plot(zobj, panel = mypanel)

In case of a univariate series, you could also do plot(time(zobj), 
coredata(zobj), ...) and then use the usual base plot arguments.

(The above uses a diverging color scheme for selecting a color based on 
the value of the response. The underlying ideas are explained in Zeileis, 
Hornik, Murrell (2009). Escaping RGBland: Selecting Colors for Statistical 
Graphics. Computational Statistics & Data Analysis, 53, 3259-3270. 
doi:10.1016/j.csda.2008.11.033)

hth,
Z

> R 15.1 and zoo 1.7-7.
>
>
> library(zoo)
> x = seq(0,3*pi,length.out=100)
> y = sin(x)
> zobj = zoo(y, x)
>
> ###########################################################
> ## EXAMPLE 1 - GLOBAL ARGUMENT
> ## This panel function works
> ## But, it relies on mycol, which is a global variable
> ###########################################################
> palette(rainbow(100))
> mypanel_v1 = function(x, y, ...){
> lines(x, y, lty=2, col='grey')
> points(x, y, col=mycol, pch=16)
> }
> mycol = round((y - min(y)) / (max(y) - min(y)) * 99) + 1
> plot(zobj, panel=mypanel_v1)
>
>
> ###########################################################
> ## EXAMPLE 2 - PASSING IN MY_COLOR AS PARAM (WITH WARNING)
> ## How would I make the color argument modular?
> ## This works, but throws errors
> ## What is the best way to to this?
> ###########################################################
> palette(rainbow(100))
> mypanel_v2 = function(x, y, MY_COLOR, ...){
> lines(x, y, lty=2, col='grey')
> points(x, y, col=MY_COLOR, pch=16)
>
> ## By the way I also tried a variety of strategies
> ## like this:
> # points(..., col=MY_COLOR, pch=16)
> ## but I get got warnings about passing in pch and col
> ## more than once, and "matching multiple arguments".
>
> ## The col value has the length of number of zoo
> ## objects rather than the number of points in each
> ## column....
> }
> mycol = round((y - min(y)) / (max(y) - min(y)) * 50) + 1
> plot(zobj, panel=mypanel_v2, MY_COLOR = mycol)
>
>
>
> Thank you,
>   Gene Leynes
> _____________________________________________
> *Data Scientist*
> *http://www.linkedin.com/in/geneleynes
> *
> <http://goog_598053156>*http://geneorama.com/ <http://geneorama.com/%20>*
>
> 	[[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