如何调整rect_border以接受多种颜色,就像其他调色板一样?

huangapple go评论102阅读模式
英文:

How to adjust rect_border to accept multiple colours like other colour palettes?

问题

I am unable to pass other color schemes into rect_border but it somehow works when the colour palette lancet is specified. How can I adjust the second plot to work on the smooth_rainbow colours?

请问你需要对第二个图表进行调整以使用smooth_rainbow颜色吗?

英文:

I am unable to pass other color schemes into rect_border but it somehow works when the colour palette lancet is specified. How can I adjust the second plot to work on the smooth_rainbow colours?

See reprex below:

  1. library(factoextra)
  2. library(ggplot2)
  3. library(khroma)
  4. df <- scale(mtcars) # Standardize the data
  5. dist <- dist(df, method = "euclidean") # df = standardized data
  6. hc <- hclust(dist, method = "ward.D2")
  7. p <- fviz_dend(hc, k = 4, # Cut in four groups
  8. cex = 0.6, # label size
  9. k_colors = "lancet",
  10. color_labels_by_k = TRUE, # color labels by groups
  11. rect = TRUE, # Add rectangle around groups
  12. rect_border = "lancet",
  13. rect_fill = TRUE,
  14. rotate = TRUE) +
  15. theme_dark()
  16. #> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
  17. #> "none")` instead.
  18. p$layers[[1]]$data$col

    ]$data$col == "black"] <- "white"

  19. p$layers[[2]]$data$angle <- 0

  20. p

如何调整rect_border以接受多种颜色,就像其他调色板一样?

  1. smooth_rainbow <- khroma::colour("smooth rainbow")
  2. p2 <-
  3. fviz_dend(hc, k = 4, # Cut in four groups
  4. cex = 0.6, # label size
  5. k_colors = smooth_rainbow(n = 4),
  6. color_labels_by_k = TRUE, # color labels by groups
  7. rect = TRUE, # Add rectangle around groups
  8. rect_border = smooth_rainbow(n = 4),
  9. rect_fill = TRUE,
  10. rotate = TRUE) +
  11. ggplot2::theme_dark()
  12. #> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
  13. #> "none")` instead.
  14. #> Error in if (color == "cluster") color <- "default": the condition has length > 1
  15. p2
  16. #> Error in eval(expr, envir, enclos): object 'p2' not found

<sup>Created on 2023-05-07 by the reprex package (v2.0.1)</sup>

  1. &gt; sessionInfo()
  2. R version 4.2.3 (2023-03-15 ucrt)
  3. Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. Running under: Windows 10 x64 (build 19044)
  5. Matrix products: default
  6. locale:
  7. [1] LC_COLLATE=English_South Africa.utf8 LC_CTYPE=English_South Africa.utf8
  8. [3] LC_MONETARY=English_South Africa.utf8 LC_NUMERIC=C
  9. [5] LC_TIME=English_South Africa.utf8
  10. attached base packages:
  11. [1] stats graphics grDevices utils datasets methods base
  12. other attached packages:
  13. [1] khroma_1.10.0 cluster_2.1.4 MTGmeta_0.0.0.9000 factoextra_1.0.7
  14. [5] magrittr_2.0.3 here_1.0.1 forcats_0.5.1 stringr_1.4.0
  15. [9] dplyr_1.0.9 purrr_0.3.5 readr_2.1.2 tidyr_1.2.0
  16. [13] tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2

答案1

得分: 1

以下是翻译好的内容,代码部分不进行翻译:

"This is a bug and it would be great if you could report it to the package maintainer. The error originates from a conditional statement in factoextra:::.rect_dendrogram, which tests if your color argument is color == &quot;cluster&quot;. This only works if your color argument is a vector of length 1. (e.g., passing a palette name works, as you have demonstrated).

When passing a vector of colors, this naturally fails, as R doesn't like to compare vectors of length > 1 with a ==. If you replace the conditional statement, e.g. with all(color == &quot;cluster&quot;), it works.

