Question Details

No question body available.

Tags

r ggplot2

Answers (2)

February 25, 2026 Score: 4 Rep: 34,723 Quality: Medium Completeness: 80%

While I like the other answer, if you are fine with colors "blending" at the edges, we can simply use geomribbon() with group = 1 to create a continuous area.

library(ggplot2)

ggplot(taxodiversity, aes(x = intervalMa)) + geomribbon(aes(ymin = 0, ymax = speciesrichness, fill = period, group = 1), alpha = 0.4) + geomline(aes(y = speciesrichness), color = "black", group = 1) + labs(x = "Millions of years ago", y = "Number of species") + scalefillmanual(values = c("lightpink", "mediumpurple1", "skyblue", "darkolivegreen2", "orange")) + themeclassic() + theme(axis.text.x = elementtext(angle = 90, hjust = 1, vjust = 0.5), legend.position = "bottom")

Data:

taxo
diversity
February 25, 2026 Score: 3 Rep: 168,420 Quality: Medium Completeness: 80%

I think the use of a factor on the x-axis is making this a little harder than it needs to be. I suggest changing that to using start itself as a continuous variable. With this, we can just grab the lead/lag of start and speciesrichness and calculate the mid points.

Up front, while these data appear to be fixed on intervals of 10, I'm not going to assume that, in case you have anything else. Also, I'm using dplyr here for convenience, but this can be done in base R if you'd prefer.

Note: your data has factors for intervalMa but we don't have enough to recreate that. Frankly, the factors (and their ordering) is not used here, I'll replicate what I think is happening by reversing the x axis.

Using the apparent data and your plot code (while reversing the x-axis), I see the following.

original plot from OP's data and code

My change:

library(dplyr) taxodiversity |> arrange(start, end) |> mutate( across(c(start, speciesrichness), ~ lag(.x), .names = "prev{.col}"), across(c(start, speciesrichness), ~ lead(.x), .names = "next{.col}") ) |> reframe( .by = period, start = c(first(prevstart + (start - prevstart)/2), last(start + (nextstart - start)/2) - 1e-9), speciesrichness = c(first(prevspeciesrichness + (speciesrichness - prevspeciesrichness)/2), last(speciesrichness + (nextspeciesrichness - speciesrichness)/2)) ) |> bindrows(taxodiversity) |> filter(!is.na(start), !is.na(speciesrichness)) |> arrange(start) |> ggplot(aes(x = start, y = speciesrichness)) + geomarea(aes(fill = period, group = period), alpha = 0.4) + geomline(color = "black", group = 1) + themeclassic() + labs(x = "Millions of years ago", y = "Number of species") + scalexreverse(breaks = taxodiversity$start, labels = taxodiversity$intervalMa) + scalefillmanual(values = c("lightpink", "mediumpurple1", "skyblue", "darkolivegreen2", "orange")) + theme( axis.text.x = elementtext(angle = 90, hjust = 1, vjust = 0.5), legend.position = "bottom" )

updated plot with contiguous regions that split evenly between the surrounding periods

Data used for this plot:

taxo
diversity