Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

r - Highcharter stacked column groupings not using hchart()

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...