I'm trying to make a plot with a line and two ribbons for each of three factor levels (factor named block). This is my call to ggplot2:
ggplot(df, aes(x = x, y = y, fill = block, color = block)) +
geom_ribbon(aes(ymin = llb, ymax = uub), alpha = .1, color = NA) +
geom_ribbon(aes(ymin = lb, ymax = ub), alpha = .5, color = NA) +
geom_line(size = 2)
This is what I get: graph a better example added in later edit
As you can see in the picture, this is the order of plotting:
- Light red ribbon
- Light blue ribbon
- Light green ribbon
- Dark red ribbon
- Dark blue ribbon
- Dark green ribbon
- Red line
- Blue line
- Green line
This is ugly. I want to plot the line and two ribbons for the first factor level first, then all of those for the second level, and then the third. Or, more explicitly, plot in this order:
- Light red ribbon
- Dark red ribbon
- Red line
- Light blue ribbon
- Blue line
- Dark blue ribbon
- Light green ribbon
- Dark green ribbon
- Green line
Any ideas on doing so without subsetting the data by factor and manually calling the geoms three times? Perhaps an easy way to group three geoms into a new geom?
Thanks!