简体   繁体   中英

How to rotate ylab labels in dendrogram in R?

df <- scale(mtcars) # Standardize the data

library("factoextra")
library("cluster")

dist <- dist(df, method = "euclidean") # df = standardized data
hc <- hclust(dist, method = "ward.D2")

fviz_dend(hc, k = 4, # Cut in four groups
          cex = 0.6, # label size
          k_colors = "jco",
          color_labels_by_k = TRUE, # color labels by groups
          rect = TRUE, # Add rectangle around groups
          rect_border = "jco",
          rect_fill = TRUE,
          rotate = TRUE)

Hello, New to r, my questions are;

  1. I want dendrogram in clockwise direction as below plot, how can I make horizontal ylab labels in dendrogram
  2. How can I reduce space between axis ticks and axis labels I have used mtcars data

在此处输入图像描述

Following my above question, I have found code to rotate ylab labels in dendrogram. Posting here, it might be useful to others.

library("ggdendro", "dendextend")
ggdendrogram(hc) + theme_minimal(16) +
  coord_flip() + 
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())

library(dendextend)
hc %>% 
  as.dendrogram %>%
  set("branches_k_color", k = 3) %>% 
  set("branches_lwd", 1.2) %>%
  as.ggdend( ) %>%
  ggplot(horiz=TRUE, 
         offset_labels = -2.8 ) + 
  theme_minimal(16) +
  labs(x = "Y", 
       y = "X") +
  scale_y_continuous(position = "left") + 
  theme(axis.text.y = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM