[R] Stacked area plot for time series

Ben Neal bneal at spg.ucsd.edu
Fri Dec 23 19:16:28 CET 2011

Meant to say ZOO objects . . . 

-----Original Message-----
From: Ben Neal [mailto:bneal at spg.ucsd.edu]
Sent: Fri 12/23/2011 10:15 AM
To: Hadley Wickham; UncleFish
Cc: r-help at r-project.org
Subject: RE: [R] Stacked area plot for time series
Yes, thanks for the direction. I am new to the list and new to R, and I can see that was not a very helpful description. 

I have since resolved my issue. My main issue was that stackpoly does not (I think) interact with XOO objects or ts object, and my secondary issue was that my time series was highly irregular (with inferred indigenous historical consumption rates of fish, separated by large gaps). I resolved this by taking my data, making it a zoo to order it, transforming to a ts to interpolate missing values, and transforming to a matrix to do the stackpoly plot.

I then resolved the issue of too many tick marks on the x axis by making a new axis. I did seem to have difficulty with some plot functions apparently do not to function in plotrix (specifically xaxt='n'). 

I am aware that stacked area charts might not an acceptable graphical representation of the data in some opinions, and I tend to agree. However, this output had to match the format of other graphs in the work, and was thus desired in this specific format. 

Thanks for providing this stackpoly tool - very handy! Code follows, and a final graphic is attached: 

CaribData = read.csv("FLA_Loren.csv", header = TRUE)

## Create ZOO to order by time

##Convert to ts to interpolate missing NA values

## Merge into one object
ts4Ex<-cbind(ts1Ex, ts2Ex, ts3Ex, deparse.level = 1)
##Test plots to examine series

## Convert for stacked area plotting and plot
stackpoly(CaribMatrix, stack=TRUE, xlab="Year",ylab="Catch in tons",
          main="Historical Florida reef fisheries catch by sector", axis4="FALSE")

stackpoly(CaribMatrix, stack=TRUE, xlab="Year",ylab="Catch in tons", 
          col.main="red", font.main=4,
          axis4="FALSE",  xat=seq(50,760,by=100), 
          xaxlab=c(1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000))

-----Original Message-----
From: h.wickham at gmail.com on behalf of Hadley Wickham
Sent: Fri 12/23/2011 9:41 AM
To: UncleFish
Cc: r-help at r-project.org
Subject: Re: [R] Stacked area plot for time series
You are more likely to get a helpful response if you provide a
reproducible example - without that I can only guess that you need to
use approx so you get y values at same x values.


On Wed, Dec 21, 2011 at 8:13 AM, UncleFish <bpneal at ucsd.edu> wrote:
> I wish to make a stacked area chart of a time series with three variables.
> The observations are very irregular, covering 500 years, with a few
> historical observations in the range 1500-1850, and then more regular
> observations since 1880 or so.  If I plot this in Excell it truncates the
> time series, but it can make a nice stacked area plot. My question is how
> can I have a correct x axis with a stacked plot in R? I thave tried the data
> in a zoo as well as a ts, and have tried ggplot2 and plotrix, but cannot get
> beyond a lattice plot.
> --
> View this message in context: http://r.789695.n4.nabble.com/Stacked-area-plot-for-time-series-tp4221849p4221849.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University

More information about the R-help mailing list