shading between two lines

23 views
Skip to first unread message

Michael Cawthon

unread,
Feb 13, 2015, 10:13:50 PM2/13/15
to ggplot2
Given a data frame:

data <- data.frame(year = 2001:2010, y1 = 1:10, y2 = seq(10,13,1/3))


We plot the two lines over time:

ggplot(data, aes(x = year)) + geom_line(aes(y=y1), color = 'red') + geom_line(aes(y = y2), color = 'blue')


Suppose we want to shade a region in between the two lines; how can this be accomplished?

I've attempted using both geom_area() and geom_polygon(), but I am struggling with the appropriate mapping.

Thank you for your help.

Ista Zahn

unread,
Feb 13, 2015, 10:26:14 PM2/13/15
to Michael Cawthon, ggplot2
I think you want geom_ribbon:

ggplot(data, aes(x = year)) +
geom_ribbon(aes(ymin = y1, ymax = y2), fill = "green") +
geom_line(aes(y=y1), color = 'red') +
geom_line(aes(y = y2), color = 'blue')

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages