Highchart 堆叠柱

Highchart stacked columns

我有下图:

highchart() %>%
  hc_title(text = "Composition") %>% 
  hc_subtitle(text = "Subtitle") %>% 
  hc_chart(type = "column", polar = F) %>% 
  hc_xAxis(categories = c("A", "B", "C", "D", "E")) %>% 
  hc_add_series(name = "Type A", data = c(5, 3, 4, 7, 2), stack = "A") %>% 
  hc_add_series(name = "Type B", data = c(5, 3, 4, 7, 2), stack = "A") %>% 
  hc_add_series(name = "Type C", data = c(5, 3, 4, 7, 2), stack = "A") %>% 
  hc_add_series(name = "Type D", data = c(5, 3, 4, 7, 2), stack = "A") %>% 
  hc_add_series(name = "Type E", data = c(5, 3, 4, 7, 2), stack = "A") %>% 
  hc_add_series(name = "Type A", data = c(2, 2, 3, 2, 1), stack = "B") %>%
  hc_add_series(name = "Type B", data = c(2, 2, 3, 2, 1), stack = "B") %>%
  hc_add_series(name = "Type C", data = c(2, 2, 3, 2, 1), stack = "B") %>%
  hc_add_series(name = "Type D", data = c(2, 2, 3, 2, 1), stack = "B") %>%
  hc_add_series(name = "Type E", data = c(2, 2, 3, 2, 1), stack = "B") %>%
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal"))

该图正在创建不同的变量,并为每个堆栈 = "A" 和堆栈 = "B" 复制图例。然后,图例中的类型 A 两次,图例中的类型 B 两次,等等。我如何堆叠不同的组并且每个组只有一个变量?

我加一张图:

编辑:我添加了一些数据

df <- structure(list(TYPE = c("TYPE_A", "TYPE_A", "TYPE_A", "TYPE_A", 
"TYPE_A", "TYPE_A", "TYPE_A", "TYPE_A", "TYPE_A", "TYPE_A", "TYPE_A", 
"TYPE_A", "TYPE_A", "TYPE_A", "TYPE_A", "TYPE_B", "TYPE_B", "TYPE_B", 
"TYPE_B", "TYPE_B", "TYPE_B", "TYPE_B", "TYPE_B", "TYPE_B", "TYPE_B", 
"TYPE_B", "TYPE_B", "TYPE_B", "TYPE_B", "TYPE_B"), SUB_TYPE = c("Sub_A", 
"Sub_A", "Sub_A", "Sub_A", "Sub_A", "Sub_B", "Sub_B", "Sub_B", 
"Sub_B", "Sub_B", "Sub_C", "Sub_C", "Sub_C", "Sub_C", "Sub_C", 
"Sub_A", "Sub_A", "Sub_A", "Sub_A", "Sub_A", "Sub_B", "Sub_B", 
"Sub_B", "Sub_B", "Sub_B", "Sub_C", "Sub_C", "Sub_C", "Sub_C", 
"Sub_C"), PERIOD = c("curr", "t0", "t1", "t2", "t3", "curr", 
"t0", "t1", "t2", "t3", "curr", "t0", "t1", "t2", "t3", "curr", 
"t0", "t1", "t2", "t3", "curr", "t0", "t1", "t2", "t3", "curr", 
"t0", "t1", "t2", "t3"), VALUE = c(296.6954, 352.8885, 360.5875, 
375.2185, 389.8869, 1409.0355, 1470.8772, 1537.3365, 1590.0784, 
1650.0942, 115.7838, 117.8871, 122.5989, 133.0147, 140.5065, 
296.6954, 352.8885, 360.5875, 437.319, 382.3504, 1409.0355, 1470.8772, 
1440.1987, 1289.093, 1244.0955, 115.7838, 117.8871, 169.3969, 
113.2784, 98.1415)), .Names = c("TYPE", "SUB_TYPE", "PERIOD", 
"VALUE"), row.names = c(NA, -30L), class = "data.frame")

