[R] plot(): I want to display dates on X-axis.

Alberto Monteiro albmont at centroin.com.br
Mon Mar 5 12:43:25 CET 2007


Sarthi M. wrote:
> 
> I want to display dates on my x-axis of the plot.
>
Dates are a problem. There's a standard for dates, but it
seems that most users and software didn't catch up :-/

> The variable "dat" is a data frame. The first column has numeric 
> values and second column has date.
> 
> e.g. dat
> 
>                  [,1]                        dat[,2]
> 
> [1,]            300                       20060101
> [2,]            257                       20060102
> [3,]            320                       20060103
> [4,]            311                       20060104
> [5,]            297                       20060105
> [6,]            454                       20060106
> [7,]            360                       20060107
> [8,]            307                       20060108
> ....
> ....
> 
> the command I am performing is::
> 
> plot(x=dat[1], y=as.character(dat[2]))
> 
Hmmm... When I needed something similar, I did this day:

y <- dat[,1]  # because in a (xy) plot, x is the scale and y the data
years <- floor(dat[,2] / 10000)
months <- floor(dat[,2] / 100) %% 100
days <- dat[,2] %% 10000
x <- ISOdate(years, months, days)
plot(x, y)

> Kindly suggest some method by which I can perform my task of 
> displaying the first column values on y-axis against dates on x-axis.
> 
Of course, you can combine all that into one line, but readability
will be blown up:

plot(ISOdate(floor(dat[,2] / 10000), floor(dat[,2] / 100) %% 100,
  dat[,2] %% 10000), dat[,1])

Alberto Monteiro



More information about the R-help mailing list