Hi, I am getting accumulated data from PostgreSQL, ie for every day in which a condition is true I get the number (count) of cases. Starting date is 2008-01-01 and end day the last day for which the condition is true (which is not necessarily today). I obviously do not get records (dates) with count = 0, in other words this is not a complete list of every day since 2008-01-01. Now I want I plot this, with the sum on the Y axis and the months on the X axis, preferably as a line drawing. Any ideas? el -- Dr. Eberhard W. Lisse \ / Obstetrician & Gynaecologist (Saar) el at lisse.NA el108-ARIN / * | Telephone: +264 81 124 6733 (cell) PO Box 8421 \ / Please send DNS/NA-NiC related e-mail Bachbrecht, Namibia ;____/ to dns-admin at na-nic.com.na
The zoo package can represent and plot irregular time series. There are three vignettes that describe it plus the help pages. See ?zoo ?plot.zoo ?xyplot.zoo e.g. library(zoo) z <- zoo(1:3, Sys.Date() + c(1, 2, 5)) plot(z) library(lattice) xyplot(z) On Wed, Sep 3, 2008 at 3:00 PM, Dr Eberhard W Lisse <el at lisse.na> wrote:> Hi, > > I am getting accumulated data from PostgreSQL, ie for every day in > which a condition is true I get the number (count) of cases. Starting > date is 2008-01-01 and end day the last day for which the condition > is true (which is not necessarily today). > > I obviously do not get records (dates) with count = 0, in other words > this is not a complete list of every day since 2008-01-01. > > Now I want I plot this, with the sum on the Y axis and the months > on the X axis, preferably as a line drawing. > > Any ideas? > > el > -- > Dr. Eberhard W. Lisse \ / Obstetrician & Gynaecologist (Saar) > el at lisse.NA el108-ARIN / * | Telephone: +264 81 124 6733 (cell) > PO Box 8421 \ / Please send DNS/NA-NiC related e-mail > Bachbrecht, Namibia ;____/ to dns-admin at na-nic.com.na > > ______________________________________________ > 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. >
On 3 September 2008 at 20:00, Dr Eberhard W Lisse wrote: | Hi, | | I am getting accumulated data from PostgreSQL, ie for every day in | which a condition is true I get the number (count) of cases. Starting | date is 2008-01-01 and end day the last day for which the condition | is true (which is not necessarily today). | | I obviously do not get records (dates) with count = 0, in other words | this is not a complete list of every day since 2008-01-01. | | Now I want I plot this, with the sum on the Y axis and the months | on the X axis, preferably as a line drawing. | | Any ideas? Sure. It's straightforward if you convert your date data to actual Date types. So for argument's sake:> rawData <- data.frame(x = c("2008-01-01", "2008-01-21", "2008-02-15", "2008-03-08", "2008-04-20", "2008-05-10", "2008-06-20"), y = c(4, 6, 8, 5, 7, 2 ,1)) > rawDatax y 1 2008-01-01 4 2 2008-01-21 6 3 2008-02-15 8 4 2008-03-08 5 5 2008-04-20 7 6 2008-05-10 2 7 2008-06-20 1> rawData[,"x"] <- as.Date(rawData[,"x"]) > rawData[,"yCum"] <- cumsum(rawData[, "y"]) > with(rawData, plot(x, yCum, main="Some text", type='l'))The key is the as.Date(). If your date characters have a different format, specify it there. See help(as.Date). If you want to aggregates, slice, dice, summarise by time-unit (like months or weeks), make this a zoo object and study the three excellent vignettes in the zoo package. R is pretty good at calculating on dates and times provided it is given the data in the right form. Hth, Dirk -- Three out of two people have difficulties with fractions.