R
のplotly
という樹状図を持つクラスタヒートマップを作成する方法については、this exampleに従っています。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)
できます:ここでは例です、私は少しそれが私の本当の上で高速に実行するので、私の場合にヒートマップがplotly
はなくggplot
で生成されるようにコードを変更し
heatmap.plotly <- plot_ly() %>% add_heatmap(z=~mat,x=factor(colnames(mat),lev=colnames(mat)),y=factor(rownames(mat),lev=rownames(mat)))
そして:
与え210subplot(px, p_empty, heatmap.plotly, py, nrows = 2, margin = 0.01)
を私の質問は以下のとおりです。私は、彼らは両方のプロットにそうであるようにヒートマップの行と列のラベルが切断されませもらうにはどうすればよい
?
2番目の図では、colorerのラベルが「mat」に変更されています。どのようにそれを防止するための任意のアイデア?
ヒートマップとデンドログラムのマージンを変更するにはどうすればよいですか?
私はplotyオブジェクトのフィールドに直接アクセスするのではなく、インタフェースが多少変更されやすいので、 'colorbar(sply、title =" mat ")'を使用します。 – aocall