首页 > 解决方案 > highcharter:如何使用 hcaes 函数制作分组图

问题描述

下面的图在 highcharter 中制作了一个堆积柱形图。我试图将其设为分组柱形图,但我添加的堆栈字段未能达到预期的结果。

data = data.frame(player = rep(c('Edwin', 'Ahmad', 'Sonia', 'Jessica'), 2),
              games = c(10, 20, 15, 40, 12, 57, 18, 49),
              win_rate = c(.5, .2, .8, .4, .1, .7, .3, .43),
              week = rep(c(1,2), 4))




highcharter::highchart() %>% 
  highcharter::hc_xAxis(categories = data$player) %>% 
  highcharter::hc_plotOptions(column = list(stacking = 'normal'))  %>%
  highcharter::hc_add_series(data = data %>% dplyr::filter(week == 1), type = 'column', highcharter::hcaes(x = player, y = win_rate, stack = week),
                             dataLabels = list(enabled = TRUE, format='{point.games}')) %>% 
  highcharter::hc_add_series(data = data %>% dplyr::filter(week == 2), type = 'column', highcharter::hcaes(x = player, y = win_rate, stack = week),
                             dataLabels = list(enabled = TRUE, format='{point.games}'))

标签: rhighchartsr-highcharter

解决方案


这与您想要的相似吗?

highchart() %>% 
  hc_xAxis(categories = data$player) %>% 
  hc_add_series(data = data %>% filter(week == 1), type = 'bar', hcaes(x = player, y = win_rate, group = week),
                dataLabels = list(enabled = TRUE, format='{point.games}')) %>% 
  hc_add_series(data = data %>% filter(week == 2), type = 'bar', hcaes(x = player, y = win_rate, group = week),
                dataLabels = list(enabled = TRUE, format='{point.games}'))


推荐阅读