英文:
Grouped drilldown series names with highcharter package in R
问题
Sure, here is the translated portion of your text:
我想使用R中的highcharter
包来实现一个分组的下钻柱状图。我已经找到了一些简洁的语法来在下钻中保持分组,但当您点击图例名称时,默认为"Series 1"和"Series 2"。我希望确保图例名称在整个过程中保持一致... 有关如何实现这一点的任何想法吗?
# 载入库
library(dplyr)
library(purrr)
library(tibble)
library(highcharter)
# 示例数据
data <- as_tibble(datasets::HairEyeColor) %>%
rename_all(tolower)
# 拆分为列和下钻数据集
column <- data %>%
group_by(sex, hair) %>%
summarize(n = sum(n),
# 设置列ID以匹配下钻ID
id = paste0(hair, "-", sex))
drilldown <- data %>%
group_nest(sex, hair) %>%
mutate(
# 设置下钻ID以匹配列ID
id = paste0(hair, "-", sex),
type = "column",
data = data |>
map(mutate, name = eye, y = n),
data = data |>
map(list_parse)
)
# 创建highcharts图
column |
hchart(
"column",
hcaes(x = hair, y = n, name = hair, drilldown = id, group = sex)
) |
hc_drilldown(
activeAxisLabelStyle = list(textDecoration = "none"),
allowPointDrilldown = FALSE,
series = list_parse(drilldown)
) |
hc_yAxis(
title = ""
) |
hc_xAxis(
title = ""
) #|
# 尝试修改图例标签
# hc_legend(
# labelFormat = "{sex}"
# )
我还没有找到成功实现的示例,但这里有一个具有相同功能的JSfiddle示例。当您点击时,您会注意到图例标签从2010/2014切换到Series1/Series2... 尝试在下钻中保持顶级图例标签持续存在正在变得具有挑战性...
英文:
I'd like to implement a grouped drilldown column chart using the highcharter
package in R. I've figured out some clean syntax for maintaining groupings in the drilldowns, but when you click through the legend names default to "Series 1" and "Series 2". I'd like to make sure legend names are consistent throughout... Any ideas on how I can achieve that?
# load libraries
library(dplyr)
library(purrr)
library(tibble)
library(highcharter)
# example data
data <- as_tibble(datasets::HairEyeColor) |>
rename_all(tolower)
# split into column and drilldown datasets
column <- data |>
group_by(sex, hair) |>
summarize(n = sum(n),
# set column id to match drilldown id
id = paste0(hair, "-", sex))
drilldown <- data |>
group_nest(sex, hair) |>
mutate(
# set drilldown id to match column id
id = paste0(hair, "-", sex),
type = "column",
data = data |> map(mutate, name = eye, y = n),
data = data |> map(list_parse)
)
# create highcharts plot
column |>
hchart(
"column",
hcaes(x = hair, y = n, name = hair, drilldown = id, group = sex)
) |>
hc_drilldown(
activeAxisLabelStyle = list(textDecoration = "none"),
allowPointDrilldown = FALSE,
series = list_parse(drilldown)
) |>
hc_yAxis(
title = ""
) |>
hc_xAxis(
title = ""
) #|>
# trying to alter legend labels
# hc_legend(
# labelFormat = "{sex}"
# )
I haven’t found an example of it successfully implemented but here’s a JSfiddle with the same functionality. You’ll notice when you click though, the legend label switches from 2010/2014 to Series1/Series2… trying to get the top-level legend labels to persist in the drilldown is proving to be challenging…
答案1
得分: 1
Here is the translated content:
这是您想要的吗?我正在使用 R < 4.0,所以我不得不用 '%>%' 替换 |>
。
您只需将名称变量添加到您的下钻表。
drilldown <- data %>%
group_nest(sex, hair) %>%
mutate(
# 将下钻 id 设置为匹配的列 id
id = paste0(hair, "-", sex),
name = sex, # 添加 'series.name' 到下钻系列
type = "column",
data = data %>% map(mutate, name = eye, y = n),
data = data %>% map(list_parse)
)
英文:
Is this what you're after? I'm using R < 4.0 so I had to replace |>
with '%>%'.
You just need to add the name var to your drilldown table.
drilldown <- data %>%
group_nest(sex, hair) %>%
mutate(
# set drilldown id to match column id
id = paste0(hair, "-", sex),
name = sex, # add 'series.name' to drilldown series
type = "column",
data = data %>% map(mutate, name = eye, y = n),
data = data %>% map(list_parse)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论