[R] X-axis with POSIXct dates

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 20 18:00:59 CEST 2001


On Wed, 19 Sep 2001 Jim_Garrett at bd.com wrote:

>
> I have a series of datasets, each containing pH measurements and
> manufacturing dates, and each dataset pertains to a different manufactured
> product.  I'm trying to create a series of plots of pH measurements by
> date, but the default X-axis labeling behavior is not giving adequate
> results in this particular case, and I can't figure out how to persuade R
> to come up with something more meaningful.  Specifically, the datasets
> generally span about 1.5 years, and only January 1 is given a tick mark and
> label.  Some datasets contain both Jan. 1 2000 and Jan. 1 2001, and others
> just Jan. 1 2001, so I'm getting one or two tick marks.  Generally every
> month is represented in these datasets, and I would like to see a tick mark
> every two or three months.  I can't seem to find a graceful way to do this.
>
> System info:  Windows NT 4.0, 450 MHz Pentium II (I think), R version
> 1.3.0.
>
> Here is a small subset of data which reproduces the undesired default
> behavior on my system:
>
> > test.dat
>         man.date First.Bottle.pH
> X14   2000-03-08            9.74
> X4241 2000-08-04            9.42
> X4542 2000-12-07            8.54
> X350  2001-02-05            9.51
> X4591 2001-02-05            8.47
> X4716 2001-02-07            9.54
> X4933 2001-03-19            9.63
> X2831 2001-04-19            9.70
> X1068 2001-04-23            9.65
> X1914 2001-05-23            9.98
>
> "man.date" is of class POSIXct.
>
> Entering
>
> > plot(test.dat$man.date, test.dat$First.Bottle.pH)
>
> yields a plot with a single X-axis tick mark and label at Jan. 1, 2001, as I said.  I dabbled with the "axis.POSIXct" function,
> especially with the format option, but couldn't coerce it to plot more tick marks.  For example,
>
> > axis.POSIXct(1, test.dat$man.date, "%m %d, %Y")
>
> creates a tick mark and date label in the desired format, but only at Jan. 1, 2001.  In other words, It controls what the label looks like,
> but not the number and spacing of labeled tick marks.
>
> Any ideas would be appreciated!

axis.POSIXct is just a convenient way to generate calls to axis.
For some dates z

axis(1, at=z, labels = format(z, format="%m %d, %Y"))

should label the x axis at those dates.

All rules will be rough-and-ready.  axis.POSIXct switches from months to
years at 1.1 years: perhaps that is too low, but once you have much more
than that you want the years as well.

z <- test.dat$man.date
axis(1, at=z, labels = format(z, format="%m %d, %Y"))

omits some labels, by overlap.

plot.default(test.dat$man.date, test.dat$First.Bottle.pH, xaxt="n")
axis(1, at=z, labels = format(z, format="%m %d\n%Y"), srt=45)

does a better job, but you will need to choose dates that make sense to
you.

>
> Jim Garrett
> Becton Dickinson Diagnostic Systems
> Baltimore, Maryland, USA
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list