facet_wrap / facet_grid在highcharter中有任何类似的功能吗?

时间:2018-11-13 08:58:00

标签: r r-highcharter

我具有以下数据框,并且已使用ggplot进行绘制。. facet_grid facet_wrap或highcharter的类似功能>

df1<-data.frame(
      Year=sample(2016:2018,100,replace = T),
      Month=sample(month.abb,100,replace = T),
      category1=sample(letters[1:6],100,replace = T),
      catergory2=sample(LETTERS[8:16],100,replace = T),
      lic=sample(c("P","F","T"),100,replace = T),
      count=sample(1:1000,100,replace = T)
    )

地块代码:

ggplot(df1,aes(Year,count,fill=factor(lic))) +
      geom_bar(stat = "identity",position = "stack") +
      facet_grid(~category1)

output

1 个答案:

答案 0 :(得分:1)

我发现的最好的就是这个。

df2 <- df1 %>% 
  group_by(Year,category1  ,lic) %>% 
  summarise(count=sum(count)) 
map(unique(df2$category1), function(x){
  df2 %>% filter(category1 == x) %>% 
    hchart(., "column", hcaes(x = Year, y = count, group = lic),showInLegend = FALSE) %>% 
    hc_plotOptions(column = list(stacking = "normal")) %>% 
    hc_add_theme(hc_theme_smpl()) %>% 
    hc_title(text = x) %>% 
    hc_yAxis(title = list(text = ""))
  }) %>% 
  hw_grid(rowheight = 150) %>% htmltools::browsable()

enter image description here