Unable to plot a decent x-Axis in a time series plot using zoo

April 7, 2011
By

(This article was first published on Greg's Research » R, and kindly contributed to R-bloggers)

I use the R package zoo to plot a yearly time series of weekly averaged data. The problem is that my date variable (m.all$date) contains week numbers and these are plotted as x-Axis. What I would rather like to to is plot abbreviated months.

I can suppress the x-Axis in the plot using xaxt = "n" in the plot.zoo command, but cannot define a suitable x-Axis that plot abbreviated months instead.

I tried several variations of the axis.Date() commands without luck!

# Create zoo object for time series plot
z < - zoo(cbind(m.all$obsTPM, m.all$modTPM, m.all$refTPM), m.all$date)
names(z) <- c("Observation","Model estimate", "Model reference")
# Plot
plot.zoo(z[, 1], type = "l", lwd = 1, col = "black", screens = c(1), xlab = "Date (2005)", ylim=c(m.all$obsTPM,m.all$modTPM, m.all$refTPM), ylab = "Concentration (pg m-3)", main = "Alert TPM: Observations vs. Model Estimates, Weekly Means")
lines(z[, 2], lty = 5, lwd = 1, col = "blue")
lines(z[, 3], lty = 3, lwd = 1, col = "red")
legend("topleft", lty = c(1,5,3), legend = colnames(z), bty = "n", col = c("black","blue","red"), lwd = 1)

To leave a comment for the author, please follow the link and comment on his blog: Greg's Research » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , ,

Comments are closed.