TYPE 是 X 中的类别,SUB_TYPE 是列中的堆叠值,PERIOD 是每个类别 (TYPE) 中的列。

当您第二次添加系列"Type A"时,即使名称相同,也会被视为不同的系列。我们在这里可以做的是 link 第二个到第一个将它附加到相同的图例条目(参见 API reference)并手动设置相同的颜色。

library(highcharter)

default_colors <- c("#7cb5ec", "#434348", "#90ed7d", "#f7a35c", "#8085e9",
                    "#f15c80", "#e4d354", "#2b908f", "#f45b5b", "#91e8e1")

highchart() %>%
  hc_title(text = "Composition") %>% 
  hc_subtitle(text = "Subtitle") %>% 
  hc_chart(type = "column", polar = F) %>% 
  hc_xAxis(categories = c("A", "B", "C", "D", "E")) %>% 
  hc_add_series(name = "Type A", data = c(5, 3, 4, 7, 2), stack = "A",
                id = "AA", color = default_colors[[1]]) %>% 
  hc_add_series(name = "Type B", data = c(5, 3, 4, 7, 2), stack = "A",
                id = "BA", color = default_colors[[2]]) %>% 
  hc_add_series(name = "Type C", data = c(5, 3, 4, 7, 2), stack = "A",
                id = "CA", color = default_colors[[3]]) %>% 
  hc_add_series(name = "Type D", data = c(5, 3, 4, 7, 2), stack = "A",
                id = "DA", color = default_colors[[4]]) %>% 
  hc_add_series(name = "Type E", data = c(5, 3, 4, 7, 2), stack = "A",
                id = "EA", color = default_colors[[5]]) %>% 
  hc_add_series(name = "Type A", data = c(2, 2, 3, 2, 1), stack = "B",
                linkedTo = "AA", color = default_colors[[1]]) %>%
  hc_add_series(name = "Type B", data = c(2, 2, 3, 2, 1), stack = "B",
                linkedTo = "BA", color = default_colors[[2]]) %>%
  hc_add_series(name = "Type C", data = c(2, 2, 3, 2, 1), stack = "B",
                linkedTo = "CA", color = default_colors[[3]]) %>%
  hc_add_series(name = "Type D", data = c(2, 2, 3, 2, 1), stack = "B",
                linkedTo = "DA", color = default_colors[[4]]) %>%
  hc_add_series(name = "Type E", data = c(2, 2, 3, 2, 1), stack = "B",
                linkedTo = "EA", color = default_colors[[5]]) %>%
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal"))

UPD

如果事先不知道 series/categories 的数量,我们仍然可以通过定义包含所有选项的系列列表并将该列表传递给 hc_add_series_list 来创建绘图。当我们想要将 data.frame 转换为复杂列表时,purrr 包会派上用场。

library(dplyr)
library(purrr)

x_cats <- unique(df$TYPE)

default_colors <- c("#7cb5ec", "#434348", "#90ed7d", "#f7a35c", "#8085e9",
                    "#f15c80", "#e4d354", "#2b908f", "#f45b5b", "#91e8e1")

colors_df <- tibble(SUB_TYPE = unique(df$SUB_TYPE)) %>% 
  mutate(color = default_colors[1:n()])

series <- df %>% 
  left_join(colors_df, by = "SUB_TYPE") %>% 
  group_by(SUB_TYPE, color, PERIOD) %>% 
  group_split() %>% 
  map(~list(
    name = .$SUB_TYPE[[1]],
    stack = .$PERIOD[[1]],
    color = .$color[[1]],
    data = .$VALUE,
    id   = paste(.$SUB_TYPE[[1]], .$PERIOD[[1]], sep = "-"),
    linkedTo = paste(.$SUB_TYPE[[1]], "curr", sep = "-")
  )) %>%
  # remove links of curr to itself
  modify_if(~ .$stack == "curr", ~.[-6])

highchart() %>% 
  hc_chart(type = "column") %>% 
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal")
  ) %>%
  hc_xAxis(categories = unique(df$TYPE)) %>% 
  hc_add_series_list(series)