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
df
is derived from themtcars
dataset (judging by some variable names), could you add to your code example how you've deriveddf
frommtcars
? 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