我无法将其他配色方案传递到rect_border
中,但当指定调色板lancet
时,它可以工作。如何调整第二个图以使用smooth_rainbow
颜色?
参见以下报告:
library(factoextra)
library(ggplot2)
library(khroma)
df <- scale(mtcars) # Standardize the data
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()
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.
p$layers[[1]]$data$col[p$layers[[1]]$data$col == "black"] <- "white"
p$layers[[2]]$data$angle <- 0
p
smooth_rainbow <- khroma::colour("smooth rainbow")
p2 <-
fviz_dend(hc, k = 4, # Cut in four groups
cex = 0.6, # label size
k_colors = smooth_rainbow(n = 4),
color_labels_by_k = TRUE, # color labels by groups
rect = TRUE, # Add rectangle around groups
rect_border = smooth_rainbow(n = 4),
rect_fill = TRUE,
rotate = TRUE) +
ggplot2::theme_dark()
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.
#> Error in if (color == "cluster") color <- "default": the condition has length > 1
p2
#> Error in eval(expr, envir, enclos): object 'p2' not found
由reprex包(v2.0.1)于2023-05-07创建
> sessionInfo()
R version 4.2.3 (2023-03-15 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_South Africa.utf8 LC_CTYPE=English_South Africa.utf8
[3] LC_MONETARY=English_South Africa.utf8 LC_NUMERIC=C
[5] LC_TIME=English_South Africa.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] khroma_1.10.0 cluster_2.1.4 MTGmeta_0.0.0.9000 factoextra_1.0.7
[5] magrittr_2.0.3 here_1.0.1 forcats_0.5.1 stringr_1.4.0
[9] dplyr_1.0.9 purrr_0.3.5 readr_2.1.2 tidyr_1.2.0
[13] tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2
1条答案
按热度按时间pftdvrlh1#
这是一个bug,如果你能把它报告给软件包维护者就太好了。该错误源于
factoextra:::.rect_dendrogram
中的条件语句,该语句测试颜色参数是否为color == "cluster"
。这仅在color参数是长度为1的向量时有效。(例如,传递一个调色板名称就可以了,正如您所演示的那样)。当传递颜色向量时,这自然会失败,因为R不喜欢将长度> 1的向量与
==
进行比较。如果您替换条件语句,例如all(color == "cluster")
,它可以工作。注意,为了方便起见,我复制了调整后的函数--有一些未注解的修改,特别是向一些未导出的函数添加了所需的
factoextra:::
。创建于2023-05-07带有reprex v2.0.2