11

I have data frame containing variable and it's conf. interval

time x     x.upper   x.lower
   1 1.00     0.91      1.11
   2 1.03     0.92      1.13
   3 1.03     0.95      1.17
   2 1.06     0.90      1.13

I ggplot it:

library(ggplot2)
ggplot(data = df,aes(time,x))+
    geom_line(aes(y = x.upper), colour = 'red') +
    geom_line(aes(y = x.lower), colour = 'blue')+
    geom_line()

I want to highlight area between red and blue lines, smth similar to geom_smooth() function. How can I do it?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
BiXiC
  • 933
  • 3
  • 9
  • 29

1 Answers1

25

A geom_ribbon is exactly what you need

ggplot(data = df,aes(time,x))+
    geom_ribbon(aes(x=time, ymax=x.upper, ymin=x.lower), fill="pink", alpha=.5) +
    geom_line(aes(y = x.upper), colour = 'red') +
    geom_line(aes(y = x.lower), colour = 'blue')+
    geom_line()

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Could you add a legend to identify the color of the blue and red lines? – dca Aug 15 '17 at 00:30
  • use `geom_line (aes(y = x.upper, colour = 'upper'))` and `geom_line((aes(y = x.lower, colour = 'lower')` to automatically create colour scales and legend – mzuba Jul 03 '19 at 10:07