高图堆积列

时间:2019-10-22 11:57:41

标签: r r-highcharter

我有以下图形:

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”复制了图例。然后,我们在图例中有两次Type A,在图例中有两次Type B,依此类推。如何堆叠不同的组,并且每个组只有一个变量?

我添加图片:

grouped stacked graph

编辑:我添加了一些数据

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)中的列。

1 个答案:

答案 0 :(得分:1)

第二次添加系列“ 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"))

enter image description here

UPD

如果系列/类别的数量事先未知,我们仍然可以通过定义带有所有选项的系列列表并将此列表传递给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)

enter image description here