2017-05-05 138 views
4

我跟随this example关于如何用Rplotly树状图创建聚类热图。这里的例子:用树状图绘制聚类热图与绘图使用R的情节

library(ggplot2) 
library(ggdendro) 
library(plotly) 

#dendogram data 
x <- as.matrix(scale(mtcars)) 
dd.col <- as.dendrogram(hclust(dist(x))) 
dd.row <- as.dendrogram(hclust(dist(t(x)))) 
dx <- dendro_data(dd.row) 
dy <- dendro_data(dd.col) 

# helper function for creating dendograms 
ggdend <- function(df) { 
    ggplot() + 
    geom_segment(data = df, 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()) 
} 

# x/y dendograms 
px <- ggdend(dx$segments) 
py <- ggdend(dy$segments) + coord_flip() 

# heatmap 
col.ord <- order.dendrogram(dd.col) 
row.ord <- order.dendrogram(dd.row) 
xx <- scale(mtcars)[col.ord, row.ord] 
xx_names <- attr(xx, "dimnames") 
df <- as.data.frame(xx) 
colnames(df) <- xx_names[[2]] 
df$car <- xx_names[[1]] 
df$car <- with(df, factor(car, levels=car, ordered=TRUE)) 
mdf <- reshape2::melt(df, id.vars="car") 
p <- ggplot(mdf, aes(x = variable, y = car)) + geom_tile(aes(fill = value)) 

mat <- matrix(unlist(dplyr::select(df,-car)),nrow=nrow(df)) 
colnames(mat) <- colnames(df)[1:ncol(df)-1] 
rownames(mat) <- rownames(df) 

# hide axis ticks and grid lines 
eaxis <- list(
    showticklabels = FALSE, 
    showgrid = FALSE, 
    zeroline = FALSE 
) 

p_empty <- plot_ly(filename="r-docs/dendrogram") %>% 
    # note that margin applies to entire plot, so we can 
    # add it here to make tick labels more readable 
    layout(margin = list(l = 200), 
     xaxis = eaxis, 
     yaxis = eaxis) 

subplot(px, p_empty, p, py, nrows = 2, margin = 0.01) 

这给:

enter image description here

我改变了代码,以便有点,在我的情况下,热图与plotly而不是ggplot,因为它的运行速度更快我真正产生大数据,所以我做的:

heatmap.plotly <- plot_ly() %>% add_heatmap(z=~mat,x=factor(colnames(mat),lev=colnames(mat)),y=factor(rownames(mat),lev=rownames(mat))) 

然后:

subplot(px, p_empty, heatmap.plotly, py, nrows = 2, margin = 0.01) 

这给: enter image description here

我的问题是:

  1. 我如何获得的,因为他们在这两个地块做热图不会被切断的行和列标签?

  2. 在第二个图中,着色器的标签更改为“垫子”。任何想法如何防止?

  3. 如何更改热图和树状图之间的边距?

回答

0

如何获得热图的行和列标签得不到,因为他们在这两个地块做隔断>?

尝试产生

sply <- subplot(px, p_empty, heatmap.plotly, py, nrows = 2) 
sply <- layout(sply, 
       margin = list(l = 150, 
          r = 0, 
          b = 50, 
          t = 0 
          ) 
       ) 

的colorer的标签被改变为在所述第二数字“垫”的情节后设置margin秒。任何 想法如何预防?

不知道如何防止它,但你可以覆盖标签。

sply$x$data[[3]]$colorbar$title <- 'mat' 

如何更改热图和树形图的利润率?

您可以为每个子图的每个轴指定domainyaxis对应于左上角的图,yaxis2到右侧的图中等。

增加距离比减少距离效果更好。

sply <- layout(sply, 
       yaxis = list(domain=c(0.47, 1)), 
       xaxis = list(domain=c(0, 0.5)), 
       xaxis3 = list(domain=c(0, 0.5)), 
       xaxis4 = list(domain=c(0.5, 1)), 
       ) 

enter image description here

pl <- subplot(px, p_empty, p, py, nrows = 2) 
heatmap.plotly <- plot_ly() %>% add_heatmap(z=~mat,x=factor(colnames(mat),lev=colnames(mat)),y=factor(rownames(mat),lev=rownames(mat))) 
sply <- subplot(px, p_empty, heatmap.plotly, py, nrows = 2) 
sply$x$data[[3]]$colorbar$title <- 'mat' 
sply <- layout(sply, 
       yaxis = list(domain=c(0.47, 1)), 
       xaxis = list(domain=c(0, 0.5)), 
       xaxis3 = list(domain=c(0, 0.5)), 
       xaxis4 = list(domain=c(0.5, 1)), 
       margin = list(l = 150, 
          r = 0, 
          b = 50, 
          t = 0 
          ) 


       ) 

sply 
+0

我会使用'colorbar(sply,title =“mat”)'而不是直接访问绘图对象中的字段,因为接口有点容易改变。 – aocall

3

制作一个完全工作组热图与plotly不是那么简单,因为它可能在一开始看起来。幸运的是,有一个名为heatmaply的R软件包就是这样做的。您可以在online vignette中看到许多功能的示例。

例如:

install.packages("ggplot2") 
install.packages("plotly") 
install.packages("heatmaply") 

library(heatmaply) 
heatmaply(scale(mtcars), k_row = 3, k_col = 2) 

enter image description here

该图是完全交互式的(无论是从热图和树状图)。请注意,它使用dendextend

具体地说setting the margins of the dendrograms是一个开放的问题(ggdendro,其也可以,只是举例,考虑分支颜色/线型/线宽的更发达版本)(来自刚刚今天),但这很快就会解决。

+0

谢谢@Tal Galili。 heatmaply看起来不错。有没有办法创建一个heatmapr对象,其中cellnote是一个字符矩阵而不是数字?其目的是将鼠标悬停在单元格上将显示行和列标签附加的文本信息(例如,如果这是通过样本热图的基因表达,我想显示基因的描述,其加入等)。我尝试将这样的字符矩阵传递给cellnote,但它崩溃。任何解决方案 – user1701545

+0

此外,我尝试将ggdend对象传递给heatmapr中的Rowv和Colv,但未显示它们。 – user1701545

+0

Hi @ user1701545。 (1)关于这个手写笔,有趣的想法。请创建一个自包含的小例子,并向我们提交问题以查看https://github.com/talgalili/heatmaply/issues(2)您应该传递一个常规树状图(可能使用dendextend :: color_branches进行修改) Rowv和Colv。所有的ggdend操作都是在你的函数内完成的。 –