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.
chuckwhite wrote:> > I am trying to plot this dataset using ggplot2: > > .. self-contained example removed > > 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. > >To quote Hadley Wickham: http://markmail.org/message/xbuecsbudjxkmqkf "However, what you describe sounds like you want multiple scales on a single plot - and that's not something that ggplot is likely to ever support" One alternative would be to use free scales on separate plot. Or even better, a plot of sales against price? I know, executives and clinic head have troubles understanding these. Dieter -- View this message in context: http://n4.nabble.com/ggplot2-time-series-with-different-scales-tp1469011p1469030.html Sent from the R help mailing list archive at Nabble.com.
Thank you both for your response. As the names suggest, I am plotting the sales & price data for items over time to understand the how certain items may be more responsive than others to price changes. Another way of displaying this information on the same chart as the one showing sales, would be to scale the price data between 0 and 1 (1 - current_price/max_price) and use a geom_tile (?) to display it. In terms of the axes, x axis would still be time, y axis would be constant (top of the chart) and fill would change based on the scaled price (i.e. discount). Would it be possible to do this in ggplot2? df <- melt(df, id=c('time', 'sid')) ggplot(df, aes(time, value, colour=sid)) + geom_line(data = subset(df, variable=='sales')) + geom_point(data = subset(df, variable=='price')) + <--- replace with geom_tile? facet_wrap(~sid + variable, ncol=1, scales='free_y') Thanks.