Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r - ggplot stacked area plot with NAs using geom_area

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

Stacked area plot showing four categories, displaying continuous data from 2005-2010

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

Stacked line plot showing the same contours as previous; lines break after 2007 and resume in 2009

like image 570
KT- Avatar asked Dec 02 '25 21:12

KT-


1 Answers

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

enter image description here

like image 67
Jon Spring Avatar answered Dec 05 '25 11:12

Jon Spring



Donate For Us

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