Search code examples
rggplot2stacked-chartfacet-wrap

Facet and Stacked bar plot from excel to ggplot2


I want to re-produce the following figure I did on excel + MS.pptx. I am doing this because I want a most sophisticated figure. However, I am struggling a lot.

I am using ggplot2 in R but struggling with facet

data %>% 
  ggplot(aes(x = year, 
             y = metric )) + 
  geom_bar(stat = "identity") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  facet_wrap(~scenario)

I did this on MS excel and ppt

Here you can find the reproducible data.

Any guidance to start the figure is very much appreciated.


Solution

  • ggplot(dat, aes(year, value, fill=fuel)) + 
      geom_col() + 
      facet_grid(metric~scenario, scales="free", space="fixed") +
      scale_x_continuous(breaks=c(2010, 2030, 2050)) +
      theme(axis.text.x = element_text(angle = 45, hjust = 1))
    

    enter image description here