[R] Using dates on axis with Grid plots

Paul Murrell p.murrell at auckland.ac.nz
Thu May 26 01:04:31 CEST 2011


Hi

On 15/05/2011 2:01 a.m., Larry White wrote:
> Hi,
>
> I'm trying to use Grid plots and would like to have an X axis that
> represents dates. I have several years of data so I would like to be able to
> have labeled tick marks only intermittently (not one per date).  I can
> transform the initial data from a date time string into POSIXlt or POSIXct,
> or Date objects.
>
> The issue is that when I try to layout the plot using:
>
> dev.off()
>    pushViewport(
>      plotViewport(
>        c(5, 5, 4, 2),
>          xscale=c(
>             min(foo),
>             max(foo)),
>             yscale=c(0,30)
>      )
> )
>
> I get this error, regardless of whether "foo" is formated as POSIXlt,
> POSIXct or Date for the xscale.
>
> Error in valid.viewport(x, y, width, height, just, gp, clip, xscale, yscale,
>   :
>    Invalid 'xscale' in viewport
>
> How does one specify the scale?  In the end, I would prefer something like
> "2007   2008", etc. to appear on the axis and, of course, I'd actually want
> to plot the data with an x axis of dates. I'm not yet far enough to see if
> that's going to be an issue.

The viewport scale has to be numeric.  Dates are not supported at all by 
grid.xaxis() or grid.yaxis().  If your dates are just days, you can 
as.numeric() them to get a useful range and then pretty() and 
as.Date(origin="1970-01-01") to get useful labels for tick marks, but 
the general solution is more complex (see axis.Date()).  Depending on 
what your plots need to look like, another approach would be to 
customize a 'lattice' plot because it provides support for date-based  axes.

Paul

>
> Apologies if this is a stupid question, but it's very hard to search for
> Grid examples on the web, because "grid" is a very common term in R plotting
> generally.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list