سؤال

I am trying to make a levelplot (also displying contour lines) from the lattice package. I am having a couple of issues (described below).

MyData: https://www.dropbox.com/s/ht55g0qlkiou2x2/growth.matrix.stackoverflow.xlsx

require(lattice)      
require(latticeExtra) 
require(xlsx)   

#matrix to be plotted
growth.matrix<-as.matrix(read.xlsx("C:/Users/eckmannm/Dropbox/growth.matrix.stackoverflow.xlsx",sheetName="p4.pct",colIndex=2:8,startRow=3,endRow=15,header=FALSE))

#column values
TempRange<-as.numeric(c(0, 2, 4, 6, 8, 10, 12))
#row values
MeanTemp<-as.numeric(c(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18))
#x and y axis labels
x.range<-as.numeric(c(6, 8, 10, 12, 14, 16, 18))

#color ramp
col.l <- colorRampPalette(c('red', 'orange', 'yellow', 'green', 'cyan', 'blue'))

colorplot<-
   levelplot(
   growth.matrix, 
   row.values=(MeanTemp),
   column.values=(TempRange),
   col.regions=col.l,
   at=seq(from=0,to=1.01,length=100),   
   lattice.options=list(key=list(cex=4)),
   panel = panel.2dsmoother,
   scales=list((x=list(labels=MeanTemp)), y=list(labels=TempRange)), 
   colorkey=list(at=as.numeric(factor(c(seq(from=0, to=1, by=.20)))),
               labels=as.character(c( "0", "20%", "40%", "60%", "80%", "100%")),
               col=(col.l)))

contourplot<- 
    contourplot(
    growth.matrix, 
    row.values=MeanTemp,
    column.values=TempRange,
    at=seq(from=0, to=1.01, by=.2), 
    panel=panel.2dsmoother,
    label.style=("flat"),
    lwd=2,
    labels=(list(cex=1.5,labels=(c("0", "20%", "40%", "60%", "80%", "100%")))))  

(final.plot= (colorplot + contourplot))

>> dput(head(growth.matrix))
structure(c(0.117236699239957, NA, 0.416693811074919, NA, 0.776134455117953, 
NA, NA, 0.263952225841477, NA, 0.582491856677524, NA, 0.855917480998914, 
NA, NA, 0.423344191096634, NA, 0.6792888165038, NA, NA, NA, NA, 
0.5164223669924, NA, 0.698561346362649, NA, NA, NA, NA, 0.534989142236699, 
NA, NA, NA, NA, NA, NA, 0.507193268186754, NA, NA, NA, NA, NA, 
NA), .Dim = 6:7, .Dimnames = list(NULL, c("X2", "X3", "X4", "X5", 
"X6", "X7", "X8")))

The plot I have been producing

1) I would really like to remove the vertical white border at the edge of the colored region (between the axis labels and the graph).

2) I would like the legend to have discrete labels, with different breakpoints than the color

3) For some reason the contour line labels are plotted off of the graph???

Any help would be greatly appreciated! Thanks in advance.

هل كانت مفيدة؟

المحلول

I would really like to remove the vertical white border at the edge of the colored region (between the axis labels and the graph).

Add xlim=c(6,18) to your levelplot call


I would like the legend to have discrete labels, with different breakpoints than the color

From ?levelplot:

at: numeric vector specifying where the colors change. must be of length 1 more than the col vector.

labels: a character vector for labelling the at values, or more commonly, a list describing characteristics of the labels. This list may include components labels, at, cex, col, rot, font, fontface and fontfamily.

So, essentially you can set colorkey as:

colorkey=list(at=seq(0, 1, 0.2), 
                 labels=list(at=c(0, 0.3, 0.6, 0.9), 
                             labels=c("none", "a bit", "a bit more", "a lot"))

Note that there is no need to use factor, as.numeric or as.character, so I removed them.


3) For some reason the contour line labels are plotted off of the graph???

Always from the help page:

labels: Typically a logical indicating whether contour lines should be labelled, but other possibilities for more sophisticated control exists. Details are documented in the help page for panel.levelplot, to which this argument is passed on unchanged. That help page also documents the label.style argument, which affects how the labels are rendered

And, in ?panel.levelplot we find that:

label.style: Controls how label positions and rotation are determined. A value of "flat" causes the label to be positioned where the contour is flattest, and the label is not rotated. A value of "align" causes the label to be drawn as far from the boundaries as possible, and the label is rotated to align with the contour at that point. The default is to mix these approaches, preferring the flattest location unless it is too close to the boundaries.

I think for your specific data the mixed (default) solution works best. The easiest thing is to remove the label.style parameter from the contourplot call altogether.

The same manual page also helps in styling the labels.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top