在树状图对象内旋转标签并更改默认树状图颜色 (R)

Rotating labels within a dendrogram object and changing the default dendrogram colour (R)

我希望完成两件事。

  1. 我希望能够旋转标签。 fviz_dend 函数与 ggplot2 theme 函数兼容,但我不完全确定如何实现它。

  2. 我想更改树状图的线条颜色 - 特别是那些不属于聚类的线条颜色。我想把黑色改成白色。

知道如何在此 reprex 上完成它吗?

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 = "lancet",
          color_labels_by_k = TRUE, # color labels by groups
          rect = TRUE, # Add rectangle around groups
          rect_border = "lancet",
          rect_fill = TRUE,
          rotate = TRUE) +
ggplot2::theme_dark()
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.

reprex package (v2.0.0)

于 2022-03-13 创建

通过查看文档,我没有看到直接执行此操作的方法,但由于这是一个 ggplot 对象,您可以在创建对象后重写图层数据。

df <- scale(mtcars) # Standardize the data

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

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

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

p$layers[[1]]$data$col[p$layers[[1]]$data$col == "black"] <- "white"
p$layers[[2]]$data$angle <- 0
p