I have an issue with geom_ribbon(). I am displaying the change of two different variables for several years. I fail to understand how to make geom_ribbon follow the path for years instead of connecting the data points according to the increase of values in x oder y axes.
Here is a minimal reproducible example. Any help is appreciated:
library(tidyverse)
library(ggplot2)
plotdata <- data.frame(Region = factor(c("A", "A", "A", "B", "B", "B")), income = c(60, 80, 90, 40, 50, 55),
population = c(90,120,100, 80, 90, 100), year = factor(c(2020, 2050, 2080, 2020, 2050, 2080)),
income_sd = c(5, 8, 10, 2, 3, 3))
ggplot(data = plotdata, aes(x = income, y = population, shape = year,
color = Region, fill = Region, group = Region)) +
theme_minimal(base_size = 12) +
geom_ribbon(aes(xmin = income - income_sd, xmax = income + income_sd), alpha = 0.2, color = NA) +
geom_line() + geom_point() +
geom_text(aes(label = year), color = "black")
In the resulting plot, note how for Region A everything looks the way it is supposed, since the y-variable increases monotonically with year. In the case of region B, however, the red ribbons connect in the wrong order since it is following the y-variable in monotonically increasing order. This is not what I want, I want it to follow the "years" variable. How do I achieve this?
Adding a y = * argument to geom_ribbon() removes the ribbons and thus defies the purpose. I have also played around with the order = * argument of geom_ribbon, as well as with levels in the dataframe for year. No success.
Thank you!