rhierarchical-clusteringfactoextra

Rotate x-axis value in dendogram using fviz_dend


I was trying to totate x-axis value (45 degree) in dendogram using fviz_dend function from factoextra package, but nothing works.

enter image description here

I also tried to follow the answer in this post rotating dendogram x axis label in R, but no luck either. The following is my code:

library(factoextra)
res.dist <- dist(data, method = "euclidean")
res.hc <- hclust(d = res.dist, method = "ward.D2")
dendogram <- fviz_dend(res.hc, k = 2, cex = 0.5, k_colors = c("#1B9E77", "#7570B3"), 
          color_labels_by_k = FALSE)

This is my data:

structure(list(TPACK = c(4.35714285714286, 4.07142857142857, 
4.30952380952381, 3.92857142857143, 4.5952380952381, 4.30952380952381, 
3.57142857142857, 3.80952380952381, 3.69047619047619, 3.71428571428571, 
4.16666666666667, 4.5, 3.78571428571429, 4.30952380952381, 4.28571428571429, 
3.78571428571429, 4.30952380952381, 3.9047619047619, 3.26190476190476, 
3.71428571428571, 2.85714285714286, 4.14285714285714, 4.4047619047619, 
4.23809523809524, 4.35714285714286, 4.28571428571429, 4, 4.97619047619048, 
3.66666666666667, 4.0952380952381, 4.92857142857143, 4.33333333333333, 
4.85714285714286, 4.33333333333333, 4.04761904761905, 4.85714285714286, 
4.11904761904762, 4.5, 3.69047619047619, 3.11904761904762, 3.92857142857143, 
4.80952380952381, 4.69047619047619, 4.04761904761905, 3.80952380952381, 
4.14285714285714, 4.38095238095238, 3.83333333333333, 4.9047619047619, 
4.88095238095238, 3.4047619047619, 4.78571428571429, 3.5952380952381, 
3.69047619047619, 4.33333333333333, 4.07142857142857, 4.33333333333333, 
4.11904761904762, 4.54761904761905, 3.04761904761905, 4.97619047619048, 
3.42857142857143, 4, 4.28571428571429, 3.23809523809524, 3.92857142857143, 
4.21428571428571, 3.45238095238095, 3.80952380952381, 3.66666666666667, 
4.88095238095238, 3.5952380952381, 3.69047619047619, 3.78571428571429, 
4.21428571428571, 4.02380952380952, 3.85714285714286, 3.42857142857143, 
4.54761904761905, 4.35714285714286, 4.11904761904762, 3.92857142857143, 
3.80952380952381, 3.23809523809524, 3.4047619047619, 4.52380952380952, 
3.97619047619048, 4.38095238095238, 4.14285714285714, 3.85714285714286, 
4.66666666666667, 3.97619047619048, 3.11904761904762, 4.02380952380952, 
3.54761904761905, 4.95238095238095, 4.9047619047619, 4.28571428571429, 
4.85714285714286, 3.76190476190476, 3.42857142857143, 2.66666666666667, 
4.33333333333333, 4.69047619047619, 3.0952380952381, 4.76190476190476, 
3.80952380952381, 4.45238095238095, 4.07142857142857, 4.64285714285714, 
3.64285714285714, 4.30952380952381, 3.97619047619048, 4.23809523809524, 
3.5952380952381, 4.30952380952381, 2.5, 3.92857142857143, 3.14285714285714, 
3.83333333333333, 3.69047619047619, 4.07142857142857, 4.28571428571429, 
4.5952380952381, 3.5, 4.57142857142857, 3.5, 3.85714285714286, 
3.95238095238095, 3.9047619047619, 3.57142857142857, 3.52380952380952, 
3.02380952380952, 3.52380952380952, 4.54761904761905, 3.95238095238095, 
4.78571428571429, 3.88095238095238, 3.80952380952381, 4.5, 4.16666666666667, 
3.45238095238095, 4.42857142857143, 3.95238095238095, 2.83333333333333, 
4.97619047619048, 3.83333333333333, 4.19047619047619, 3.5, 3.83333333333333, 
4.78571428571429, 3.85714285714286, 4.4047619047619, 4.80952380952381, 
4.80952380952381, 4.14285714285714, 4.28571428571429, 4.47619047619048, 
3.83333333333333, 4.16666666666667, 4.23809523809524, 3.85714285714286, 
3.61904761904762, 4.85714285714286, 4.5952380952381, 4.88095238095238, 
3.78571428571429, 4.11904761904762, 4.21428571428571, 4.14285714285714, 
4.11904761904762, 4.19047619047619, 3.5, 3.61904761904762, 4.28571428571429, 
3.85714285714286, 4.4047619047619, 3.69047619047619, 4.23809523809524, 
4.42857142857143, 3.83333333333333, 3.4047619047619, 3.5952380952381, 
3.88095238095238, 3.83333333333333, 3.95238095238095, 4.5952380952381, 
3.71428571428571, 4.78571428571429, 4, 4, 3.73809523809524, 3.47619047619048, 
2.07142857142857, 3.9047619047619, 4.92857142857143, 4.4047619047619, 
4.30952380952381, 4.4047619047619, 4.30952380952381, 4.21428571428571, 
4.07142857142857, 3.5952380952381, 4.26190476190476, 4.30952380952381, 
4, 4.45238095238095, 4.54761904761905, 3.95238095238095, 4.26190476190476, 
4.23809523809524, 3.76190476190476, 4.85714285714286, 3.30952380952381, 
4.02380952380952)), row.names = c("PST1", "PST2", "PST3", "PST4", 
"PST5", "PST6", "PST7", "PST8", "PST9", "PST10", "PST11", "PST12", 
"PST13", "PST14", "PST15", "PST16", "PST17", "PST18", "PST19", 
"PST20", "PST21", "PST22", "PST23", "PST24", "PST25", "PST26", 
"PST27", "PST28", "PST29", "PST30", "PST31", "PST32", "PST33", 
"PST34", "PST35", "PST36", "PST37", "PST38", "PST39", "PST40", 
"PST41", "PST42", "PST43", "PST44", "PST45", "PST46", "PST47", 
"PST48", "PST49", "PST50", "PST51", "PST52", "PST53", "PST54", 
"PST55", "PST56", "PST57", "PST58", "PST59", "PST60", "PST61", 
"PST62", "PST63", "PST64", "PST65", "PST66", "PST67", "PST68", 
"PST69", "PST70", "PST71", "PST72", "PST73", "PST74", "PST75", 
"PST76", "PST77", "PST78", "PST79", "PST80", "PST81", "PST82", 
"PST83", "PST84", "PST85", "PST86", "PST87", "PST88", "PST89", 
"PST90", "PST91", "PST92", "PST93", "PST94", "PST95", "PST96", 
"PST97", "PST98", "PST99", "PST100", "PST101", "PST102", "PST103", 
"PST104", "PST105", "PST106", "PST107", "PST108", "PST109", "PST110", 
"PST111", "PST112", "PST113", "PST114", "PST115", "PST116", "PST117", 
"PST118", "PST119", "PST120", "PST121", "PST122", "PST123", "PST124", 
"PST125", "PST126", "PST127", "PST128", "PST129", "PST130", "PST131", 
"PST132", "PST133", "PST134", "PST135", "PST136", "PST137", "PST138", 
"PST139", "PST140", "PST141", "PST142", "PST143", "PST144", "PST145", 
"PST146", "PST147", "PST148", "PST149", "PST150", "PST151", "PST152", 
"PST153", "PST154", "PST155", "PST156", "PST157", "PST158", "PST159", 
"PST160", "PST161", "PST162", "PST163", "PST164", "PST165", "PST166", 
"PST167", "PST168", "PST169", "PST170", "PST171", "PST172", "PST173", 
"PST174", "PST175", "PST176", "PST177", "PST178", "PST179", "PST180", 
"PST181", "PST182", "PST183", "PST184", "PST185", "PST186", "PST187", 
"PST188", "PST189", "PST190", "PST191", "PST192", "PST193", "PST194", 
"PST195", "PST196", "PST197", "PST198", "PST199", "PST200", "PST201", 
"PST202", "PST203", "PST204", "PST205", "PST206", "PST207", "PST208", 
"PST209", "PST210", "PST211", "PST212", "PST213", "PST214", "PST215"
), class = "data.frame")

Solution

  • The label are added via a geom_text layer and from inspection of the source code the angle for the labels is hard-coded.

    But you can achieve your desired result by manipulating the returned ggplot object, i.e. in your case the geom_text is the second layer and hence the angle for the labels can be set using dendogram$layers[[2]]$aes_params$angle <- 45.

    library(factoextra)
    
    res.dist <- dist(data, method = "euclidean")
    res.hc <- hclust(d = res.dist, method = "ward.D2")
    dendogram <- fviz_dend(res.hc,
      k = 2, cex = 0.5, k_colors = c("#1B9E77", "#7570B3"),
      color_labels_by_k = FALSE
    )
    #> Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
    #> of ggplot2 3.3.4.
    #> ℹ The deprecated feature was likely used in the factoextra package.
    #>   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
    #> This warning is displayed once every 8 hours.
    #> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    #> generated.
    
    dendogram$layers[[2]]$aes_params$angle <- 45
    
    dendogram