[R] Plot cumulative sums of rainfall per year

Martin Labadz m.labadz at qut.edu.au
Tue Mar 26 06:44:13 CET 2013


Hi @all,

I am biting my nails with the following problem:

I have a data set of daily rainfall measurements for the last 20 years. What I want to do is calculate the daily cumulative sum of rainfall but only for every year which means that the cumulative sum has to be reset each year. After the calculations I want to plot each year of cumulative rainfall as a separate line in one graph preferably using ggplot with the x-axis showing the julian day 1 to 365 (366) and the y-axis showing the cumulative values.

I have the following code:

library(plyr)
library(ggplot2)
data<-read.csv("http://dl.dropbox.com/u/4236038/test_cumu.csv")
data$year <- as.numeric(format(as.Date(data$Date), format="%Y"))
ddply(data,.(year),transform,cumRain = cumsum(Rainfall))->cumu
ggplot(cumu, aes(Date,cumRain))+geom_point()

What it does it perfectly calculates the cumulative sum of the rainfall and resets the sum at the beginning of each year but I cannot plot the cumulative sum of rainfall in a way that each year is represented by a separate line in one graph such as in this example: http://dl.dropbox.com/u/4236038/example_cumulative_rainfall.png

Any help would be highly appreciated.

Thanks,
Martin



More information about the R-help mailing list