“使用R中的highcharter包分组下钻系列名称”

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

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 &lt;- as_tibble(datasets::HairEyeColor) |&gt;
  rename_all(tolower)

# split into column and drilldown datasets
column &lt;- data |&gt;
  group_by(sex, hair) |&gt;
  summarize(n = sum(n),
            # set column id to match drilldown id
            id = paste0(hair, &quot;-&quot;, sex))

drilldown &lt;- data |&gt;
  group_nest(sex, hair) |&gt;
  mutate(
    # set drilldown id to match column id
    id = paste0(hair, &quot;-&quot;, sex),
    type = &quot;column&quot;,
    data = data |&gt; map(mutate, name = eye, y = n),
    data = data |&gt; map(list_parse)
  )

# create highcharts plot
column |&gt;
  hchart(
    &quot;column&quot;,
    hcaes(x = hair, y = n, name = hair, drilldown = id, group = sex)
  ) |&gt;
  hc_drilldown(
    activeAxisLabelStyle = list(textDecoration = &quot;none&quot;),
    allowPointDrilldown = FALSE,
    series = list_parse(drilldown)
  ) |&gt;
  hc_yAxis(
    title = &quot;&quot;
  ) |&gt;
  hc_xAxis(
    title = &quot;&quot;
  ) #|&gt;
  # trying to alter legend labels
  # hc_legend(
  #   labelFormat = &quot;{sex}&quot;
  # )

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,所以我不得不用 '%>%' 替换 |&gt;

您只需将名称变量添加到您的下钻表。

drilldown &lt;- data %&gt;%
  group_nest(sex, hair) %&gt;%
  mutate(
    # 将下钻 id 设置为匹配的列 id
    id = paste0(hair, &quot;-&quot;, sex),
    name = sex, # 添加 &#39;series.name&#39; 到下钻系列
    type = &quot;column&quot;,
    data = data %&gt;% map(mutate, name = eye, y = n),
    data = data %&gt;% map(list_parse)
  )

“使用R中的highcharter包分组下钻系列名称”

英文:

Is this what you're after? I'm using R < 4.0 so I had to replace |&gt; with '%>%'.

You just need to add the name var to your drilldown table.

drilldown &lt;- data %&gt;%
  group_nest(sex, hair) %&gt;%
  mutate(
    # set drilldown id to match column id
    id = paste0(hair, &quot;-&quot;, sex),
    name = sex, # add &#39;series.name&#39; to drilldown series
    type = &quot;column&quot;,
    data = data %&gt;% map(mutate, name = eye, y = n),
    data = data %&gt;% map(list_parse)
  )

“使用R中的highcharter包分组下钻系列名称”

huangapple
  • 本文由 发表于 2023年4月13日 22:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006819.html
匿名

发表评论

匿名网友

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

确定