[R] Question regarding lattice::levelplot and distribution of colors

Duncan Mackay dulcalma at bigpond.com
Mon Jul 7 06:42:20 CEST 2014


Your y values are not of the type required by levelplot
? levelplot 

I prefer not to use themes as they do not suit my data here is a way to get
what you want - colours are a bit garish but they are some easily to hand
breaks/cuts are just what came in a reasonable sequence - yours to change 

testdf$y <- ifelse(testdf$yy == "L",1,0)

levelplot(zz ~ xx + y, testdf, par.settings = custom.theme.2(),
          scales = list(x = list(cex = 0.5, rot=90), y = list(cex = 0.5),
                         alternating = FALSE),
          xlim = c(0, 50),
          at = seq(-0.3, 0.3, length = 20),
          panel = function(x, y, z, ...) {
              panel.levelplot(x, y, z,...)
          },
          colorkey=list(labels = list(cex = 1.5))
          )

  testcol <- c("#FF0000","#00FF00","#0000FF","#FFA54F",
             "#00FFFF","#FF00FF","#C00000","#00B414")

  levelplot(zz~ xx+y, testdf,#subset(testdf, xx > 0),
            at =  seq(-0.2,0, 0.025),
            cuts = 8,
            col.regions = testcol,
            scales = list(x = list(cex = 0.5,
                                   rot = 90),
                          y = list(cex = 0.5),
                          alternating = FALSE),
             xlim = c(0, 50),
             colorkey = list(labels = paste(seq(-0.2,0, 0.025)),
                             at =  seq(-0.2,0, 0.025),
                             cex = 1.5)
            )

Duncan

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

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Sunny Srivastava
Sent: Monday, 7 July 2014 09:35
To: R mailing list
Subject: [R] Question regarding lattice::levelplot and distribution of
colors

Hello R-helpers:

I think there is some problem with my code, but I would like to seek you
help because I can't spot it.

I have a data.frame defined as follows:

testdf <- structure(list(yy = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("R", "L"), class = c("ordered",
"factor")),
                         xx = c(8L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 32L,
33L, 34L, 37L, 38L, 44L, 47L, 48L),
                         zz = c(-0.0509470162236187, -0.127284016213917,
-0.148955671035087, -0.142907338502986, -0.160948815798959,
-0.173350477727937, -0.161083124553469,
                             -0.14273409035068, -0.0214438692797626,
-0.0628618704957434, -0.0877747107755074, -0.0948371137126557,
-0.0659475583478307, -0.0601482978919971,
                             -0.0339547824620206, -0.0433516197870341)),
                    .Names = c("yy", "xx", "zz"),
                    row.names = c("25", "26", "27", "28", "29", "210",
"211", "212", "213", "214", "215", "216", "217", "218", "219", "220"),
class = "data.frame")

I would like to see a 'levelplot'  of yy vs xx, but the value of xx creates
problem in the distribution of colors.  I am guessing it is due to the
value of xx = 8.  The levelplot below seems to be fine.

library(latticeExtra)


## ok; note the subset argument!
levelplot(zz ~ xx + yy, testdf, par.settings = custom.theme.2(), subset =
xx != 8,
          scales = list(x = list(cex = 0.5, rot=90), y = list(cex = 0.5),
alternating = FALSE),
          xlim = c(0, 50),
          at = seq(-0.3, 0.3, length = 20),
          panel = function(x, y, z, ...) {
              panel.levelplot(x, y, z,...)
          },
          colorkey=list(labels = list(cex = 1.5))
          )

If I remove the subsetting of xx != 8, then the distribution of colors is
wrong.  Specifically, it ranges from xx =1 to 20 for y = R, which is
incorrect.  What am I missing here?  Note that using the default color
scheme has no effect.

## seems to be a bug ? see xx = 1 to 20; note that the subset argument is
removed
levelplot(zz ~ xx + yy, testdf, par.settings = custom.theme.2(),
          scales = list(x = list(cex = 0.5, rot=90), y = list(cex = 0.5),
alternating = FALSE),
          xlim = c(0, 50),
          at = seq(-0.3, 0.3, length = 20),
          panel = function(x, y, z, ...) {
              panel.levelplot(x, y, z,...)
          },
          colorkey=list(labels = list(cex = 1.5))
          )


Thanks,
S.

	[[alternative HTML version deleted]]

______________________________________________
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