英文:
Creating a two-level Treemap in Highcharter R with data_to_hierarchical
问题
我正在尝试使用highcharter
包中的data_to_hierarchical()
函数在R中创建一个两级Treemap。它需要有两个级别 - 最顶层是Level1
变量,然后可以点击查看第二个Level2
变量:
如何使它成为两级的?我已经进行了很多搜索,但关于在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:
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 <- 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")
答案1
得分: 0
我通过向我的 hchart()
函数添加allowTraversingTree
、levelIsConstant
和 levels
参数来解决了这个问题:
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))
)
)
顶层:
第二层:
英文:
I solved this by adding the allowTraversingTree
, levelIsConstant
, and levels
arguments to my hchart()
function:
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))
)
)
Top level:
Second level:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论