5

I'm trying to create a stacked bar chart with groupings using Highcharter, and need to create it without using the hchart() function. I have the following code (hchart() portion works).

data <- data.frame(
  building = c("Building A", "Building A", "Building B", "Building B"),
  type = c("Rent", "Owned"),
  measure = c(100, 35, 124, 150),
  measure_target = c(95, 20, 122, 145)
)

# This works
hchart(data, "column", hcaes(x = "building", y = "measure", group = "type")) %>%
  hc_plotOptions(column = list(stacking = "normal"))

# How do we go from the above, to something like this?
highchart() %>%
  hc_xAxis(categories = data$building) %>%
  hc_add_series(type = "column", data = data$measure) %>%
  hc_plotOptions(column = list(stacking = "normal"))

Expected output below. The end goal of this is to add the stacked bars, and follow it with another hc_add_series that would add a series of points in the measure_target column (so comparing actuals vs. targets).

I need something similar to this: enter image description here

Except with two stacked bars and one line for the target, something like:

enter image description here

Bogdan Rau
  • 625
  • 5
  • 17

1 Answers1

4

Something like this?

library(highcharter)
library(dplyr)

data <- data.frame(
  building = c("Building A", "Building A", "Building B", "Building B"),
  type = c("Rent", "Owned"),
  measure = c(100, 35, 124, 150),
  measure_target = c(95, 20, 122, 145)
)

data_lst <- data %>% 
  group_by(type) %>% 
  do(data = list_parse2(.[, c('building', 'measure')])) %>% 
  rename(name = type) %>% 
  mutate(type = 'column') %>% 
  list_parse()


data_lst2 <- data %>% 
  group_by(type) %>% 
  do(data = list_parse2(.[, c('building', 'measure_target')])) %>% 
  rename(name = type) %>% 
  mutate(type = 'scatter') %>% 
  list_parse()


highchart() %>%
  hc_xAxis(categories = data$building) %>%
  hc_add_series_list(data_lst)%>%
  hc_add_series_list(data_lst2)%>%
  hc_plotOptions(series=list(stacking='normal'))

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Close. I'd need the points to not be connected, which I guess you could do with scatter rather than line. However, I can't find list.parse3. What version of highcharter are you using? I'm on v 0.5.0.9999 – Bogdan Rau Oct 10 '17 at 17:41
  • Im using dev version – Pork Chop Oct 10 '17 at 18:32
  • @PorkChop the dev version of 1 year ago :D, list.parseX have been replaced by list_parse / list_parse2. – jbkunst Oct 11 '17 at 00:45
  • @jbkunst I had a different version at home ^^, which is much older. I've updated the answer, thx – Pork Chop Oct 11 '17 at 07:09
  • Quick comment: using categories on hc_xAxis creates incorrectly labeled xAxis steps. It's actually evidenced in the screenshot above (see how Building A shows up twice, instead of Building A and Building B). Any ideas? – Bogdan Rau Oct 16 '17 at 23:12
  • Hmmmm...I'm not as worried about sharing legends, but rather correctly identifying the points on the X axis. Look at your screenshot: the first group of bars is labeled Building A. The second group is also labeled Building A, when it should be Building B. I may be misreading, but I don't think that issue fixes what's going on above. Am I wrong? – Bogdan Rau Oct 17 '17 at 14:52