R Highcharts按组划分的多个堆积条形图

时间:2019-11-27 22:03:53

标签: r plot highcharts stack r-highcharter

我有这个数据框:

A_B2 <- data.frame(
  Pais = c("Ecuador","ALC","OECD"),
  Anio = c(rep(2014,each=9),rep(2017,each=9)),
  value = c(12,13,3,11,18,44,1,2,3,11,12,3,12,17,49,1,2,3),
  variable = c(rep("efectivo",each=3),rep("cuentaif",each=3),rep("telef",each=3)))

并使用以下代码:

hchart(A_B2%>%filter(Anio=="2017"), "column", hcaes(x = Pais, y = value, group = variable), stack = "2017") %>% 
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal",
    enableMouseTracking = TRUE))

我得到这张图: enter image description here

但是我想要这张图表;每年与每个“ Pais”堆叠的变量。 enter image description here

我尝试了以下代码,但没有结果:

hchart(A_B2%>%filter(Anio=="2017"), "column", hcaes(x = Pais, y = value, group = variable), stack = "2017") %>% 
  hc_xAxis(categories = unique(A_B2$Pais)) %>%
  hc_add_series(A_B2%>%filter(Anio=="2014"),type="column",hcaes(x = Pais, y = value, group = variable), stack = "2014")%>%
  hc_plotOptions(column = list(
    dataLabels = list(enabled = FALSE),
    stacking = "normal",
    enableMouseTracking = TRUE))

我在这里用库(highcharter)enter link description here找到了一个类似的问题,使用ggplot2可以得到答案enter link description here

使用ggplot2和我的数据的解决方案是:

ggplot(A_B2, aes(x = Pais, y = value, fill = variable)) + geom_col() + facet

0 个答案:

没有答案