[R] Color bins for lattice levelplot
Alex Reynolds
reynolda at uw.edu
Tue Dec 27 13:37:16 CET 2011
I am creating a levelplot figure with the following code. This should be repeatable code, which includes a link to the data I am working with.
---------------------------
pdfFn <- "foo.pdf"
df <- read.table("http://dl.dropbox.com/u/31495717/stackoverflow.overlaps.list.txt", sep="\t", header=FALSE)
names(df) <- c("x", "y", "level")
df$level <- round(df$level*100, 0)
# reorder cell type row-factors (in reverse of given order)
df$y <- factor(df$y, levels=unique(df$y[length(df$y):1]))
lowestValue <- min(df$level)
secondHighestValue <- unique(sort(df$level, decreasing=TRUE))[2]
numberOfColorBins <- 10
col.seq <- seq(lowestValue, secondHighestValue, length.out=numberOfColorBins)
brks <- c(0, col.seq, Inf)
cuts <- cut(df$level, breaks=brks)
colors <- colorRampPalette(c("white", "red"))(length(levels(cuts))-1)
colors <- c(colors, "black")
cls <- rep(colors, times=table(cuts))
library(lattice)
trellis.device(dev=pdf, file=pdfFn)
fig <- levelplot(cuts~x*y,
data=df,
cuts=numberOfColorBins,
col.regions=cls,
xlab="",
ylab="",
aspect="iso",
scales=list(
x=list(rot=90)
),
panel=function(...) {
arg <- list(...)
panel.levelplot(...)
panel.text(df$x, df$y, df$level, cex=0.5)
},
colorkey=list(col=colorRampPalette(c("white", "red"))(length(col.seq)), at=col.seq)
)
print(fig)
graphics.off()
---------------------------
For reference, here is what the graph ("foo.pdf") looks like:
* http://twitpic.com/7z9u2c
Please note, in this example, the diagonal of black cells (representing cells with a level of "100").
The variable 'numberOfColorBins' defines the number of gradients between white and red, for values between the minimum level, and the second-highest level.
If I adjust the variable 'numberOfColorBins' from the current 10 to some value 16 or greater, then the diagonal of black cells (values of "100") turns into a diagonal of red cells.
Because I define red cells as between (in this example) the minimum of "14" and the second-highest maximum "79", values of "100" should always be black.
My question is:
If I have 16 or more color bins between white and red, inclusive, then cells that were colored black are now colored red, which is incorrect. How can I increase the number of color bins between white and red, while keeping black cells black?
Thanks for any advice.
Regards,
Alex
More information about the R-help
mailing list