使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

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

Creating a two-level Treemap in Highcharter R with data_to_hierarchical

问题

我正在尝试使用highcharter包中的data_to_hierarchical()函数在R中创建一个两级Treemap。它需要有两个级别 - 最顶层是Level1变量,然后可以点击查看第二个Level2变量:

使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

如何使它成为两级的?我已经进行了很多搜索,但关于在R中制作高级图表Treemap的信息相互冲突且存在已弃用的函数,我无法找到答案。

可重现的代码:

  1. library(tidyverse)
  2. library(highcharter)
  3. set.seed(110)
  4. ex <- data.frame(
  5. Level1 = rep(paste0("Country", seq(1:5)), each = 5),
  6. Level2 = rep(paste0("Sector", seq(1:5)), 5),
  7. Percentage = runif(25, 0, 1)
  8. )
  9. ex %>%
  10. data_to_hierarchical(c(Level1, Level2), Percentage) %>%
  11. hchart(type = "treemap")
英文:

I'm attempting to create a two-level Treemap in R using data_to_hierarchical() from the highcharter package. It needs to have two levels - the topmost being the Level1 variable, which can then be clicked to view the second Level2 variable:

使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

How can I make it two-level? I've done a lot of googling, but there's a lot of conflicting information and depreciated functions regarding making highcharts treemaps in R, and I haven't been able to find an answer.

Reproducible code:

  1. library(tidyverse)
  2. library(highcharter)
  3. set.seed(110)
  4. ex &lt;- data.frame(
  5. Level1 = rep(paste0(&quot;Country&quot;, seq(1:5)), each = 5),
  6. Level2 = rep(paste0(&quot;Sector&quot;, seq(1:5)), 5),
  7. Percentage = runif(25, 0, 1)
  8. )
  9. ex %&gt;%
  10. data_to_hierarchical(c(Level1, Level2), Percentage) %&gt;%
  11. hchart(type = &quot;treemap&quot;)

答案1

得分: 0

我通过向我的 hchart() 函数添加allowTraversingTreelevelIsConstantlevels 参数来解决了这个问题:

  1. library(tidyverse)
  2. library(highcharter)
  3. set.seed(110)
  4. ex <- data.frame(
  5. Level1 = rep(paste0("Country", seq(1:5)), each = 5),
  6. Level2 = rep(paste0("Sector", seq(1:5)), 5),
  7. Percentage = runif(25, 0, 1)
  8. )
  9. ex %>%
  10. data_to_hierarchical(c(Level1, Level2), Percentage) %>%
  11. hchart(type = "treemap",
  12. allowTraversingTree = T,
  13. levelIsConstant = F,
  14. levels = list(
  15. list(level = 1, dataLabels = list(enabled = TRUE,
  16. format = "{point.name}<br>{point.value}%"), borderColor = "black", borderWidth = 2),
  17. list(level = 2, dataLabels = list(enabled = FALSE))
  18. )
  19. )

顶层:

使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

第二层:

使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

英文:

I solved this by adding the allowTraversingTree, levelIsConstant, and levels arguments to my hchart() function:

  1. library(tidyverse)
  2. library(highcharter)
  3. set.seed(110)
  4. ex &lt;- data.frame(
  5. Level1 = rep(paste0(&quot;Country&quot;, seq(1:5)), each = 5),
  6. Level2 = rep(paste0(&quot;Sector&quot;, seq(1:5)), 5),
  7. Percentage = runif(25, 0, 1)
  8. )
  9. ex %&gt;%
  10. data_to_hierarchical(c(Level1, Level2), Percentage) %&gt;%
  11. hchart(type = &quot;treemap&quot;,
  12. allowTraversingTree = T,
  13. levelIsConstant = F,
  14. levels = list(
  15. list(level = 1, dataLabels = list(enabled = TRUE,
  16. format = &quot;{point.name}&lt;br&gt;
  17. {point.value}%&quot;), borderColor = &quot;black&quot;, borderWidth = 2),
  18. list(level = 2, dataLabels = list(enabled = FALSE))
  19. )
  20. )

Top level:

使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

Second level:

使用 `data_to_hierarchical` 在 Highcharter R 中创建一个两级树状图。

huangapple
  • 本文由 发表于 2023年2月16日 09:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466871.html
匿名

发表评论

匿名网友

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

确定