[R] Help with levelplot color assignment in lattice

Duncan Mackay mackay at nsw.chariot.net.au
Mon Aug 29 03:03:47 CEST 2011


Hi


If you change the NA to a value either below or above the range of 
values you can then use it.
Below is something that is cobbled together from some code I have.
The datacols argument may be necessary if you want to do something 
fancy like I was doing
I used my own colours to see if all was working -
I frequently want colours that standout when placed in a grid rather 
than graduated shades.

data$level[is.na(data$level)] <- 10

datacols <-
   with(data,
       level.colors(level,
                    at =  c(-0.5,1:11),
                    col.regions = TRUE,
                    colors = FALSE))

levelplot(level~ x* y, data = data,
            #colors = datacols,
             as.table = TRUE,
                 at        = c(0:10),
                 col.regions = 
c("#FFFFFF","#00FF00","#0000FF","#FFA54F","#00FFFF","#FF00FF","#C00000","#00B414","#FFD18F","#000000"),
             colorkey = list(at = c(0:9,10), labels= 
c(as.character(c(0:9)),"dead")),
             xlab="x",
             ylab="y",
             strip = strip.custom(factor.levels = c("date 3")))

Regards

Duncan


Duncan Mackay
Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
Email: home mackay at northnet.com.au



At 23:15 28/08/2011, you wrote:
>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, 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, 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.
>
>______________________________________________
>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