R Highcharter Errorbar 系列位置闪避

R Highcharter Errorbar Series Position Dodge

我无法让误差线正确覆盖在我的分组柱状图上。在 ggplot 中,我会使用 position = position_dodge(),但我无法在 highcharts 的错误栏文档中找到类似的内容。

这是一个 MWE:

df <- data.frame(Gender = c("Male", "Male", "Female", "Female"),
                 ShareType = rep(c("Long", "Short"),2),
                 InvestedPerAccount = c(10,9,7,8),
                 lower = c(8,7,6,7),
                 upper = c(11.5,10,9,8.8))



highchart() %>%
  hc_add_series(df, "column",hcaes(x = ShareType, y = InvestedPerAccount, group = Gender),
                tooltip = list(enabled = TRUE,pointFormat = '${point.y}')) %>%
  hc_add_series(df, 'errorbar', hcaes(x = ShareType, low = lower, high = upper, group = Gender, grouping = FALSE)) %>%
  hc_xAxis(categories = df$ShareType, title = list(text = "Share Type")) %>% 
  hc_colors(c("pink","lightblue"))

我不认为这是它应该的工作方式,但在您找到 函数之前,这可能是一种解决方法。

highchart() %>%
  hc_add_series(df, "column",hcaes(x = ShareType, y = InvestedPerAccount, group = Gender),
                tooltip = list(enabled = TRUE,pointFormat = '${point.y}')) %>%
  hc_add_series(df, "errorbar", stemWidth = 1,  whiskerLength = 10, grouping = FALSE,
                centerInCategory = TRUE, groupPadding = .68,
                hcaes(x = ShareType, low = lower, high = upper, group = Gender)) %>%
  hc_xAxis(categories = df$ShareType, title = list(text = "Share Type")) %>% 
  hc_colors(c("pink","lightblue"))

@tamtam 的答案有效,但您需要将 groupPadding 设置为一个奇怪的常数值,在本例中为 0.68。

真正的答案很简单——默认情况下,errorbar 系列与之前的系列相连。所以你的系列的顺序应该这样定义:

1. column 2. errorbar 3. column 4. errorbar

显示正确顺序的演示:https://jsfiddle.net/BlackLabel/rmhw6f9y/

但 R 包装器按以下顺序添加它们:

1. column 2. column 3. errorbar 4. errorbar

演示显示顺序错误:https://jsfiddle.net/BlackLabel/bw0p68f3/


因为 4. errorbar 没有连接到列(3. 不是列系列),error bars 分组不正确。

这个解决方案很简单 - 你需要将 3. 与 1. 和 4. 与 2 连接起来。为此,你可以使用 column.iderrorbar.linkedTo 属性:

https://api.highcharts.com/highcharts/series.column.id https://api.highcharts.com/highcharts/series.errorbar.linkedTo

我不知道如何将它们添加到图表的选项中 (hc_add_series),但我知道如何使用 JavaScript:

更新系列
hc_chart(events = list(load = JS("function () {
    this.series[0].update({
      id: 'firstColumnSeries'
    }, false);
    this.series[1].update({
      id: 'secondColumnSeries'
    }, false);
    this.series[2].update({
      linkedTo: 'secondColumnSeries'
    }, false);
    this.series[3].update({
      linkedTo: 'firstColumnSeries'
    });
  }"))) %>%

这是完整的代码:

library('highcharter')

df <- data.frame(Gender = c("Male", "Male", "Female", "Female"),
                 ShareType = rep(c("Long", "Short"),2),
                 InvestedPerAccount = c(10,9,7,8),
                 lower = c(8,7,6,7),
                 upper = c(11.5,10,9,8.8))


highchart() %>%
  hc_chart(events = list(load = JS("function () {
    this.series[0].update({
      id: 'firstColumnSeries'
    }, false);
    this.series[1].update({
      id: 'secondColumnSeries'
    }, false);
    this.series[2].update({
      linkedTo: 'secondColumnSeries'
    }, false);
    this.series[3].update({
      linkedTo: 'firstColumnSeries'
    });
  }"))) %>%
  hc_add_series(df, "column",hcaes(x = ShareType, y = InvestedPerAccount, group = Gender),
                tooltip = list(enabled = TRUE,pointFormat = '${point.y}')) %>%
  hc_add_series(df, 'errorbar', hcaes(x = ShareType, low = lower, high = upper, group = Gender, grouping = FALSE)) %>%
  hc_xAxis(categories = df$ShareType, title = list(text = "Share Type")) %>% 
  hc_colors(c("pink","lightblue"))

ps。您可能希望将 secondColumnSeriesfirstColumnSeries 交换,具体取决于您要放置错误栏的哪一侧。

一个解决方案是在第一组系列中使用 id 参数,然后在错误栏系列中使用 linkedTo 参数。由于 "errobar" 类型系列默认链接到上一个。

example_dat <- tibble(Type = c("Human", "High-Elf", "Orc"), 
       key = c("World1", "World2", "World3")) %>% 
  expand.grid() %>% 
  mutate(mean = runif(9, 5, 7)) %>% 
  mutate(sd = runif(9, 0, 2)) 


hchart(
  example_dat, 
  "column",
  hcaes(x = key, y = mean, group = Type),
  id = c("a", "b", "c")
  ) %>%
  
  hc_add_series(
    example_dat,
    "errorbar", 
    hcaes(y = mean, x = key, low = mean - sd, high = mean + sd, group = Type),
    linkedTo = c("a", "b", "c"),
    enableMouseTracking = TRUE,
    showInLegend = FALSE
    ) %>% 
  
  hc_plotOptions(
    errorbar = list(
      color = "black", 
      # whiskerLength = 1,
      stemWidth = 1
    ) 
  )