2017-05-05 137 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操作都是在你的函數內完成的。 –