Following up on this question, I want to make a levelplot heat map and color each cell according to the variable p.value
in my data frame.
I would like to have the cells colored in such way that there are only 3 colors (discretized color palette for continuous variable):
white for p.values >0.05
red for p.values <0.05 and >0.01
dark red for p.values <0.01
So far this is my MWE.
set.seed(150)
pv.df <- data.frame(compound=rep(LETTERS[1:8], each=3), comparison=rep(c("a/b","b/c","a/c"), 8), p.value=runif(24, 0, 0.2))
pv.df
myPanel <- function(x, y, z, ...) {
panel.levelplot(x, y, z, ...)
panel.text(x, y, round(z, 2))
}
#install.packages("latticeExtra")
library(latticeExtra)
library(RColorBrewer)
cols <- rev(colorRampPalette(brewer.pal(6, "Reds"))(10))
png(filename="test.png", height=1000, width=600)
print(
levelplot(p.value ~ comparison*compound,
pv.df,
panel = myPanel,
col.regions = cols,
colorkey = list(col = cols,
at = do.breaks(range(pv.df$p.value), 10)),
xlab = "", ylab = "", # remove axis titles
scales = list(x = list(rot = 45), # change rotation for x-axis text
cex = 0.8), # change font size for x- & y-axis text
main = list(label = "Total FAME abundance - TREATMENT",
cex = 1.5)) # change font size for plot title
)
dev.off()
Which produces:
I am showing the actual p.value
values in the levelplot. There seems to be a problem with the code above, since that "0.14" is colored darker than the "0.08" right next to it.