I am trying to plot a line plot by groups, where I would like to add underling shading covering min
and max
values for categories. I would like to replicate plot like this, but grey shade covers all between the dashed and solid black lines (not showing confidence interval):
Normally, this is done using geom_ribbon
where I need to specify values for aes(ymin = ymin, ymax = ymax)
. But how to pass the ymin
and ymax
values for values of group c
and a
(in my example)?
Here is my dummy example:
# example for shaded line plot
dd <- data.frame(year = c(1:5),
group = rep(c("a", "b", "c"), each = 5),
vals = c(5, 5.2, 5.6, 5.8, 6,
5, 4.9, 4.8, 4.7, 4.2,
5, 4.8, 4.4, 4, 3))
dd %>%
ggplot(aes(x = year,
y = vals,
shape = group,
color = group,
linetype = group)) +
ylim(0,6.5) +
geom_line(lwd = 1.5) +
theme_bw()
My data are in long format and I have many groups, so I would appreciate some automatic solution. Thank you in advance.
My approach:
convert data from long to wide format to pass argumens as ymin
and ymax
. Not working as using two different dataframes:
# maybe first convert data from long to wide?
library(tidyr)
dd.spread <- spread(dd, group, vals)
dd %>%
ggplot(aes(x = year,
y = vals,
shape = group,
color = group,
linetype = group)) +
geom_line() +
geom_ribbon(dd.spread, aes(ymin = c, ymax = a), color = "grey70")
Expected outcome (shaded between a-c):
You can put in a sneaky pivot_wider
at the data
argument of a layer. In the example below I migrated some aesthetics from the main plot call to the line layer, as they were conflicting in the ribbon layer.
library(ggplot2)
library(tidyr)
#> Warning: package 'tidyr' was built under R version 4.0.3
# example for shaded line plot
dd <- data.frame(year = c(1:5),
group = rep(c("a", "b", "c"), each = 5),
vals = c(5, 5.2, 5.6, 5.8, 6,
5, 4.9, 4.8, 4.7, 4.2,
5, 4.8, 4.4, 4, 3))
dd %>%
ggplot(aes(x = year)) +
ylim(0,6.5) +
geom_ribbon(
data = ~ pivot_wider(., names_from = group, values_from = vals),
aes(ymin = c, ymax = a)
) +
geom_line(aes(y = vals,
color = group,
linetype = group),
lwd = 1.5) +
theme_bw()
Created on 2021-04-19 by the reprex package (v1.0.0)