【问题标题】:Make color palette of discretized values for a continuous variable (lattice levelplot)为连续变量制作离散值的调色板(格点图)
【发布时间】:2018-04-04 17:35:07
【问题描述】:

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

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

  • white 表示 p.values >0.05

  • red 表示 p.values 0.01

  • 深红色,p.values

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

产生:

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

【问题讨论】:

    标签: r graphics colors lattice discretization


    【解决方案1】:

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

    如果您希望区域为&lt;=0.01&gt;0.01 &amp; &lt;=0.05&gt;0.05,则需要在levelplot 调用中使用at 参数指定它(当然colorkey 也是如此) .

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

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

    给予:

    注意: 我是根据您对中断的描述调用的,对于您在代码中使用的中断,您需要在调用(例如,在定义col.regions之后)

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多