如何将值分配给特定颜色,同时将所有其他值映射到连续的颜色渐变?

时间:2019-01-15 08:55:34

标签: r ggplot2 heatmap r-plotly ggplotly

我使用diffexpggplot制作了数据框架ggplotly()中的细胞亚群之间的差异基因表达的热图。

每个图块代表一对子簇之间差异表达的基因数量(在工具提示中显示为total),并且NA设置为灰色。

cluster.heatmap <- 

  ggplot(diffexp) +
  geom_tile(aes(x = cluster_A, y = cluster_B, fill = total, 
    text = paste0("up_genes: ", up_genes)), 
    colour="white", size=0.05) +
  scale_fill_viridis_c(begin=0.1, na.value="grey20") +
  ggtitle("Differentially Expressed Genes Between Subcluster Pairs")

enter image description here

当我使用ggplotly将其转换为交互式热图时,NA却改为白色:

ggplotly(cluster.heatmap)

ggplotly

我也尝试过使用heatmaply软件包,但同样的事情也会发生。

diffexp.spread <- select(diffexp, cluster_A, cluster_B, total) %>%
                   spread(cluster_B, total) %>% na_if(0)

heatmaply(diffexp.spread, na.color = "grey20")

enter image description here

我要解决的问题是以某种方式重新调整颜色映射,以便将0映射到grey20,并且viridis色标从1开始。

我知道我必须使用scales包,并可能创建一个新的颜色矢量,例如

colors.vir <- c("grey20", viridis(256))

scale_fill_gradient(colors = colors.vir,
                     values = scales::rescale(
                               (0, 0.1, # grey value limits
                                1, max(diffexp.spread) # viridis value limits) 
                    )

或类似上述内容,但我不太清楚。

0 个答案:

没有答案