Ggplot2 and ggdendro - display colored bars under node leaves

Currently, I use ggplot2and ggdendroto construct phylogenetic trees. However, now I need to build a discrete variable under the leaves along with the inscriptions.

For example, in a publication (Zhang et al., 2006), I saw a similar dendrogram (note the color bar under the sheet labels):

Example dendrogram

I am interested in doing the same with ggdendro + ggplot2, using data that I have already pinned. Is it possible?

+4
source share
1 answer

-, dataframe . , USArrests - hclust() . , , cutree() . states hc, , hc.

library(ggdendro)
library(ggplot2)
hc <- hclust(dist(USArrests), "ave")
df2<-data.frame(cluster=cutree(hc,6),states=factor(hc$labels,levels=hc$labels[hc$order]))
head(df2)
           cluster     states
Alabama          1    Alabama
Alaska           1     Alaska
Arizona          1    Arizona
Arkansas         2   Arkansas
California       1 California
Colorado         2   Colorado

- dendrogram colorbar, geom_tile(), states x cluster . .

p1<-ggdendrogram(hc, rotate=FALSE)


p2<-ggplot(df2,aes(states,y=1,fill=factor(cluster)))+geom_tile()+
  scale_y_continuous(expand=c(0,0))+
  theme(axis.title=element_blank(),
        axis.ticks=element_blank(),
        axis.text=element_blank(),
        legend.position="none")

@Baptiste , .

library(gridExtra)

gp1<-ggplotGrob(p1)
gp2<-ggplotGrob(p2)  

maxWidth = grid::unit.pmax(gp1$widths[2:5], gp2$widths[2:5])
gp1$widths[2:5] <- as.list(maxWidth)
gp2$widths[2:5] <- as.list(maxWidth)

grid.arrange(gp1, gp2, ncol=1,heights=c(4/5,1/5))

enter image description here

+7

All Articles