[英]Rotating labels within a dendrogram object and changing the default dendrogram colour (R)
I am hoping to accomplish two things.我希望完成两件事。
I want to be able to rotate the labels.我希望能够旋转标签。 The fviz_dend
function is compatible with ggplot2
theme
function but I am not entirely sure how to accomplish it. fviz_dend
function 与ggplot2
theme
function 兼容,但我不完全确定如何实现它。
I would like to change the line colour of the dendrogram - specifically those that do not fall within the clustering.我想更改树状图的线条颜色 - 特别是那些不属于聚类的线条颜色。 I would like to change the black to white.我想把黑色改成白色。
Any idea how to accomplish it on this 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.
Created on 2022-03-13 by the reprex package (v2.0.0)由reprex package (v2.0.0) 创建于 2022-03-13
From looking at the docs, I don't see a way to do this directly, but since this is a ggplot object, you can rewrite the layer data after creating the object.通过查看文档,我没有看到直接执行此操作的方法,但由于这是一个 ggplot object,您可以在创建 object 后重写图层数据。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.