[R] Help with levelplot color assignment in lattice

sguyader sguyader at gmail.com
Sun Aug 28 15:15:14 CEST 2011


Dear R users,

I'm currently trying to make level plots of a longitudinal study of the
spatio-temporal spread of a plant disease in a field, but the results of the
color key assignment isn't what I expect.

Here's more info. I recorded the level of a disease on an ordinal scale from
0 (no disease) to 9 (dead plant) on a grid that has 9 rows and 14 columns.
The disease level on each plant was recorded on a weekly basis, and I what I
need is to make a levelplot from each weekly snapshot, and of course the
color levels need to be the same to compare images, and ultimately turn this
in an animation to visualize the spread of the disease in time and space.

In the following code, what I get from levelplot in lattice is a color scale
that is not centered, and not correct (I want white for level=0, red for
level=9, and yellow shades for intermediate levels). Is there a way to make
it look right ?

# Example data set from an intermediate snapshot. Note that at the maximal
level recorded in this one is 8, but I still want the color key to go up to
9 for comparison with later plots
data <- data.frame(x=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10,
11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13,
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14,
14),y=rev(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3,
4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,
2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8,
9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6,
7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4,
5, 6, 7, 8, 9)),level=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 1, 1, 1,
3, 2, 2, 1, 1, 1, 1, 1, 1))
library(lattice)
library(grDevices) # For the color palette
colpal <- colorRampPalette(c("white","yellow","red"))
colseq <- seq(0,9,by=1)
levelplot(level~ x* y, data = data,
            as.table = TRUE,
            at = colseq,
            region = TRUE,
            col.regions = colpal(10),
            colorkey = list(at = colseq, labels=list(at=colseq)),
            xlab="x",
            ylab="y",
            strip = strip.custom(factor.levels = c("date 3")))

As you should see, instead of having the level 0 polygon in white, and most
polygons in the first shade of yellow (level 1), most of them ar white. As
an extra, I would like to have the "NA" data appear as black, but I know I
can use "lrect" do to it.

The attached image is from the graph I can obtain from a custom fonction
calling the regular plot() and assigning colors to polygons, and
color.legend() from package plotrix to generate the color key. It gives me
the right kind of color key and colors assignment to polygons (and note how
the color key lebaels are centered vertically with regards to the polygons),
but the plot() is not as easily customizable as I'd like. I hope you can
help me figure it out with lattice.
http://r.789695.n4.nabble.com/file/n3774374/levelplot.jpg 

--
View this message in context: http://r.789695.n4.nabble.com/Help-with-levelplot-color-assignment-in-lattice-tp3774374p3774374.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list