% hc_title-6ren">
gpt4 book ai didi

R Highchart//x轴分组类别

转载 作者:行者123 更新时间:2023-12-02 20:56:30 26 4
gpt4 key购买 nike

我想创建一个带有分组类别的堆积条形图,例如 there

这是我的示例数据框:

region <- c("bavaria", "bavaria", "bavaria", "bavaria", "berlin", "berlin", "berlin", "berlin")
year <- c(2016, 2016, 2017, 2017, 2016, 2016, 2017, 2017)
month <- c(11, 12, 01, 02, 11, 12, 01, 02)
sales <- c(20, 17, 10, 5, 18, 16, 10, 7)
inc_sales <- c(3, 2, 1, 0, 4, 3, 2, 0)
df <- data.frame(region, year, month, sales, inc_sales)

x 轴应按月/年/地区分组,其中月份在顶部。

我已经仅使用月份作为 x 轴进行了编码:

library(tidyr)
library(dplyr)
library(highcharter)

highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "Sales") %>%
hc_xAxis(categories = df$month) %>%
hc_yAxis(title = list(text = "Sales")) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
) %>%
hc_series(list(name="sales",data=df$inc_sales),
list(name="inc_sales",data=df$sales))

有人可以帮我对类别进行分组吗?

最佳答案

也许你可以尝试这样的事情:

df <- data.frame(region, date=paste(month, year, sep="-"), sales, inc_sales)
df

highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "Some Title") %>%
hc_add_series(name="Sales",data = df$sales ) %>%
hc_add_series(name="Inc Sales", data = df$inc_sales) %>%
hc_xAxis(categories = list(
list(
name = "Bavaria",
categories = list("11-2016","12-2016","1-2017","2-2017")
),
list(
name = "Berlin",
categories = list("11-2016","12-2016","1-2017","2-2017")

)
) )%>%

hc_plotOptions(column = list(stacking = "percent"))

关于R Highchart//x轴分组类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40148108/

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