Re: [R] Using dates on axis with Grid plots

From: Paul Murrell <>
Date: Thu, 26 May 2011 11:04:31 +1200


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:
> 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.


> 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]]
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 25 May 2011 - 23:27:44 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 03 Jun 2011 - 12:20:13 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive