[R] levelplot question

Greg Snow Greg.Snow at imail.org
Tue May 5 17:58:13 CEST 2009


The function that is doing the color assignments is level.colors in the lattice package.  Looking at the code confirms that the number of colors should be 1 less than the length of the at variable (the documentation implies that it should be 1 more, looks like a documentation bug to me).

It is possible that at one time the author intended to prepend -Inf and append Inf to the at vector so that it did not need to span the entire range of the data, but that was not implemented.  I think I would prefer that fix to changing the documentation.

The level.colors function uses the cut function to decide on which color to use, by default cut will put a value that matches a cutpoint in the group to the left, so to answer you original question 3.5 goes into the (2.5,3.5] range rather than the (3.5,4.5] group.  level.colors does not allow for changing this (hard coded), so if you want the other behaiviour, you either need to rewrite level.colors, or add a very small number to your data to shift 3.5 and the like into the right bin.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: Antje [mailto:niederlein-rstat at yahoo.de]
> Sent: Tuesday, May 05, 2009 1:00 AM
> To: Greg Snow; r-help at stat.math.ethz.ch
> Subject: Re: [R] levelplot question
> 
> Hi Greg and all the others,
> 
> thanks for your answer. The color-vector has the same length like the
> at-vector
> but the recycling cannot be the reason, because only values slightly
> above my
> "threshold" doe not appear blue.
> I cannot find a good explanation of which colors are assigned to which
> value
> ranges.
> 
> I've made little example:
> 
> #--------------------------------------------------------
> mat <- matrix(seq(1,5, length.out = 12), nrow = 3)
> mat[1,2] <- 3.5
> 
> my.at <- seq(0.5,5.5)
> my.col.regions <- rainbow(6)
> 
> graph <- levelplot(t(mat[nrow(mat):1, ] ), at = my.at, col.regions =
> my.col.regions)
> print(graph)
> 
> windows()
> plot(1:10)
> legend("topleft", legend = as.character(my.col.regions), col =
> my.col.regions,
> pch = 18)
> #--------------------------------------------------------
> 
> As you can see the green (position 3 in my.col.regions) disappears
> completely
> in the levelplot (look at the color range at the right side!).
> I guess it might also happen in my case...
> 
> I've tested several cases and it looks like the length of the color-
> vector
> should be one less than the at vector (which would make sense).
> 
> Then, the rule might apply:
> 
> [ at[1],at[2] ] = color[1]
> ( at[2],at[3] ] = color[2]
> ...
> ( at[n-1],at[n] ] = color[n-1]
> 
> 
> Please correct me if I'm wrong!!!
> 
> Antje




More information about the R-help mailing list