2017-10-10 70 views
1

我想创建一个使用Highcharter分组堆积条形图,并需要创建它,而不使用hchart()函数。我有以下代码(hchart()部分作品)。Highcharter堆叠列分组不使用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列中添加一系列点(以便比较实际值与目标值)。

我需要一些与此类似: enter image description here

除非两个堆叠酒吧和目标一行,像:

enter image description here

回答

1

像这样的事情?

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

+0

关闭。我需要点不能连接,我想你可以做分散而不是线。但是,我找不到list.parse3。你正在使用什么版本的highcharter?我在v 0.5.0.9999 –

+0

Im使用dev版本 –

+0

@PorkChop 1年前的开发版本:D,list.parseX已被list_parse/list_parse2所取代。 – jbkunst