[R] Factor level coloring in trellis plot

Deepayan Sarkar deepayan at stat.wisc.edu
Thu Feb 17 15:52:28 CET 2005


On Thursday 17 February 2005 04:39, T.A.Wassenaar wrote:
> Hi :)
>
> Was just wondering whether someone could help me with
> adjustments to trellis plots (parallel).
>
> I've got two way multivariate data. I want to make
> parallel plots for one of the factors, and want to color
> the lines according to the other factor. The first thing I
> manage, but with the other I'm lost :( Can only change the
> overall color.
>
> This is basically how far I get:
>
> parallel(~data | factor, layout=c(4,1))
>
> Any hints will be greatly appreciated.

panel.parallel doesn't handle groups, so you'll need to write a panel 
function that does. This is easy enough, the current panel function 
needs only a few additions:


panel.parallel.new <- 
    function(z, subscripts,
             groups = NULL,
             col=superpose.line$col,
             lwd=superpose.line$lwd,
             lty=superpose.line$lty, ...)
{
    superpose.line <- trellis.par.get("superpose.line")
    reference.line <- trellis.par.get("reference.line")

    n.r <- ncol(z)
    n.c <- length(subscripts)
    if (is.null(groups)) {
        col <- rep(col, length=n.c)
        lty <- rep(lty, length=n.c)
        lwd <- rep(lwd, length=n.c)
    }
    else
    {
        gnum <- as.integer(as.factor(groups))
        n.g <- length(unique(gnum))
        col <- rep(col, length=n.g)
        lty <- rep(lty, length=n.g)
        lwd <- rep(lwd, length=n.g)
    }

    llim <- numeric(n.r)
    ulim <- numeric(n.r)
    dif <- numeric(n.r)
    if (n.r > 0)
        for(i in 1:n.r) {
            grid.lines(x = c(0,1), y = c(i,i),
                       default.units = "native",
                       gp = gpar(col = reference.line$col,
                       lwd = reference.line$lwd,
                       lty = reference.line$lty))
            llim[i] <- range(as.numeric(z[,i]))[1]
            ulim[i] <- range(as.numeric(z[,i]))[2]
            dif[i] <- ulim[i] - llim[i]
        }
   
    if (is.null(groups))
        for (i in seq(along=subscripts))
        {
            x <- (as.numeric(z[subscripts[i],,])-llim)/dif
            grid.lines(x = x,
                       y = 1:n.r, 
                       gp = gpar(col=col[i], lty=lty[i], lwd=lwd[i]),
                       default.units="native")
        }
    else 
        for (i in seq(along=subscripts))
        {
            x <- (as.numeric(z[subscripts[i],,])-llim)/dif
            grid.lines(x = x,
                       y = 1:n.r, 
                       gp =
                       gpar(col=col[gnum[subscripts[i]]],
                            lty=lty[gnum[subscripts[i]]],
                            lwd=lwd[gnum[subscripts[i]]]),
                       default.units="native")
        }
    invisible()
}


With this, you should get what you want with 

library(grid)
library(lattice)
parallel(~iris[1:4], iris, 
         panel = panel.parallel.new, 
         groups = Species)

I'll include this in the next release.

Deepayan




More information about the R-help mailing list