Based on this example, I am trying to plot lines by group and varying linetype, and add under each subgroup a shade of color for that group. So, I have a group modif
that I need to color the underlining ribbon
, and have grp
to specify linetype
. Ideally, if lines would be black/have the same color as the ribbon (ribbon should have some alpha). AS my ribbon
is not an indication of confidence interval, insteda it plots min and max values from additional groups, I use ~ pivot_wider
and accordingly define ymin = c, ymax = a
.
This is what I want to get, including the line plot and ribbons by group:
and this is what I get so far, without the underlying ribbons:
Here is my dummy example:
# Make a different colur of shade by group
library(ggplot2)
library(tidyr)
# example for shaded line plot
dd1 <- data.frame(year = c(1:5),
grp = 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),
modif = rep('no', each = 15))
dd2 <- dd1
dd2$vals = dd1$vals*0.8
dd2$modif = 'yes'
# Put data together
dd <- rbind(dd1, dd2)
# Get a plot
dd %>%
ggplot(aes(x = year)) +
geom_ribbon(
data = ~ pivot_wider(., names_from = grp,
values_from = vals),
aes(ymin = c, ymax = a, fill = modif)
) +
ylim(0,6.5) +
geom_line(aes(y = vals,color = modif, linetype = grp),
lwd = 1.5) +
theme_bw()
If I run the code, I got a warning message: Warning messages: 1: In max(ids, na.rm = TRUE) : no non-missing arguments to max; returning -Inf 2: In max(ids, na.rm = TRUE) : no non-missing arguments to max; returning -Inf
and geom_ribbon
is missing. How to correct this?