Make color palette of discretized values for a continuous variable (lattice levelplot)

我与影子孤独终老i 提交于 2019-12-11 07:29:18

问题


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.


回答1:


You didn't specifiy the breakpoints to use for the regions (as you did for the colorkey), hence the mishmash in the color.

If you want the regions to be <=0.01, >0.01 & <=0.05 and >0.05 you need to specify it inside the levelplot call with the at parameter (and of course the same for the colorkey).

So, in the call, you need to tell that you want the colors "darkred", "red", and "white" for the different region (col.regions = c("darkred", "red", "white")) with breaks at 0 (actually lower bound), 0.01, 0.05, and 0.20 (upperbound, which is a kind of rounding for the maximum p.value of your data.frame).

Your levelplot call is then:

levelplot(p.value ~ comparison*compound,
          pv.df,
          panel = myPanel,
          col.regions = c("darkred", "red", "white"),
          at=c(0, 0.01, 0.05, 0.20),
          colorkey = list(col = c("darkred", "red", "white"), 
                          at = c(0, 0.01, 0.05, 0.20)),
          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))

Giving:

N.B.: I based the call on your description of breaks, for the breaks you used in your code, you need to add at = do.breaks(range(pv.df$p.value), 10)) in the call (for example, after defining the col.regions)



来源:https://stackoverflow.com/questions/46901745/make-color-palette-of-discretized-values-for-a-continuous-variable-lattice-leve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!