簡體   English   中英

如何在 R 的樹狀圖中旋轉 ylab 標簽?

[英]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)

您好,r 的新手,我的問題是;

  1. 我想要順時針方向的樹狀圖如下 plot,如何在樹狀圖中制作水平 ylab 標簽
  2. 如何減少使用 mtcars 數據的軸刻度和軸標簽之間的空間

在此處輸入圖像描述

按照我上面的問題,我找到了在樹狀圖中旋轉 ylab 標簽的代碼。 在這里發帖,可能對其他人有用。

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())

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM