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
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Martin Labadz > Sent: Tuesday, March 26, 2013 6:44 AM > To: r-help at r-project.org > Subject: [R] Plot cumulative sums of rainfall per year > > 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()Date in your cumu is not date but factor. Maybe it can be accomplished by ggplot but in that case I would use standard plot. cumu$dat<-as.Date(cumu$Date) cumu$mon<-as.Date(format(cumu$dat, paste("%d.%m", 2012, sep=".")), format="%d.%m.%Y") lll<-split(cumu[,c(4, 6)], cumu$year) plot(lll[[1]][[2]], lll[[1]][[1]], type="l") lines(lll[[2]][[2]], lll[[2]][[1]], col=2) you can use ylim to set proper range and put lines command into for cycle if necessary. Regards Petr> > 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 > > ______________________________________________ > 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.
Try this:
library(plyr)
library(ggplot2)
library(lubridate)
data<-read.csv("http://dl.dropbox.com/u/4236038/test_cumu.csv")
data$Date <- as.Date(data$Date)
cumu <- ddply(data,.(year(Date)),transform, cumRain = cumsum(Rainfall))
ggplot(cumu, aes(x = yday(Date), y = cumRain, color = factor(year(Date)))) +
geom_line()
You'll have to work on the date axis.
Jason Law
Statistician
City of Portland, Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
503-823-1038
jason.law at portlandoregon.gov
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Martin Labadz
Sent: Monday, March 25, 2013 10:44 PM
To: r-help at r-project.org
Subject: [R] Plot cumulative sums of rainfall per year
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
______________________________________________
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.