NB I have copied the adjusted functions for your convenience - there are a few uncommented modifications, in particular adding required factoextra::: to some un-exported functions.

  1. library(factoextra)
  2. library(ggplot2)
  3. library(khroma)
  4. ##adjusted functions
  5. fviz_dend2 &lt;-
  6. function (x, k = NULL, h = NULL, k_colors = NULL, palette = NULL,
  7. show_labels = TRUE, color_labels_by_k = TRUE, label_cols = NULL,
  8. labels_track_height = NULL, repel = FALSE, lwd = 0.7, type = c(&quot;rectangle&quot;,
  9. &quot;circular&quot;, &quot;phylogenic&quot;), phylo_layout = &quot;layout.auto&quot;,
  10. rect = FALSE, rect_border = &quot;gray&quot;, rect_lty = 2, rect_fill = FALSE,
  11. lower_rect, horiz = FALSE, cex = 0.8, main = &quot;Cluster Dendrogram&quot;,
  12. xlab = &quot;&quot;, ylab = &quot;Height&quot;, sub = NULL, ggtheme = theme_classic(),
  13. ...) {
  14. ...
  15. }
  16. rect_dendrogram &lt;- function(dend, k = NULL, h = NULL, k_colors = NULL, palette = NULL,
  17. rect_fill = FALSE, rect_lty = 2, lower_rect = -1.5, ...) {
  18. ...
  19. }
  20. fviz_dend2(hc, k = 4, # Cut in four groups
  21. cex = 0.6, # label size
  22. k_colors = smooth_rainbow(4),
  23. # color_labels_by_k = TRUE, # color labels by groups
  24. rect = TRUE, # Add rectangle around groups
  25. rect_border = smooth_rainbow(4),
  26. rect_fill = TRUE,
  27. rotate = TRUE) +
  28. ggplot2::theme_dark()
  29. #&gt; Warning: The `&lt;scale&gt;` argument of `guides()` cannot be `FALSE`. Use &quot;none&quot; instead as
  30. #&gt; of ggplot2 3.3.4.
  31. #&gt; ℹ The deprecated feature was likely used in the factoextra package.
  32. #&gt; Please report the issue at &lt;https://github.com/kassambara/factoextra/issues&gt;.

