1

跟进这个问题,我想制作一个水平图热图,并根据p.value我的数据框中的变量为每个单元格着色。

我想让单元格以只有 3 种颜色的方式着色(连续变量的离散调色板):

  • p.values > 0.05 为白色

  • p.values <0.05 和 >0.01 为红色

  • p.values <0.01 为深红色

到目前为止,这是我的 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()

产生:

测试

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

4

1 回答 1

2

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

如果您希望区域为<=0.01>0.01 & <=0.05并且>0.05您需要在levelplot调用中使用at参数指定它(当然对于 也是相同的colorkey)。

因此,在通话中,您需要告知您想要不同区域 ( col.regions = c("darkred", "red", "white")) 的颜色为“深红色”、“红色”和“白色”,在 0(实际上是下限)、0.01、0.05 和 0.20(上限,这是p.value您的最大值的一种舍入data.frame)。

你的levelplot电话是:

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))

给予:

在此处输入图像描述

注意: 我根据您对中断的描述调用,对于您在代码中使用的中断,您需要at = do.breaks(range(pv.df$p.value), 10))在调用中添加(例如,在定义之后col.regions

于 2017-10-26T08:05:38.040 回答