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 .... .... the command I am performing is:: plot(x=dat[1], y=as.character(dat[2])) 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]]
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
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
You can also do it with the following: plot(as.POSIXct(strptime(as.character(dat[,2]), "%Y%m%d")), dat[,1]) On 3/5/07, d. sarthi maheshwari <samay.sar@gmail.com> wrote:> > 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 > .... > .... > > the command I am performing is:: > > plot(x=dat[1], y=as.character(dat[2])) > > > 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@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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
> > 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 > .... >This looks like a matrix ... not a data frame. You defiantly want a data frame. So lets say you have: dat <- matrix(c(round(rnorm(8)*100+400),20060101:20060108),ncol=2) #convert it: dat <- as.data.frame(dat) #determine the dates: dat$date <- as.Date(as.character(dat$V2),"%Y%m%d") #plot it: plot(V1 ~ date, dat)