I have found lots of good advice on this forum about stacked area charts but I?ve run into problems with the 2 recommended options: stackploy in plotrix or qplot in ggplot2. I have a many page report that will be in a 2x2 page format ?par(mfrow=c(2,2))?? and need one of the page components to be a stacked chart. I?d prefer to use stackpoly, if possible, but I?m stuck on how to do the legend in hopefully a relatively simple way. I just need a simple legend to be outside the plot area (b/c the plot area is full as I?m doing it as 100% stacked area chart format) on the right side with the little box with the color and the series name next to it ? same/similar to Excel format. The qplot approach would work as well and seems like the legend is built into the default, but the graph always ends up on its own page and can?t figure out how to force it into one of boxes of my 2x2 page layout. I like the looks of the stackpoly more as consistent with other graphs I have on report but qplot stacked chart would be fine if I could get it into the layout. Please let me know if you have any suggestions or could point me to somewhere this has been discussed before. Many thanks for any help. -- View this message in context: http://www.nabble.com/Stacked-area-chart-and-legends-tp19840703p19840703.html Sent from the R help mailing list archive at Nabble.com.
On Mon, Oct 6, 2008 at 10:54 AM, mbr <mattblakley at gmail.com> wrote:> > I have found lots of good advice on this forum about stacked area charts but > I've run into problems with the 2 recommended options: stackploy in plotrix > or qplot in ggplot2. I have a many page report that will be in a 2x2 page > format "par(mfrow=c(2,2))"" and need one of the page components to be a > stacked chart. > > I'd prefer to use stackpoly, if possible, but I'm stuck on how to do the > legend in hopefully a relatively simple way. I just need a simple legend to > be outside the plot area (b/c the plot area is full as I'm doing it as 100% > stacked area chart format) on the right side with the little box with the > color and the series name next to it ? same/similar to Excel format. > > The qplot approach would work as well and seems like the legend is built > into the default, but the graph always ends up on its own page and can't > figure out how to force it into one of boxes of my 2x2 page layout. I like > the looks of the stackpoly more as consistent with other graphs I have on > report but qplot stacked chart would be fine if I could get it into the > layout.http://had.co.nz/ggplot2/book/grid.pdf describes how to position multiple ggplots on the same page. However, this won't work with a mix of base graphics and grid graphics (like lattice or ggplot). You either need to create all of your plot with one plotting system, or jump through some slightly painful hoops. Hadley -- http://had.co.nz/
mbr wrote:> I have found lots of good advice on this forum about stacked area charts but > I?ve run into problems with the 2 recommended options: stackploy in plotrix > or qplot in ggplot2. I have a many page report that will be in a 2x2 page > format ?par(mfrow=c(2,2))?? and need one of the page components to be a > stacked chart. > > I?d prefer to use stackpoly, if possible, but I?m stuck on how to do the > legend in hopefully a relatively simple way. I just need a simple legend to > be outside the plot area (b/c the plot area is full as I?m doing it as 100% > stacked area chart format) on the right side with the little box with the > color and the series name next to it ? same/similar to Excel format. > > The qplot approach would work as well and seems like the legend is built > into the default, but the graph always ends up on its own page and can?t > figure out how to force it into one of boxes of my 2x2 page layout. I like > the looks of the stackpoly more as consistent with other graphs I have on > report but qplot stacked chart would be fine if I could get it into the > layout. > >Hi Matt, The easy way is to make your device wider than it is high: # use the screen device for the example x11(width=10,height=7) #leave lots of room on the right side of the plot: par(mar=c(5,4,4,10)) #then plot, stop clipping to the plot region, add the legend # and restore clipping stackpoly(matrix(abs(rnorm(30)),nrow=10),stack=TRUE, main="Look ma. just like Excel!") par(xpd=TRUE) legend(11,3,1:5,fill=1:5) par(xpd=FALSE) Jim