简体   繁体   English

R Highchart //在x轴上分组类别

[英]R Highchart // grouped categories in x axis

I want to create a stacked bar chart with grouped categories like there . 我想创建一个分组类别,如堆积条形图存在

This is my example dataframe: 这是我的示例数据框:

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)

The x axis should be grouped by month / year / region, where month is on top. x轴应按月/年/地区分组,其中月份在最上方。

I already coded this with only using month as x axis: 我已经使用仅月份作为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))

Can someone help me to group the categories? 有人可以帮我对类别进行分组吗?

您会发现“分组类别”选项很有用: 高级章程示例

Maybe you can try something like this: 也许您可以尝试这样的事情:

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM