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

Petr Pikal petr.pikal at precheza.cz
Mon Mar 5 10:30:23 CET 2007


Hi

you probably know that the second column are dates but your poor PC 
does not, so you should to tell him.

You have several options:

Change the column to suitable date format - see chron package or help 
pages related to date functions e.g. strptime, as.Date, ... and 
perform your plot.

Change your dat column to character vector and using it as a labels 
to x axis - see help pages to plot, axes, titles....

On 5 Mar 2007 at 13:26, d. sarthi maheshwari wrote:

Date sent:      	Mon, 5 Mar 2007 13:26:28 +0530
From:           	"d. sarthi maheshwari" <samay.sar at gmail.com>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] plot(): I want to display dates on X-axis.

> Hi,
> 
> I want to display dates on my x-axis of the plot. I was trying to use
> plot() command for the same and passing the values in following
> manner:
> 
> 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
> ....
> ....
> 

However what did you suppose this command will do?
Did you even try to read plot help page?

> the command I am performing is::
> 
> plot(x=dat[1], y=as.character(dat[2]))
> 

If you want to plot date on x axis and values on y axis why you did 
it in opposite way?

plot(dat[,2], dat[,1])

after transformation to date format shall do what you want.

Regards
Petr

> 
> 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.
> 
> -- 
> Thanks & Regards
> Sarthi M.
> 
>  [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list