r Changing line width and branch color of dendrogram

Publish date: 2024-05-08

The ggdendro package offers a handy ggdendrogram() function for plotting the results of hierarchical cluster analysis. The appearance of the dendrogram generated by said function can be modified by tweaking several parameters of a ggplot object, but I have not been able to change either the line width and/or the colour of the dendrogram branches.

This is my test code (I am taking means just so simply the dendrogram for this question):

library(ggplot2) iris <- datasets::iris means <- aggregate(iris, by = list(iris$Species), FUN = mean) d <- dist(means) hc <- hclust(d, method = "average") dd <- ggdendrogram(hc) + theme_minimal() + labs(x = "Species", y = "Euclidean distance") + coord_flip() + theme(panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank()) plot(dd) 

What I need is probably something like this: Increase the size of line in geom_line, but I would like a simple solution without requiring tidyverse trickery!

Thanks in advance!

2

1 Answer

The function ggdendrogram is not in the ggplot package, but the "ggdendro" package. There is no option in the function itself to change line weights or colour, but you can change them in the object after it is created. When I run your code, I get the following plot:

dd 

To change the line size (here to size 3), we can do:

dd$layers[[2]]$aes_params$size <- 3 dd$layers[[2]]$aes_params$colour <- "red" dd$layers[[2]]$geom_params$lineend <- "round" 

Now we have

dd 

enter image description here

Created on 2022-10-14 with reprex v2.0.2

1

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobm1oZ2p%2Bd32Oq2ScoJGjtKq6xmajoqaVYsSqsNOhZJqmlGKvs63NnJ9mm5%2BhvLN5zp9knZ2emb%2Bws9GapA%3D%3D