Skip to content

(no subject)

7 messages · Gabor Grothendieck, Dirk Eddelbuettel, Dr Eberhard W Lisse +1 more

#
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:
#
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:
x 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
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
#
Dirk, Gabor,

thanks for your advice, I have now tried to study these
vignettes, but I must say, as an aging Gynecologist,
I am facing an enormous learning curve :-)-O.

This works nicely:

rawData <- data.frame(x = c("2008-03-01", "2008-03-21", "2008-03-23",  
"2008-04-08", "2008-04-20", "2008-05-10", "2008-06-20"), y = c(4, 6,  
8, 5, 7, 2 ,1))
rawData[,"x"] <- as.Date(rawData[,"x"])
with(rawData, plot(x, y, main="Some text", type='l'))

How do I make the X-Axis start on 2008-01-01, end on 2008-12-31,
with tick marks for every month (ie month.abb[1:12] or something
similar)?

A pointer is enough, weekend is coming up and I am off :-)-O

I also need to add 2 lines statements, but I reckon I can figure that  
out
by myself :-)-O


greetings, 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
#
Is this what you want?

 library(ggplot2)
rawData <- data.frame(Date = c("2008-03-01", "2008-03-21", "2008-03-23",
"2008-04-08", "2008-04-20", "2008-05-10", "2008-06-20"), y = c(4, 6,8, 5, 7, 2 ,1))
 rawData$Date <- as.Date(rawData$Date)
 qplot(Date,y,data=rawData,geom="line",xlim=c(as.Date("2008-1-1"),as.Date("2008-12-1")))
--- On Thu, 9/4/08, Dr Eberhard W Lisse <el at lisse.na> wrote:

            
#
I assume the problem is that you want the axis to have all 12
months but your data is much shorter.  Try this:

mos <- seq(as.Date("2008-01-01"), length = 12, by = "month")
plot(range(mos), range(rawData$y), type = "n", xaxt = "n")
lines(rawData$Date, rawData$y)
axis(1, mos, month.abb)
On Thu, Sep 4, 2008 at 3:15 PM, Dr Eberhard W Lisse <el at lisse.na> wrote:
#
Oh, YES, thank you!

This weekend I'll try and figure out how to plot these events on
a 24 hour scale, i.e I'll aggregate the SQL query on the time
But not on the date) to see how many of those fall outside of
normal working hours :-)-O

greetings, el
On 05 Sep 2008, at 00:59 , Gabor Grothendieck wrote: