[R] beginner plotting with dates

Michael A. Miller mmiller3 at iupui.edu
Tue Mar 11 16:44:24 CET 2003


>>>>> "ripley" == ripley  <ripley at stats.ox.ac.uk> writes:

    > On Tue, 11 Mar 2003, Rebecca Freeman Grais wrote:
    >> I am a beginner and have a question about plotting dates.
    >> I am creating a very simple boxwhisker plot ,
    >> boxplot(Total Case~Month).  When the boxplot appears, the
    >> months are re-ordered alphabetically.  I would like to be
    >> able to preserve the order, i.e. Jan, Feb, March, etc.  Is
    >> there a way to set the x-axis so that it is not
    >> automatically re-ordered? Thanks in advance for your help.

    > No, but you can set up Month (a factor?) with the levels in
    > the order you want, and then boxplot will follow.  See
    > ?factor.

I to create ordered factors from dates (and times) using chron.
For example:

> require(chron)
[1] TRUE
> dates(seq(1,100,by=10))
 [1] 01/02/70 01/12/70 01/22/70 02/01/70 02/11/70 02/21/70 03/03/70 03/13/70
 [9] 03/23/70 04/02/70
> x <- dates(seq(1,100))
> y <- seq(1,100)

> x
  [1] 01/02/70 01/03/70 01/04/70 01/05/70 01/06/70 01/07/70 01/08/70 01/09/70
  [9] 01/10/70 01/11/70 01/12/70 01/13/70 01/14/70 01/15/70 01/16/70 01/17/70
...

> plot(x,y)
> levels(months(x))
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> plot(months(x),y)

For time ranges spanning more than a single year, you can use
cut:

> x <- dates(seq(1,1000))
> y <- seq(1,1000)
> plot( cut(x,breaks='months'), y)

Mike



More information about the R-help mailing list