'How can we wrap or repel label names when plotting dendrogram horizontally in R?
Important remark: this question considers plotting a dendro using factoextra package but suggestions on how to plot dendro propely using base R ploting are welcome.
Consider we have a dataset with statements as our columns, rows are respondents, and we want to make clusters to receive segments. No problems with clustering, it's a basic task. The problem is to present this to your colleagues properly. When you are about to plot a dendrogram you find out that your statements do not fit into the frame of the picture.
I tried wrapping label names (like this paste(strwrap(x), width = 10), collapse = "\n")) and assign it to the dataframe before clustering. But eventually wrapped labels do not match with their original names, so clustering results are completely inaccurate.
Also, I tried to play with fviz_dend arguments to fit label names in to the picture. Unfortunately did not succeed.
How can we properly wrap or repel label names to make dendrogram more neat?
Reproducible example:
library(tidyverse)
library(factoextra)
library(vegan)
set.seed(16022022)
sample_tbl <- tibble(
"blablablablalba bla bla bla bla bla bla bla" = runif(1000, 0, 10),
"long long name namelong long name namelong long name name" = runif(1000, 0, 20),
"heyayayayayayayayayayayayayayayayayayayay song" = runif(1000, 0, 15),
"looooooooooooooooooooooooooooooooooooooooooooooooooong name" = runif(1000, 0, 1),
"hdjsdhakshdah idk what to write" = runif(1000, 20, 40),
"sometimes it is very boring to make a reprex" = runif(1000, 10, 23),
"but it helps users to guess what you want" = runif(1000, 0, 10),
"an some more long column names btw" = runif(1000, 0, 5),
"and the last one so i can finally post it" = runif(1000, 0, 10)
)
distance_matrix <- sample_tbl %>%
scale %>%
vegan::vegdist(., method = "euclidean") %>%
t
clusters_hc <- hclust(as.dist(distance_matrix), "ward.D2")
fviz_dend(
clusters_hc,
horiz = T
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
