Reputation: 1119
Let's say I create a simple plot using ggplot:
data(mtcars)
var1 <- mtcars$mpg
var2 <- mtcars$mpg + 5
df <- melt(cbind(var1,var2))
ggplot(df,aes(x=X1, y=value,color=X2))+geom_line()
I would like to draw a shaded region over each plotted line.
The problem is that I would like to use different values for each line.
I tried using geom_ribbon()
but I could only supply the shaded region values for one of the lines, but not for multiple lines.
Is there a way to plot a geom_ribbon()
for each line separately?
Upvotes: 1
Views: 6045
Reputation: 16842
You can treat geom_ribbon
the same as any other geom—create multiple based on some aspect of the data and map values to aesthetics. So the y-min & y-max arguments can be handled like you would color, fill, size, etc. inside aes
, including different values of those offsets. To illustrate, I've just added a column that gives those offsets, which are then used to determine the width of the ribbons. You can set the color of the line and the fill of the ribbon to match, as I did here, or use different sets of colors.
library(dplyr)
library(ggplot2)
data(mtcars)
var1 <- mtcars$mpg
var2 <- mtcars$mpg + 5
df <- reshape::melt(cbind(var1, var2)) %>%
mutate(gap = ifelse(X2 == "var1", 1, 2))
ggplot(df, aes(x = X1, y = value, color = X2)) +
geom_line() +
geom_ribbon(aes(ymin = value - gap, ymax = value + gap, fill = X2), alpha = 0.3, color = NA) +
scale_fill_manual(values = c("skyblue", "coral"), aesthetics = c("color", "fill"))
Since this question is 4 years old, I'll point out also that giving the aesthetics
argument in the scale is a shortcut added recently to set the same palette to multiple encodings at once (in this case, both color and fill); I rarely actually use it, but it fit the problem here.
Upvotes: 3
Reputation: 5152
Is that what you want?
data(mtcars)
var1 <- mtcars$mpg
var2 <- mtcars$mpg + 15
df <- melt(cbind(var1,var2))
df$shadv <- rep(c(2,6),each=length(var1))
df1 <- df[df$X2=="var1",]
df2 <- df[df$X2=="var2",]
ggplot(df,aes(x=X1, y=value,color=X2))+
geom_ribbon(data=df1,aes(x = X1,ymin = value - shadv, ymax = value + shadv), inherit.aes = FALSE,fill = "lightblue")+
geom_ribbon(data=df2,aes(x = X1,ymin = value - shadv, ymax = value + shadv), inherit.aes = FALSE,fill = "lightgreen")+
geom_point()+geom_line()
Upvotes: 4