【问题标题】:R Highcharter Errorbar Series Position DodgeR Highcharter Errorbar 系列位置闪避
【发布时间】:2021-12-18 05:51:30
【问题描述】:

我无法让误差线正确覆盖在我的分组列图上。在 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"))

【问题讨论】:

标签: r ggplot2 highcharts data-visualization r-highcharter


【解决方案1】:

我不认为这是它应该工作的方式,但也许它是一种解决方法,直到你找到 函数。

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"))

【讨论】:

    【解决方案2】:

    @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未连接到column(3.不是column series),errorbars分组错误。

    解决方案很简单 - 您需要将 3. 与 1. 和 4. 与 2. 连接。为此,您可以使用 column.id 和 errorbar.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。您可能希望将 secondColumnSeries 与 firstColumnSeries 交换,具体取决于您要放置错误栏的哪一侧。

    【讨论】:

      【解决方案3】:

      解决方案是在第一组系列中使用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
          ) 
        ) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 2021-08-09
        • 2012-05-16
        • 2021-05-19
        • 1970-01-01
        • 2021-04-21
        • 2020-02-14
        相关资源
        最近更新 更多