具有分层聚类的堆叠条形图(树状图)

Lu *_*Pan 5 r dendrogram bar-chart dendextend

我正在尝试获取类似的内容,但不幸的是,我找不到任何可以使我用树状图绘制堆积条形图的软件包,如下所示:

树状图

有人知道怎么做吗?

Tal*_*ili 3

第一次尝试找到答案 - 但需要更多的工作才能使其真正发挥作用。具体来说,需要更仔细地考虑元素位置的对齐(以及它们的顺序)。

# library
library(ggplot2)

# create a dataset
specie=c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition=rep(c("normal" , "stress" , "Nitrogen") , 4)
value=abs(rnorm(12 , 0 , 15))
data=data.frame(specie,condition,value)


dend <- as.dendrogram(hclust(dist(with(data, tapply(value, specie, mean)))))

data$specie <- factor(data$specie, levels = labels(dend))

# Stacked Percent
library(dendextend)
p1 <- ggplot(dend, horiz = T) 
p2 <- ggplot(data, aes(fill=condition, y=value, x=specie)) + 
    geom_bar( stat="identity", position="fill") + coord_flip()

library(cowplot)
plot_grid(p1, p2, align = "h")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述