rCharts (highcharts) 中的自定义图例

Custom legend in rCharts (highcharts)

我的数据框如下所示:

## 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

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

如有任何帮助,我们将不胜感激。

我是这样解决的。

每个 'category' 都被分成一个单独的系列,并使用 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

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

免责声明:我知道问题是 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.parse3类似于toJSONArray2.

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

结果将是: