2

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!

Plot:

Steffen
  • 79
  • 11

1 Answers1

2

This definitely isn't a good solution, but if you're in a pinch for just this one plot, it looks like you could hack it subsetting your data:

plotdata_1 <- plotdata[plotdata$Region!='A' | plotdata$year!='2080',]
plotdata_2 <- plotdata[plotdata$Region=='A' & plotdata$year!='2020',]

And then combining two ggplot objects with nearly identical uses of geom_ribbon (as you've provided above), except providing these two different dfs as your data.

navona
  • 92
  • 4
  • Thanks! The original plot which I intend to create has a much larger dataset with more "Regions" and even another grouping variable, so I do not think that this would be the best solution for me. – Steffen Aug 17 '21 at 09:52