Home > Net >  Transform Stacked Bar chart into Stacked Area chart
Transform Stacked Bar chart into Stacked Area chart

Time:12-31

I have a stacked bar chart with the following code:

ggplot(data_frame, aes(x = year, y = percent, fill = reorder(type, desc(percent)), label = round(percent, digits=2)))  
  geom_bar(stat = "identity")  
  geom_text(size = 3, position = position_stack(vjust = 0.5))

I am trying to turn it into a stacked area chart with the following code:

ggplot(data_frame, aes(x=year, y=percent, fill=as.factor(type)))   
    geom_area()

But the plot is empty or it does not work. Does anyone know what I am doing wrong here? It is my first time trying to create a stacked area chart. Thank you in advance

My dataframe is as follows:

structure(list(year = c(2012, 2013, 2013, 2014, 2014, 2015, 2015, 
2015, 2016, 2016, 2016, 2017, 2017, 2017, 2018, 2018, 2018, 2019, 
2019, 2019, 2020, 2020, 2020, 2021, 2021, 2021, 2022, 2022, 2022
), type = c("Blue", "Red", "Blue", "Red", "Blue", "Red", "Yellow", 
"Blue", "Red", "Yellow", "Blue", "Red", "Yellow", "Blue", "Red", 
"Yellow", "Blue", "Red", "Yellow", "Blue", "Red", "Yellow", "Blue", 
"Red", "Yellow", "Blue", "Red", "Yellow", "Blue"), percent = c(1, 
0.822493861837742, 0.177506138162258, 0.890831469712102, 0.109168530287897, 
0.813387070409725, 0.0497778071288365, 0.136835122461439, 0.864261238604485, 
0.036537301512893, 0.0992014598826223, 0.869551858365865, 0.0555953345546625, 
0.0748528070794728, 0.830539033602976, 0.075436440035225, 0.0940245263617988, 
0.775315022945149, 0.0633554986887996, 0.161329478366051, 0.601100639265675, 
0.254061406486953, 0.144837954247372, 0.630357987198663, 0.203272744842328, 
0.166369267959009, 0.660638396593096, 0.174442088167586, 0.164919515239318
)), row.names = c(NA, -29L), class = "data.frame")

CodePudding user response:

Sometimes geom_area runs into trouble when the categories aren't represented consistently at each step. (I presume under the hood, it doesn't know whether a newly-appearing category should start all at once or if it should ramp up from zero in the prior step. I typically (always?) want the latter behavior, but it's not the default.)

One quick fix would be to use tidyr::complete to fill in all the unrepresented types.

df |>
  tidyr::complete(year, type, fill = list(percent = 0)) |>
  ggplot(aes(x=year, y=percent, fill=as.factor(type)))  
  geom_area()

enter image description here

Without complete: (I'm using ggplot2 v3.4.0)

enter image description here

  • Related