[英]Plotting a clustered heatmap with dendrograms using R's plotly
我正在按照這個例子說明如何使用R
的plotly
創建具有樹狀圖的聚類熱圖。 這是一個例子:
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)
這使:
我稍微更改了代碼,以便在我的情況下,熱圖是使用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)
我的問題是:
如何使熱圖的行標簽和列標簽不會像在兩個圖中那樣被切斷?
在第二張圖中,colorer的標簽更改為“mat”。 知道怎么預防嗎?
如何更改熱圖和樹形圖之間的邊距?
使用plotly制作完全工作的群集熱圖並不像開頭那樣簡單。 幸運的是,有一個名為heatmaply的R包可以做到這一點。 您可以在在線插圖中看到許多功能示例。
例如:
install.packages("ggplot2")
install.packages("plotly")
install.packages("heatmaply")
library(heatmaply)
heatmaply(scale(mtcars), k_row = 3, k_col = 2)
該圖是完全交互式的(來自熱圖和樹形圖)。 請注意,它使用dendextend ( ggdendro的更高級版本,也可以,例如,考慮分支顏色/線型/線寬)
專門設置樹形圖的邊距是一個懸而未決的問題(從今天開始),但這可能很快就會得到解決。
如何使熱圖的行標簽和列標簽不被切斷>就像在兩個圖中一樣?
在生成繪圖后嘗試設置margin
sply <- subplot(px, p_empty, heatmap.plotly, py, nrows = 2)
sply <- layout(sply,
margin = list(l = 150,
r = 0,
b = 50,
t = 0
)
)
在第二張圖中,colorer的標簽更改為“mat”。 知道怎么預防嗎?
不知道如何防止它,但你可以覆蓋標簽。
sply$x$data[[3]]$colorbar$title <- 'mat'
如何更改熱圖和樹形圖之間的邊距?
您可以為每個子圖的每個軸指定domain
。 yaxis
對應左上角的圖, 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)),
)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.