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

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

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的信息相互冲突且存在已弃用的函数,我无法找到答案。

可重现的代码:

library(tidyverse)
library(highcharter)

set.seed(110)

ex <- data.frame(
  Level1 = rep(paste0("Country", seq(1:5)), each = 5),
  Level2 = rep(paste0("Sector", seq(1:5)), 5),
  Percentage = runif(25, 0, 1)
)

ex %>% 
  data_to_hierarchical(c(Level1, Level2), Percentage) %>%
  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:


library(tidyverse)
library(highcharter)


set.seed(110)

ex &lt;- data.frame(
  Level1 = rep(paste0(&quot;Country&quot;, seq(1:5)), each = 5),
  Level2 = rep(paste0(&quot;Sector&quot;, seq(1:5)), 5),
  Percentage = runif(25, 0, 1)
)

ex %&gt;% 
  data_to_hierarchical(c(Level1, Level2), Percentage) %&gt;%
  hchart(type = &quot;treemap&quot;)

答案1

得分: 0

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

library(tidyverse)
library(highcharter)

set.seed(110)

ex <- data.frame(
  Level1 = rep(paste0("Country", seq(1:5)), each = 5),
  Level2 = rep(paste0("Sector", seq(1:5)), 5),
  Percentage = runif(25, 0, 1)
)

ex %>% 
  data_to_hierarchical(c(Level1, Level2), Percentage) %>%
  hchart(type = "treemap",
         allowTraversingTree = T,
         levelIsConstant = F,
         levels = list(
           list(level = 1, dataLabels = list(enabled = TRUE, 
                                             format = "{point.name}<br>{point.value}%"), borderColor = "black", borderWidth = 2),
           list(level = 2, dataLabels = list(enabled = FALSE))
         )
  )

顶层:

使用 `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:


library(tidyverse)
library(highcharter)


set.seed(110)

ex &lt;- data.frame(
  Level1 = rep(paste0(&quot;Country&quot;, seq(1:5)), each = 5),
  Level2 = rep(paste0(&quot;Sector&quot;, seq(1:5)), 5),
  Percentage = runif(25, 0, 1)
)

ex %&gt;% 
  data_to_hierarchical(c(Level1, Level2), Percentage) %&gt;%
  hchart(type = &quot;treemap&quot;,
         allowTraversingTree = T,
         levelIsConstant = F,
         levels = list(
           list(level = 1, dataLabels = list(enabled = TRUE, 
                                             format = &quot;{point.name}&lt;br&gt;
                                                      {point.value}%&quot;), borderColor = &quot;black&quot;, borderWidth = 2),
           list(level = 2, dataLabels = list(enabled = FALSE))
         )
         )

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:

确定