ggplot2: geom_area producing different output than expected

591 Views Asked by At

I want to create a stacked area chart using geom_area() for a dataset which has dates a x-axis and frequency as y-axis. My dataset looks like this

 Date           Variant.     Frequency
2020-08-01      AY.1          0
2020-08-01      B.1.351       0
2020-08-01      B.1.617.1     0
2020-08-01      B.1.617.2     0
2020-08-01      B.1.617.3     0
2020-08-01      others        1
2020-08-01      others        1
2020-09-01.     AY.1          0
2020-09-01      B.1.351       0
2020-09-01      B.1.617.1     0
2020-09-01      B.1.617.2     0
2020-09-01      B.1.617.3     0
2020-09-01      others        1
2020-09-01      others        1
.
.
.
.
2021-08-03      B.1.617.3 0.00564 
2021-08-03      others    0.36    
2021-08-03      others    0.36    
2021-08-04      AY.1      0.000713
2021-08-04      AY.4      0.42    
2021-08-04      B.1.1.7   0.00546 
2021-08-04      B.1.351   0.00137 
2021-08-04      B.1.617.1 0.0109  
2021-08-04      B.1.617.2 0.22    

I have tried using the following code to create a stacked area plot -

data %>% 
  ggplot(aes(x=Date, y=Frequency, fill=Variant)) + 
  geom_area(position = 'fill', alpha=0.8) +
  scale_x_date(date_breaks = '1 month', date_labels = '%b-%y',expand = c(0.01,0)) 

However, I end up with an unexpected output which looks like this

geom_area(position='fill')

I tried changing the position setting to 'identity' with geom_area(position='identity'), it gives an improved output, but not what I desire.

geom_area(position='identity')

I would like the output to look something like a basic stacked area chart in R -

basic R stacked area chart

I have tried geom_bar() as well which gives me a stacked barplot but I would like to create the similar chart with area

1

There are 1 best solutions below

0
TarJae On BEST ANSWER

To create a Stacked area chart:

  • Most important is the shape of your data. As r2evans already mentioned.

  • Here I took your fragmented dataframe and modified it with additional columns to show how your data should be organized to plot this kind of plot.

  • Basically you need a repeating group over time with certain values -> here group a:g, time 1:6, and Frequency:

modified fake data

library(tidyverse)
data <- df %>% 
    mutate(Date = lubridate::ymd(Date)) %>% 
    mutate(time = rep(row_number(), each=7, length.out = n())) %>% 
    mutate(group = rep(letters[1:7], length.out = n())) %>% 
    mutate(Frequency = rep(runif(29, 34, 100), length.out = n()))

code for plot:

library(tidyverse)
data %>% 
    ggplot(aes(x=time, y=Frequency, fill=group)) + 
    geom_area(alpha=0.8) 

resulting plot: enter image description here

fake data:

df <- structure(list(Date = structure(c(18475, 18475, 18475, 18475, 
18475, 18475, 18475, 18506, 18506, 18506, 18506, 18506, 18506, 
18506, 18539, 18539, 18539, 18539, 18539, 18539, 18539, 18475, 
18475, 18475, 18475, 18475, 18475, 18475, 18506, 18506, 18506, 
18506, 18506, 18506, 18506, 18539, 18539, 18539, 18539, 18539, 
18539, 18539), class = "Date"), Variant = c("AY.1", "B.1.351", 
"B.1.617.1", "B.1.617.2", "B.1.617.3", "others", "others", "AY.1", 
"B.1.351", "B.1.617.1", "B.1.617.2", "B.1.617.3", "others", "others", 
"AY.1", "B.1.351", "B.1.617.1", "B.1.617.2", "B.1.617.3", "others", 
"others", "AY.1", "B.1.351", "B.1.617.1", "B.1.617.2", "B.1.617.3", 
"others", "others", "AY.1", "B.1.351", "B.1.617.1", "B.1.617.2", 
"B.1.617.3", "others", "others", "AY.1", "B.1.351", "B.1.617.1", 
"B.1.617.2", "B.1.617.3", "others", "others"), Frequency = c(57.1679558907636, 
63.9314113892615, 36.638229729142, 94.1662813336588, 75.5338987568393, 
40.2345195417292, 69.4448804655112, 45.4072538088076, 60.232708573807, 
59.4782519731671, 94.5594410258345, 91.2153454185463, 79.8043070686981, 
56.9402130353265, 48.7265761620365, 72.413387727458, 67.7010886864737, 
55.5641814963892, 69.7157254447229, 86.2067115586251, 63.0903459019028, 
73.7501232894138, 92.7098404220305, 53.342769942712, 61.7025430542417, 
72.0743641522713, 90.9143544523977, 66.1621201317757, 91.2102537448518, 
57.1679558907636, 63.9314113892615, 36.638229729142, 94.1662813336588, 
75.5338987568393, 40.2345195417292, 69.4448804655112, 45.4072538088076, 
60.232708573807, 59.4782519731671, 94.5594410258345, 91.2153454185463, 
79.8043070686981), time = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L), group = c("a", "b", "c", "d", "e", "f", "g", "a", "b", "c", 
"d", "e", "f", "g", "a", "b", "c", "d", "e", "f", "g", "a", "b", 
"c", "d", "e", "f", "g", "a", "b", "c", "d", "e", "f", "g", "a", 
"b", "c", "d", "e", "f", "g")), class = "data.frame", row.names = c(NA, 
-42L))