gpt4 book ai didi

r - 不使用 hchart() 的 Highcharter 堆积列分组

转载 作者:行者123 更新时间:2023-12-04 11:27:37 24 4
gpt4 key购买 nike

我正在尝试使用 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 中的一系列点。列(因此比较实际值与目标值)。

我需要类似的东西:
enter image description here

除了两个堆叠的条形和一条用于目标的线,例如:

enter image description here

最佳答案

像这样的东西?

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

关于r - 不使用 hchart() 的 Highcharter 堆积列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com