1
votes

I have build up a dendrogram and colored its branches according to their "purity" (whether they only include subjects with a particular value in a factor variable) using the set("by_labels_branches_col") function of the dendextend package. Now, I would like to convert this dendrogram to a ggplot2 object for further customization. I have been able to do that with the function as.ggdend (also from the dendextend package). Here is when I encounter 2 issues for which I would need some help:

1-After using as.ggdend, the resulting object "loses" the vertical axis indicating the height of the dendrogram... How could I do this transformation without losing the axis?

2.-I have also tried to enrich my dendrogram by adding a colored bar using the colored_bars function of the dendextend package. However, I do not know how to save the resulting object to convert it to a ggplot object.

Here I provide an example of my code with the mtcars dataset

df=mtcars
ds=dist(df, "euclidean")
hc<-hclust(ds,method= "average")
de=as.dendrogram(hc)
library(dendextend)
code=rownames(df[df$cyl==4,])#factor for coloring
de2<-de%>%set("by_labels_branches_col", value = c(code))%>% set("labels", "")%>%as.dendrogram(de)#coloring branches
#to add the colored bar
colores<-c("red","black", "blue") [as.factor(df$cyl)]
plot(de2)
colored_bars(colors=colores,dend=de2, y_shift=-2, rowLabels="" )
#transform to ggplot
de3=as.ggdend(de2)

Thanks in advance for any possible answer

1
If df is derived from the mtcars dataset (judging by some variable names), could you add to your code example how you've derived df from mtcars? That'll make the code reproducible and easier to solve the question. If it is not derived from a standard dataset, can you include some dummy data that allows us to reproduce the issue?teunbrand
Teunbrad: Thanks for your comment. I did not realize that I had omitted the first line of code. Now it is corrected.cs-

1 Answers

1
votes

Finally, I have found a solution for the first of the posted questions). It is far from elegant and probably there are better ways to do this. However, I post it here just in case someone finds it useful.

The solution skips the use of as.ggdend and directly uses ggplot+theme to ensure that the dendrogram axis is displayed. Because this automatically thickens all plot lines, line sizes are corrected at steps 2/3.

step1=ggplot(de2)+
    theme(axis.line.y = element_line(color="black"),
    axis.text.y = element_text(color="black"),
    axis.ticks.y = element_line(color="black"))+
    scale_y_continuous(expand = expansion(add = c(0,0)))
    
    step2=ggplot_build(step1)
    step2$data[[1]]$size=0.3

    step3= ggplot_gtable(step2)

    step4=ggplotify::as.ggplot(step3)