1

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?

upabove
  • 1,057
  • 3
  • 18
  • 29
  • You won't be seeing much difference with `±0.02` intervals for the ribbon given the scale of your plot. Try `...+ geom_ribbon(aes(ymin = value - 2, ymax = value + 2), fill = "red") + geom_line()` and you'll see that it does work. – mtoto Jul 06 '16 at 14:07
  • @mtoto: thanks, you are right. please see my updated question for the real problem that I have – upabove Jul 06 '16 at 14:25

2 Answers2

4

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()

enter image description here

Robert
  • 5,038
  • 1
  • 25
  • 43
  • thanks a lot for your help! Yes this is almost what I'm looking for. The remaining problem is that I would like to adjust the ymin ymax value of geom_ribbon separately for each line. – upabove Jul 06 '16 at 17:29
  • Excellent! I am happy to accept your answer as this is what I was asking! I do have one more small question: is it possible to use the same colors for the shading as the lines? i.e. two separate colors? – upabove Jul 06 '16 at 19:10
3

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.

camille
  • 16,432
  • 18
  • 38
  • 60