[R] Stacked area plot for time series
Ben Neal
bneal at spg.ucsd.edu
Fri Dec 23 19:15:51 CET 2011
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:
library("zoo")
library("plotrix")
setwd("~/Documents/Loren/RawData")
CaribData = read.csv("FLA_Loren.csv", header = TRUE)
attach(CaribData)
## Create ZOO to order by time
z1=zoo(Commercial,Year)
z2=zoo(Recreational,Year)
z3=zoo(Subsistence,Year)
z4<-merge(z1,z2,z3)
##Convert to ts to interpolate missing NA values
ts1<-as.ts(z1)
ts1Ex<-na.approx(ts1)
ts2<-as.ts(z2)
ts2Ex<-na.approx(ts2)
ts3<-as.ts(z3)
ts3Ex<-na.approx(ts3)
## Merge into one object
ts4Ex<-cbind(ts1Ex, ts2Ex, ts3Ex, deparse.level = 1)
##Test plots to examine series
plot(ts1Ex)
plot(ts2Ex)
plot(ts3Ex)
plot(ts4Ex)
## Convert for stacked area plotting and plot
CaribMatrix<-as.matrix(ts4Ex)
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.
Hadley
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
http://had.co.nz/
More information about the R-help
mailing list