[R] plot with dates

Gabor Grothendieck ggrothendieck at myway.com
Tue Dec 14 18:41:57 CET 2004



Halldor Björnsson <halldor <at> vedur.is> writes:

: 
: Hi,
: I am trying to understand the behaviour of the plot function.
: If I have
: 
:   novdate <- as.Date("2001/11/1") + (0:29)
:   y <- 1:30
:   b <- data.frame(novdate,y)
: 
: then plot(b$novdate,b$y) will produce a plot where the x-ticmarks are 
: given as dates (Nov 04, Nov 09 etc), but plot(b) will produce a plot
: where the x-tickmars are integer values (#day since Jan 1st 1970)
: 
: In the first case plot is getting a an x-vector of class Date, and 
: y-vector of class integer.  In the second case plot gets an object of 
: class data.frame (but with components of class Date and integer).
: 
: I am new to R so I may be wrong about this, but it seems to me that
: different plotting methods are invoked. methods(plot) yields a list of
: plot methods, but I cannot access most of them.

If you issue the command plot(x, ...whatever...) it will
wind up calling plot.foo if class(x) is "foo".  You can find
the possibilities via:

methods(plot)

To view the starred ones on the output from the above
you have to do somethingk like this assuming you want
to look at the source for plot.acf:

stats:::plot.acf

(since plot.acf is from the stats package).

: 
: Is there a way to guide plot(b) to using the method used by 
: plot(b$novdate,b$y), - or is that a bad idea?...
: 
: Sincerely,
: Halldór
: 


This really looks like a time series so you probably really want
to treat it that way, not as a data frame.

library(zoo)
z <- zoo(y, novdate)
plot(z)




More information about the R-help mailing list