为连续变量(网格等级图)制作离散值的调色板 [英] Make color palette of discretized values for a continuous variable (lattice levelplot)

查看:260
本文介绍了为连续变量(网格等级图)制作离散值的调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续



我在levelplot中显示实际的 p.value 值。上面的代码似乎有问题,因为0.14的颜色比它旁边的0.08更深。

您没有指定用于区域的断点(就像您对colorkey所做的那样),因此就是颜色混杂。



如果您想要这些区域是 <= 0.01 > 0.01& < = 0.05 > 0.05 您需要在 levelplot 调用中指定它参数中(当然对于 colorkey )也是一样。



因此,在调用中,您需要告诉您需要不同区域的颜色为暗色,红色和白色( col.regions = c( (实际为下限),0.01,0.05和0.20(上限,这是对最大值<$的一种舍入)中的中断(darkred,red,white) c $ c> p.value 您的 data.frame )。



levelplot 然后调用:

  levelplot(p.value〜comparison * 
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 =,yl ab =,#删除轴标题
scales = list(x = list(rot = 45),#改变x轴文本旋转
cex = 0.8),# &安培; y轴文本
main = list(label =Total FAME abundance-TREATMENT,
cex = 1.5))

给:


$ b $ c> at = do.breaks(range(pv.df $ p.value),10))(例如,在定义 col.regions


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.

解决方案

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)

这篇关于为连续变量(网格等级图)制作离散值的调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