如何在R heatmap中为连续色条设置特定的颜色?

时间:2018-04-22 12:02:44

标签: r scale plotly heatmap colorbar

这是我的热图的可重复示例,其显示R图中差异表达的基因用于多个样本:

colMax <- function(data) sapply(data, max, na.rm = TRUE)
colMin <- function(data) sapply(data, min, na.rm = TRUE)
test <- structure(list(`#Log2FC_00e41e6a` = c(0, 0, 0, 0, 0, 0), `#Log2FC_0730216b` = c(0, 
0, 0, 2.85798206145049, 0, 0), `#Log2FC_07ccb4e9` = c(-2.92159741497064, 
0, -2.32475763591175, 0, 0, 0), `#Log2FC_1426b4bf` = c(0, 0, 
0, -2.95962954629017, 0, 0), `#Log2FC_181c6d37` = c(0, 0, 0, 
0, 0, 0), `#Log2FC_1d7ffbe7` = c(0, 0, 0, 0, 0, 0)), .Names = c("#Log2FC_00e41e6a", 
"#Log2FC_0730216b", "#Log2FC_07ccb4e9", "#Log2FC_1426b4bf", "#Log2FC_181c6d37", 
"#Log2FC_1d7ffbe7"), row.names = c("A1BG-AS1", "A1CF", "A2M", 
"A2ML1", "A4GALT", "AADAC"), class = "data.frame")

这是使用plotly生成R热图的代码:

f1 <- list(
    family = "Arial, sans-serif",
    size = 5,
    color = "lightgrey")

f2 <- list(
    family = "Old Standard TT, serif",
    size = 10,
    color = "black")

a <- list(
  title = "",
  titlefont = f1,
  showticklabels = TRUE,
  tickangle = 45,
  tickfont = f2,
  exponentformat = "E")

plot_ly(z = as.matrix(test),
        zmin=round(min(colMin(test))),
        zmax=round(max(colMax(test))),
        x = colnames(test),
        xgap = 2,
        y = rownames(test),
        ygap = 2,
        type = "heatmap",
        colors = c("red", "green") ) %>%
    layout(xaxis = a,
           margin = list(l =90,
                         r = 10,
                         b = 100,
                         t = 10))

产生

Heatmap

问题:如何将颜色条的0值设置为黑色?

如果我这样做:

plot_ly(z = as.matrix(test),
        zmin=round(min(colMin(test))),
        zmax=round(max(colMax(test))),
        x = colnames(test),
        xgap = 2,
        y = rownames(test),
        ygap = 2,
        type = "heatmap",
        colors = c("red", "black", "green") ) %>%
    layout(xaxis = a,
           margin = list(l =90,
                         r = 10,
                         b = 100,
                         t = 10))

然后产生:

heatmap2

但问题是可重复的例子是我的整个数据的一小部分。将它应用于我的数据给出了不同的比例,其中0具有与黑色不同的颜色。这个Stackoverflow question是一个类似的问题,但与我的不同,我只需要特定值的特定颜色。

编辑1:另外,根据我的previous question的答案,@ MarcoSandri说通过定义一个色阶数组,我们可以将这些值传递给plotly。我试过了:

colorScale <- data.frame(z=c(zmin=round(min(colMin(big_data))),
                             0,
                             zmax=round(max(colMax(big_data)))),
                         col=c("#ff0000", "#000000", "#00ff00"))
colorScale$col <- as.character(colorScale$col)

plot_ly(z = as.matrix(test),
        zmin=round(min(colMin(test))),
        zmax=round(max(colMax(test))),
        x = colnames(test),
        xgap = 2,
        y = rownames(test),
        ygap = 2,
        type = "heatmap",
        colorscale = colorScale ) %>%
    layout(xaxis = a,
           margin = list(l =90,
                         r = 10,
                         b = 100,
                         t = 10))

heatmap 3

如你所见,这有两个问题,第一个颜色与我在数组中给出的值不对应,第二个与前一个颜色相似,对我的整个数据不起作用。

提前致谢!

1 个答案:

答案 0 :(得分:2)

您的色阶未正确定义。 <button id="datereleased">Date Released</button> <div id="sortcontainer"> <li id="60"> <h2>Some game</h2> <h2 class="date" id="2015">(2015)</h2> <h3>45/100</h3> </li> <li id="45"> <h2>Some game</h2> <h2 class="date" id="2017">(2017)</h2> <h3>45/100</h3> </li> <li id="80"> <h2>Some game</h2> <h2 class="date" id="2013">(2013)</h2> <h3>45/100</h3> </li> </div>的{​​{1}}列应介于0和1之间,而不应位于zcolorScale之间。请参阅下面的可能解决方案:

zmin

enter image description here

修改
当比例不以零为中心时,此解决方案应该更好。

zmax