我正在尝试使用Highcharter创建带分组的堆积条形图,并且需要在不使用hchart()
功能的情况下创建它。我有以下代码(hchart()
部分工作)。
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"))
下面的预期输出。最终目标是添加堆叠条形,然后使用另一个hc_add_series跟随它,这将在measure_target
列中添加一系列点(因此比较实际值与目标值)。
除了目标之外有两个堆叠的条形和一条线,例如:
答案 0 :(得分:2)
这样的东西?
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'))