Hello. I am having the worst time converting x-axis date ticks to real dates. I have tried several suggestions in online help tips and books to no avail. For example, the x-axis has 0, 50, 100, etc, and I want it to have "6/17/03", "8/6/03" etc. See attached (sample). Can anybody help me with this. Here's my code: ts.plot(date.attackmode.table[,1], type="l", col="blue", lty=2,ylab="IED Attacks", lwd=2,xlab="Attack Dates",main="Daily Summary of Attack Mode") grid() Thanks for your help if possible. -------------- next part -------------- A non-text attachment was scrubbed... Name: sample.pdf Type: application/pdf Size: 9873 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-help/attachments/20050614/c781e058/sample.pdf
try this example:> x.1 <- strptime("6/17/03",'%m/%d/%y') > x.1[1] "2003-06-17"> plot(0:250, xaxt='n') > dates <- x.1 + c(0,50,100,150,200,250) * 86400 # 'dates' is in seconds,so add the appropriate number of days> dates[1] "2003-06-17 00:00:00 EDT" "2003-08-06 00:00:00 EDT" "2003-09-25 00:00:00 EDT" [4] "2003-11-13 23:00:00 EST" "2004-01-02 23:00:00 EST" "2004-02-21 23:00:00 EST"> axis(1, at=c(0,50,100,150,200,250), labels=format(dates,"%m/%d/%y")) #format the output>Jim __________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Convergys Labs james.holtman at convergys.com +1 (513) 723-2929 "Bernard L. Dillard" <bld at math.umd.edu> To: r-help at stat.math.ethz.ch Sent by: cc: r-help-bounces at stat.m Subject: [R] Dateticks ath.ethz.ch 06/14/2005 12:27 Hello. I am having the worst time converting x-axis date ticks to real dates. I have tried several suggestions in online help tips and books to no avail. For example, the x-axis has 0, 50, 100, etc, and I want it to have "6/17/03", "8/6/03" etc. See attached (sample). Can anybody help me with this. Here's my code: ts.plot(date.attackmode.table[,1], type="l", col="blue", lty=2,ylab="IED Attacks", lwd=2,xlab="Attack Dates",main="Daily Summary of Attack Mode") grid() Thanks for your help if possible. (See attached file: sample.pdf) ______________________________________________ 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 -------------- next part -------------- A non-text attachment was scrubbed... Name: sample.pdf Type: application/pdf Size: 9874 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-help/attachments/20050614/24bb6b3f/sample.pdf
On 6/14/05, Bernard L. Dillard <bld at math.umd.edu> wrote:> Hello. I am having the worst time converting x-axis date ticks to real > dates. I have tried several suggestions in online help tips and books to > no avail. > > For example, the x-axis has 0, 50, 100, etc, and I want it to have > "6/17/03", "8/6/03" etc. See attached (sample). > > Can anybody help me with this. > > Here's my code: > > ts.plot(date.attackmode.table[,1], type="l", col="blue", lty=2,ylab="IED > Attacks", lwd=2,xlab="Attack Dates",main="Daily Summary of Attack > Mode") > grid() >The default format for dates in chron is the one needed here so let us use that. (We could alternately use Date class in defining tt and specify a format string as a second argument to format in the last line.) library(chron) # test data. values are in y and times are in tt. y <- rnorm(201) tt <- chron("8/6/03") + seq(0, length = length(y)) # plot without axis plot(tt, y, xaxt = "n") # axis with ticks and labels every 50 days idx50 <- seq(1, length(tt), 50) axis(1, tt[idx50], format(tt[idx50]), cex.axis = 0.5) or, to use Date class replace with this: tt <- as.Date("2003-08-06") + seq(0, length = length(y)) plot(tt, y, xaxt = "n") idx50 <- seq(1, length(tt), 50) axis(1, tt[idx50], format(tt[idx50], "%m/%d/%y), cex.axis = 0.5) More info on working with dates is in R News 4/1.