如何调整rect_border以接受多种颜色,就像其他调色板一样?(创建于2023年05月07日,使用 reprex v2.0.2

英文:

This is a bug and it would be great if you could report it to the package maintainer. The error originates from a conditional statement in factoextra:::.rect_dendrogram, which tests if your color argument is color == &quot;cluster&quot;. This only works if your color argument is a vector of length 1. (e.g., passing a palette name works, as you have demonstrated).

When passing a vector of colors, this naturally fails, as R doesn't like to compare vectors of length > 1 with a ==. If you replace the conditional statement, e.g. with all(color == &quot;cluster&quot;), it works.

NB I have copied the adjusted functions for your convenience - there are a few uncommented modifications, in particular adding required factoextra::: to some un-exported functions.

  1. library(factoextra)
  2. library(ggplot2)
  3. library(khroma)
  4. ##adjusted functions
  5. fviz_dend2 &lt;-
  6. function (x, k = NULL, h = NULL, k_colors = NULL, palette = NULL,
  7. show_labels = TRUE, color_labels_by_k = TRUE, label_cols = NULL,
  8. labels_track_height = NULL, repel = FALSE, lwd = 0.7, type = c(&quot;rectangle&quot;,
  9. &quot;circular&quot;, &quot;phylogenic&quot;), phylo_layout = &quot;layout.auto&quot;,
  10. rect = FALSE, rect_border = &quot;gray&quot;, rect_lty = 2, rect_fill = FALSE,
  11. lower_rect, horiz = FALSE, cex = 0.8, main = &quot;Cluster Dendrogram&quot;,
  12. xlab = &quot;&quot;, ylab = &quot;Height&quot;, sub = NULL, ggtheme = theme_classic(),
  13. ...) {
  14. if (missing(k_colors) &amp; !is.null(palette)) {
  15. k_colors &lt;- palette
  16. palette &lt;- NULL
  17. }
  18. if (!color_labels_by_k &amp; is.null(label_cols))
  19. label_cols &lt;- &quot;black&quot;
  20. type &lt;- match.arg(type)
  21. circular &lt;- type == &quot;circular&quot;
  22. phylogenic &lt;- type == &quot;phylogenic&quot;
  23. rectangle &lt;- type == &quot;rectangle&quot;
  24. if (inherits(x, &quot;HCPC&quot;)) {
  25. k &lt;- length(unique(x$data.clust$clust))
  26. x &lt;- x$call$t$tree
  27. }
  28. if (inherits(x, &quot;hcut&quot;)) {
  29. k &lt;- x$nbclust
  30. dend &lt;- as.dendrogram(x)
  31. method &lt;- x$method
  32. }
  33. else if (inherits(x, &quot;hkmeans&quot;)) {
  34. k &lt;- length(unique(x$cluster))
  35. dend &lt;- as.dendrogram(x$hclust)
  36. method &lt;- x$hclust$method
  37. }
  38. else if (inherits(x, c(&quot;hclust&quot;, &quot;agnes&quot;, &quot;diana&quot;))) {
  39. dend &lt;- as.dendrogram(x)
  40. method &lt;- x$method
  41. }
  42. else if (inherits(x, &quot;dendrogram&quot;)) {
  43. dend &lt;- x
  44. method &lt;- &quot;&quot;
  45. }
  46. else stop(&quot;Can&#39;t handle an object of class &quot;, paste(class(x),
  47. collapse = &quot;, &quot;))
  48. if (is.null(method))
  49. method &lt;- &quot;&quot;
  50. else if (is.na(method))
  51. method &lt;- &quot;&quot;
  52. if (is.null(sub) &amp; method != &quot;&quot;)
  53. sub = paste0(&quot;Method: &quot;, method)
  54. if (!is.null(dendextend::labels_cex(dend)))
  55. cex &lt;- dendextend::labels_cex(dend)
  56. dend &lt;- dendextend::set(dend, &quot;labels_cex&quot;, cex)
  57. dend &lt;- dendextend::set(dend, &quot;branches_lwd&quot;, lwd)
  58. k &lt;- factoextra:::.get_k(dend, k, h)
  59. if (!is.null(k)) {
  60. if (ggpubr:::.is_col_palette(k_colors))
  61. k_colors &lt;- ggpubr:::.get_pal(k_colors, k = k)
  62. else if (is.null(k_colors))
  63. k_colors &lt;- ggpubr:::.get_pal(&quot;default&quot;, k = k)
  64. dend &lt;- dendextend::set(dend, what = &quot;branches_k_color&quot;,
  65. k = k, value = k_colors)
  66. if (color_labels_by_k)
  67. dend &lt;- dendextend::set(dend, &quot;labels_col&quot;, k = k,
  68. value = k_colors)
  69. }
  70. if (!is.null(label_cols)) {
  71. dend &lt;- dendextend::set(dend, &quot;labels_col&quot;, label_cols)
  72. }
  73. leaflab &lt;- ifelse(show_labels, &quot;perpendicular&quot;, &quot;none&quot;)
  74. if (xlab == &quot;&quot;)
  75. xlab &lt;- NULL
  76. if (ylab == &quot;&quot;)
  77. ylab &lt;- NULL
  78. max_height &lt;- max(dendextend::get_branches_heights(dend))
  79. if (missing(labels_track_height))
  80. labels_track_height &lt;- max_height/8
  81. if (max_height &lt; 1)
  82. offset_labels &lt;- -max_height/100
  83. else offset_labels &lt;- -0.1
  84. if (rectangle | circular) {
  85. p &lt;- factoextra:::.ggplot_dend(dend, type = &quot;rectangle&quot;, offset_labels = offset_labels,
  86. nodes = FALSE, ggtheme = ggtheme, horiz = horiz,
  87. circular = circular, palette = palette, labels = show_labels,
  88. label_cols = label_cols, labels_track_height = labels_track_height,
  89. ...)
  90. if (!circular)
  91. p &lt;- p + labs(title = main, x = xlab, y = ylab)
  92. }
  93. else if (phylogenic) {
  94. p &lt;- .phylogenic_tree(dend, labels = show_labels, label_cols = label_cols,
  95. palette = palette, repel = repel, ggtheme = ggtheme,
  96. phylo_layout = phylo_layout, ...)
  97. }
  98. if (circular | phylogenic | is.null(k))
  99. rect &lt;- FALSE
  100. if (rect_fill &amp; missing(rect_lty))
  101. rect_lty = &quot;blank&quot;
  102. if (missing(lower_rect))
  103. lower_rect = -(labels_track_height + 0.5)
  104. if (rect) {
  105. p &lt;- p + rect_dendrogram(dend, k = k, palette = rect_border,
  106. rect_fill = rect_fill, rect_lty = rect_lty, size = lwd,
  107. lower_rect = lower_rect)
  108. }
  109. attr(p, &quot;dendrogram&quot;) &lt;- dend
  110. structure(p, class = c(class(p), &quot;fviz_dend&quot;))
  111. return(p)
  112. }
  113. rect_dendrogram &lt;- function(dend, k = NULL, h = NULL, k_colors = NULL, palette = NULL,
  114. rect_fill = FALSE, rect_lty = 2, lower_rect = -1.5, ...) {
  115. if (missing(k_colors) &amp; !is.null(palette))
  116. k_colors &lt;- palette
  117. prop_k_height &lt;- 0.5
  118. if (!dendextend::is.dendrogram(dend))
  119. stop(&quot;x is not a dendrogram object.&quot;)
  120. k &lt;- factoextra:::.get_k(dend, k, h)
  121. tree_heights &lt;- dendextend::heights_per_k.dendrogram(dend)[-1]
  122. tree_order &lt;- stats::order.dendrogram(dend)
  123. if (is.null(k))
  124. stop(&quot;specify k&quot;)
  125. if (k &lt; 2) {
  126. stop(gettextf(&quot;k must be between 2 and %d&quot;, length(tree_heights)),
  127. domain = NA)
  128. }
  129. cluster &lt;- dendextend::cutree(dend, k = k)
  130. clustab &lt;- table(cluster)[unique(cluster[tree_order])]
  131. m &lt;- c(0, cumsum(clustab))
  132. which &lt;- 1L:k
  133. xleft &lt;- ybottom &lt;- xright &lt;- ytop &lt;- list()
  134. for (n in seq_along(which)) {
  135. next_k_height &lt;- tree_heights[names(tree_heights) ==
  136. k + 1]
  137. if (length(next_k_height) == 0) {
  138. next_k_height &lt;- 0
  139. prop_k_height &lt;- 1
  140. }
  141. xleft[[n]] = m[which[n]] + 0.66
  142. ybottom[[n]] = lower_rect
  143. xright[[n]] = m[which[n] + 1] + 0.33
  144. ytop[[n]] &lt;- tree_heights[names(tree_heights) == k] *
  145. prop_k_height + next_k_height * (1 - prop_k_height)
  146. }
  147. df &lt;- data.frame(xmin = unlist(xleft), ymin = unlist(ybottom),
  148. xmax = unlist(xright), ymax = unlist(ytop), stringsAsFactors = TRUE)
  149. color &lt;- k_colors
  150. if (all(color == &quot;cluster&quot;))
  151. color &lt;- &quot;default&quot;
  152. if (ggpubr:::.is_col_palette(color))
  153. color &lt;- ggpubr:::.get_pal(color, k = k)
  154. else if (length(color) &gt; 1 &amp; length(color) &lt; k) {
  155. color &lt;- rep(color, k)[1:k]
  156. }
  157. if (rect_fill) {
  158. fill &lt;- color
  159. alpha &lt;- 0.2
  160. }
  161. else {
  162. fill &lt;- &quot;transparent&quot;
  163. alpha &lt;- 0
  164. }
  165. df$color &lt;- color
  166. df$cluster &lt;- as.factor(paste0(&quot;c&quot;, 1:k))
  167. ggpubr::geom_exec(geom_rect, data = df, xmin = &quot;xmin&quot;, ymin = &quot;ymin&quot;,
  168. xmax = &quot;xmax&quot;, ymax = &quot;ymax&quot;, fill = fill, color = color,
  169. linetype = rect_lty, alpha = alpha, ...)
  170. }
  171. fviz_dend2(hc, k = 4, # Cut in four groups
  172. cex = 0.6, # label size
  173. k_colors = smooth_rainbow(4),
  174. # color_labels_by_k = TRUE, # color labels by groups
  175. rect = TRUE, # Add rectangle around groups
  176. rect_border = smooth_rainbow(4),
  177. rect_fill = TRUE,
  178. rotate = TRUE) +
  179. ggplot2::theme_dark()
  180. #&gt; Warning: The `&lt;scale&gt;` argument of `guides()` cannot be `FALSE`. Use &quot;none&quot; instead as
  181. #&gt; of ggplot2 3.3.4.
  182. #&gt; ℹ The deprecated feature was likely used in the factoextra package.
  183. #&gt; Please report the issue at &lt;https://github.com/kassambara/factoextra/issues&gt;.

如何调整rect_border以接受多种颜色,就像其他调色板一样?<!-- -->

<sup>Created on 2023-05-07 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月7日 19:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定