将 ggplot 树状图添加到 plotly 热图时控制共享图例

Controlling the shared legend when adding a ggplot dendrogram plot to a plotly heatmap

我有 genes x samples 表达式数据 我想生成 plotly heatmap 并添加 samples dendrogram ggplot到.

这是我的数据:

set.seed(1)
mat <- matrix(rnorm(100*10),100,10,dimnames = list(paste0("G",1:100),paste0("S",1:10)))

这是 clusteringdendrograms:

library(dendsort)
library(dplyr)

col.hc <- hclust(dist(t(mat))) %>% dendsort::dendsort(.)
col.dend <- as.dendrogram(col.hc)
col.ord <- order.dendrogram(col.dend)
row.hc <- hclust(dist(mat)) %>% dendsort::dendsort(.)
row.dend <- as.dendrogram(row.hc)
row.ord <- order.dendrogram(row.dend)
mat <- mat[row.ord,col.ord]

这里我使用 dendextendcol.dend 创建一个 ggplot。请注意,所有 legend 关联的 textticks 都被抑制:

library(dendextend)
library(ggplot2)

col.gg.dend <- dendextend::as.ggdend(col.dend)
col.gg.dend.ggplot <- ggplot(col.gg.dend,labels=F)+guides(fill=F)+theme_minimal()+
  theme(axis.title=element_blank(),axis.text=element_blank(),axis.ticks=element_blank(),panel.grid=element_blank(),legend.position="none",legend.text=element_blank(),legend.background=element_blank(),legend.key=element_blank())

我在这里创建 plotly heatmap 并使用 plotly::subplot:

添加 col.gg.dend.ggplot
library(plotly)
library(reshape2)
library(grDevices)

plot.df <- reshape2::melt(mat,varnames=c("gene","sample"),value.name="value")

heatmap.plot <- plot_ly(z=c(plot.df$value),x=plot.df$sample,y=plot.df$gene,colors=colorRamp(c("darkblue","white","darkred")),type="heatmap",colorbar=list(title="Expression",len=0.4)) %>%
  layout(yaxis=list(title="Gene"),xaxis=list(title="Sample"))

empty.axis <- list(showticklabels=F,showgrid=F,zeroline=F,title=NULL)
empty.plot <- plot_ly() %>% layout(margin=list(l=200),xaxis=empty.axis,yaxis=empty.axis)
subplot(plotly_build(col.gg.dend.ggplot),empty.plot,heatmap.plot,nrows=2,margin=c(0,0,0,0),heights=c(0.2,0.8),widths=c(0.8,0.2))

这给了我:

除了将底部添加到 heatmap legend (black,solid,1)(NA,1) 之外,所有这些都很好用,我想 [=57] =].

请注意 plotly_build(col.gg.dend.ggplot) 绘制了 dendrogram 而没有 legend 部分。

一个相对简单的解决方法是:

subplot(col.gg.dend.ggplot,
        plotly_empty(), 
        heatmap.plot,
        nrows = 2,
        margin = c(0,0,0,0),
        heights = c(0.2,0.8),
        widths = c(0.8,0.2)) %>%
  layout(showlegend = FALSE)

中解释了潜在的问题。

layout options found later in the sequence of plots will override options found earlier in the sequence.

因为如果你只是颠倒次要情节的顺序。

subplot(heatmap.plot,
        plotly_empty(), 
        col.gg.dend.ggplot,
        nrows = 2,
        margin = c(0,0,0,0),
        heights = c(0.2,0.8),
        widths = c(0.8,0.2))

神器不见了。

如果您手动指定不应在热图中绘制图例,问题就会消失:

subplot(col.gg.dend.ggplot,
        plotly_empty(),
        heatmap.plot %>%
          layout(showlegend = F),
        nrows = 2,
        margin = c(0, 0, 0, 0),
        heights = c(0.2, 0.8),
        widths = c(0.8, 0.2))

并且颜色条向中间移动,表明热图图有一个不可见的图例,这触发了树状图的图例出现,因为它在子图序列中较晚。

请注意,在 subplot 中,您无需对 ggplot 对象调用 ggplotlyplotly_buildplotly_empty().

可以调用空图

另一种避免该问题的方法是使用 ggdendro:

library(ggdendro)
d.col <- dendro_data(col.dend)

col.2 <- ggplot() +
  geom_segment(data = d.col$segments, aes(x=x, y=y, xend=xend, yend=yend)) +
  labs(x = "", y = "") +
  theme_minimal() +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank())

subplot(col.2,
        plotly_empty(), 
        heatmap.plot,
        nrows = 2,
        margin = c(0,0,0,0),
        heights = c(0.2,0.8),
        widths = c(0.8,0.2)) 

生成与第一个帖子相同的情节。

heatmaply 包处理得很好(免责声明,我是贡献者):

library(heatmaply)
set.seed(1)
mat <- matrix(rnorm(100*10),100,10,dimnames = list(paste0("G",1:100),paste0("S",1:10)))

heatmaply(mat, dendrogram="column", col = cool_warm, key.title = "Expression", plot_method = "plotly")

不过,我在未设置 plot_method="plotly 时确实注意到了同样的问题。