2

I have this data frame:

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

And with this code:

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

I get this chart: enter image description here

But i want this chart; the variables stacked for each year with each "Pais". enter image description here

I tried with this code with no result:

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

i found a similar question here wiht library(highcharter) enter link description here and with ggplot2 this would be the answer enter link description here

The solution with ggplot2 with my data would be:

ggplot(A_B2, aes(x = Pais, y = value, fill = variable)) + geom_col() + facet
  • what package is that plotting function from? I know this is possible with `ggplot2` using the `facet_grid` options – morgan121 Nov 27 '19 at 22:13
  • It is from library (highcharter), do you have a link for my example? Thanks for your comment. – David Medrano Nov 27 '19 at 22:15
  • this might help:https://stackoverflow.com/questions/48475213/stacked-bar-chart-with-group-by-and-facet – morgan121 Nov 27 '19 at 22:21
  • Does this answer your question? [Stacked bar chart with group by and facet](https://stackoverflow.com/questions/48475213/stacked-bar-chart-with-group-by-and-facet) – morgan121 Nov 28 '19 at 04:44
  • Hi. It's 100% achievable with Highcharts, you don't have to use ggplot2. Could you explain more precisely, what exactly you want to achieve? How many separate series you want to have in the legend? Are series in 2014 the same as 2017? – raf18seb Nov 28 '19 at 10:59
  • 1
    Thansk @RAB , your answer is useful because i can plot with ggplot2 but im searching the answer with library(highcharter). If some one is searching my question with ggplot2: library(ggplot2) ggplot(A_B2, aes(x = Pais, y = value, fill = variable)) + geom_col() + facet_grid(. ~ Anio) – David Medrano Nov 28 '19 at 14:39
  • Hi @raf18seb, i want to achive as the picture i uploaded or the answer i get with ggplot2 in the other comMent a thanked to RAB the series are showed in my example. 2014 has different values from 2017. – David Medrano Nov 28 '19 at 14:46
  • This is a pure JS example: https://jsfiddle.net/BlackLabel/m36acyx8 You can add "2014" and "2017" using second fake x axis or rendering it using Highcharts SVG Renderer. Unfortunately, I don't know R so I don't know how to parse your data to display this in R. – raf18seb Nov 29 '19 at 09:09

0 Answers0