[R] panel.groups: use group.number to define colors

baptiste auguie ba208 at exeter.ac.uk
Tue Oct 7 20:18:03 CEST 2008


Hi, and thanks for your email,

I realise my example was not very good. The actual dataset I'm trying  
to plot is rather big and this oversimplified example did not make  
much sense.

I actually do need to color different subsets of the data differently  
in each panel, that's why I thought of using panel.groups. Here's a  
more realistic example:

>
> x <- seq(1, 2*pi, length=100)
> numberOfCurves <- 20
> y1 <- sapply(seq(0, pi/2, length=numberOfCurves), function(phi) sin(x 
> +phi))
> y2 <- sapply(seq(0, pi/2, length=numberOfCurves), function(phi) cos(x 
> +phi))
> y <- cbind(y1, y2)
>
> fact <- factor(rep(c("cos", "sin"), each=numberOfCurves*100))
> fact2 <- factor(rep(seq(0, pi, length=numberOfCurves), each=100,  
> length=2*numberOfCurves*100))
>
> my.df <- data.frame(x=rep(x, length=800), y=as.vector(y), fact =  
> fact, fact2 = fact2)
> head(my.df)
>
>
> myColors <- c("grey", "grey", "red", rep("grey", ncol(y)-3))
>
> 	xyplot(y ~ x | fact,  data = my.df, groups = fact2, type="l",
> 		par.settings=list(superpose.line=list(col=myColors, lwd=2)),
> 	      panel =  panel.superpose,
> 	      panel.groups = function(..., group.number) {
>
> 	        panel.xyplot(...)
> 	
> 	      })

Two things I don't like about my approach:

- I'd rather select the colors in the panel function than set a  
specific palette in par.settings, as it's not obvious to me what the  
order of the plotting will be. This is where I fail to use  
group.number correctly

- the purpose of the red line is make this particular curve stand out  
from the mess of grey curves. However, they partially cover it and I  
don't really know how to change the plotting order (or replot the red  
one only on top if it's any easier)

Hope this is a bit clearer,

Best regards,

baptiste





On 7 Oct 2008, at 18:25, Bert Gunter wrote:

> Not exactly sure what you want to do, but ...
>
> In your example, you do not need groups, since the color doesn't  
> change
> within the levels of the conditioning variable (fact). Hence you can  
> use the
> panel.number() function to choose the plotting color of each panel,  
> like
> this:
>
> ## .. continuing with your example
>
> myColors <- rep(c(2,4),2)
>
> xyplot(y ~ x | fact,  data = my.df,
>              panel =  function(...){
>            panel.xyplot(...,col=myColors[panel.number()])
>         }
>          )
>
> If you actually **need** groups (to color different subsets of the  
> data
> within a panel differently). it does get a bit more complicated.
> Incidentally, note that col is already a formal argument of  
> panel.superpose
> and was therefore  picked up in the ... argument of the panel.groups
> function -- that's why you got the error you did when you repeated col
> explicitly in the panel.xyplot call. By default, it's values are  
> those of
> trellis.par.get("superpose.symbol") I believe. Also, group.number  
> appears to
> be undefined in your code.
>
> HTH
>
> Cheers,
> Bert Gunter
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list