【问题标题】:Custom legend in rCharts (highcharts)rCharts 中的自定义图例(highcharts)
【发布时间】:2015-02-06 16:18:18
【问题描述】:

我有一个如下所示的数据框:

## Data
df <- data.frame(label = c("A", "B", "C", "D"), 
                 color = c("red", "red", "blue", "green"), 
                 y = c(10, 11, 12, 13))

“A”和“B”属于同一类别,而“C”和“D”属于不同类别。

我想在图表上添加一个带有类别标签的图例。

## Highchart without Legend

## Basic highchart
h <- rCharts:::Highcharts$new()
h$chart(type = "column")

## Highchart data:
h$series(showInLegend = FALSE, data =  rCharts::toJSONArray2(df[, c("label", "color", "y")], json = FALSE, names = TRUE))

## Highchart options:
h$xAxis(categories = unique(df$label), labels = list(rotation = 0, align = 'center', style = list(fontSize = '12px', fontFamily = 'Verdana, sans-serif')), replace = FALSE)
h$tooltip(formatter = "#! function() {return this.x + ': ' + this.y; } !#")
h$plotOptions(series = list(color = df$color), column = list(grouping = FALSE))
h  # display highchart

我还没有找到任何有意义的方法来解决这个问题。

任何帮助将不胜感激。

【问题讨论】:

    标签: r highcharts rcharts


    【解决方案1】:

    免责声明:我知道问题是 rCharts,我只想添加一个使用 highcharter 包的替代方案。

    就像@Optimus 所说,问题是添加倍数系列。如果您有任意数量的系列(示例中的颜色)并且想要自动添加它,您可以使用 highcharter 允许您使用 hc_add_series_list 函数从数据系列列表中添加多个系列。

    library(highcharter)
    library(dplyr)
    
    df <- data_frame(label = c("A", "B", "C", "D"), 
                     color = c("red", "red", "blue", "green"), 
                     y = c(10, 11, 12, 13),
                     x = c(1:4)-1)
    
    head(df)
    
    series <- df %>% 
      group_by(color) %>% # each serie is from one color
      do(data = list.parse3(.)) %>% 
      ungroup() %>% 
      mutate(name = paste("I'm color", color)) %>% 
      list.parse3()
    

    这里的list.parse3toJSONArray2 类似。

    series[[1]]
    
    $color
    [1] "blue"
    
    $data
    $data[[1]]
    $data[[1]]$label
    [1] "C"
    
    $data[[1]]$color
    [1] "blue"
    
    $data[[1]]$y
    [1] 12
    
    $data[[1]]$x
    [1] 2
    
    $name
    [1] "I'm color blue"
    

    最后:

    highchart() %>% 
      hc_chart(type = "column") %>% 
      hc_add_series_list(series) %>% 
      hc_xAxis(categories = df$label) %>% 
      hc_plotOptions(column = list(grouping = FALSE))
    

    结果将是:

    【讨论】:

    • 我知道你是dev of highcharter,但是,当你使用包的时候,先链接到CRAN Page再到首页。在 meta 上有一些案例,用户将合法答案标记为“过度宣传”,认为用户正在宣传某些软件包。一般用户可能不知道您是软件包的开发者。下次请小心,如果我在这方面犯了错误,请道歉。问候
    • @BhargavRao,完全同意。有道理。事实上,部分地是为了让一些任务更容易(在这种情况下添加多个数据系列)来交流关于其他包的信息。我将编辑链接 CRAN 站点的响应,并在答案末尾移动主页。谢谢!
    • 感谢您,请随时访问R Public 聊天室。这是一个公共聊天,我们帮助新用户学习 R。(昨天新用户使用 highcharter 出现问题,已解决)。
    • @jbkunst 我有一个包含 11 个级别的因子,并且希望在我的柱形图/图例中有 5 个“开启”但其中 6 个“关闭”。灰色,但仍然存在于传说中,因此如果需要,可以打开该系列......这些天有没有更简单的方法可以做到这一点,因为这是大约 1 年前写的?
    • @jbkunst 没有看到这样的选项api.highcharts.com/highcharts/legend.title
    【解决方案2】:

    这就是我解决它的方法。

    每个“类别”都被分成一个单独的系列,并带有一个 x 值以确定其在图表上的位置(highcharts 出于某种原因需要这样做。没有它,图表会堆叠)。

    这是一个有效的示例代码:

    ## Highchart with Legend
    ## Remark: simply switching showInLegend to TRUE will not work
    df$x <- c(0, 1, 2, 3) # add an index to dataframe (starting from 0)
                          # and pass it to h$series data
    h <- rCharts:::Highcharts$new()
    h$chart(type = "column")
    h$series(name = "Category 1 (Red)", color = "red", data = rCharts::toJSONArray2(df[df$color == "red", c("label", "color", "x", "y")], json = FALSE, names = TRUE))
    h$series(name = "Category 2 (Blue)", color = "blue", data = rCharts::toJSONArray2(df[df$color == "blue", c("label", "color", "x", "y")], json = FALSE, names = TRUE))
    h$series(name = "Category 3 (Green)", color = "green", data =  rCharts::toJSONArray2(df[df$color == "green", c("label", "color", "x", "y")], json = FALSE, names = TRUE)) 
    h$xAxis(categories = unique(df$label), labels = list(rotation = 0, align = 'center', style = list(fontSize = '12px', fontFamily = 'Verdana, sans-serif')), replace = FALSE)
    h$tooltip(formatter = "#! function() {return this.x + ': ' + this.y; } !#")
    h$plotOptions(series = list(color = df$color), column = list(grouping = FALSE))
    h # display chart
    

    我希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      相关资源
      最近更新 更多