[R] Stacked area chart and legends

Jim Lemon jim at bitwrit.com.au
Tue Oct 7 11:22:01 CEST 2008


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



More information about the R-help mailing list