I'm creating a stacked area plot showing phytoplankton biovolume over time, and I have one year of missing data. I'd like my plot to show the break in the data as it would using geom_line, but I can't find a way to recreate that using geom_area.
Example data:
phyto_df <- data.frame(
Date = rep(as.POSIXct(paste0(2005:2010, "-07-15")), each = 4),
Division = rep(c("Bacillariophyta", "Cryptophyta", "Cyanophyta", "Other"), 6),
Biovolume = c(570000, 190000, 880000, 220000,
360000, 570000, 910000, 210000,
430000, 880000, 980000, 310000,
NA, NA, NA, NA,
230000, 750000, 800000, 160000,
260000, 240000, 570000, 170000)
)
Here's the plot I'm currently producing:
library(ggplot2)
ggplot(phyto_df, aes(x = Date, y = Biovolume)) +
geom_area(aes(fill = Division))

For reference, I'd like my output to behave more like the stacked line plot:
ggplot(phyto_df, aes(x = Date, y = Biovolume)) +
geom_line(aes(color = Division), position = "stack")

I think you want an area chart that has a gap in it for the NA data, as does the line chart.
One way to do that would be to explicitly separate the data into groups separated by NA data. I add a variable called era that counts the cumulative number of NAs observed within each Division, and use this as the global data. Then, for the geom_area layer, I specify that the stacking should be calculated with separate group for each Division/era combination.
The geom_line layer is included for comparison, to verify that the geom_area is plotted the same way.
library(dplyr)
ggplot(phyto_df |> mutate(era = cumsum(is.na(Biovolume)), .by = Division),
aes(x = Date, y = Biovolume)) +
geom_area(aes(fill = Division, group = paste(Division, era)), alpha = 0.2) +
geom_line(aes(color = Division), position = "stack")

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With