[R] ggplot2 & ribbon

hadley wickham h.wickham at gmail.com
Sat Feb 16 16:37:44 CET 2008

> p<-ggplot(reading.melt,aes(x=Date,y=value))
> p+geom_path(aes(colour=Type),size=2)+facet_grid(variable~.)
> but can't figure out
> (1) How to add a transparent shade between the Paid & Total lines.  I've
> tried the ribbon function, but cant seem to get it to shade between the
> lines

To do this you'll need the data in a different format

df <- cast(reading.melt, ... ~ Type)

Then add on a ribbon as follows

p <- ggplot(reading.melt,aes(x=Date)) +
 facet_grid(variable ~ .)  +
 geom_ribbon(aes(min = Paid, max=Total), data=df, fill=alpha("black",
0.1), colour=NA) +
 geom_path(aes(colour=Type ,y=value))

> (2) Add a left justified title to the chart

p + opts(title = "My title")

adds a title, and to left justify it, you need to use grid to modify it:

grid.edit("title", x=unit(0, "npc"), hjust=0)



More information about the R-help mailing list