[R] ggplot2 / time series with different scales

Chuck White chuckwhite8 at charter.net
Thu Feb 4 17:04:22 CET 2010

I am trying to plot this dataset using ggplot2:

df <- data.frame(
   sid = c(rep('11',30),rep('22',30)),
   time  = rep(ISOdate(year = 2010, month = 1, day = 1:30),2),
   sales = c(rnorm(30, 1000, 20),rnorm(30, 900, 10)),
   price = c(rnorm(30, 2, 0.5),rnorm(30, 3,0.5))

Plotting just the sales can be done easily:
ggplot(data=df, aes(x=time, y=sales, group=sid, color=sid)) + geom_line(size=1)

How can I plot the price (using geom_point) in a separate plot just above the sales plot so that the xaxes match and the yaxes are different. Or is there a better way? Thanks.

More information about the R-help mailing list