Jesinsky
Jesinsky

Reputation: 81

Issue with plotting xlim with dates in R

I have been having problems setting the xlim when dealing with data with dates and I have tried various solutions suggested in forum but none have worked - what am I doing wrong? Here is a snippet of my data (NF):

               Dts          x
    1   2009-02-16  3.2894737
    2   2009-02-26  3.2894737
    3   2009-03-08  1.3157895
    4   2009-03-18  1.3157895
    5   2009-03-28  4.6052632
    6   2009-04-07  3.9473684
    7   2009-04-17  0.6578947
    8   2009-04-27  1.9736842
    9   2009-05-07  3.2894737
    10  2009-05-17  1.3157895

When I plot the data without setting a xlim it plots fine, but as soon as I try to include an xlim the plot has no data points and no time stamp on the x-axis. What am I doing wrong? Here's the code I'm using:

    plot(NF$Dts,NF$x, ylab="Y", xlab="Date", ylim=c(0,5), xlim=c(as.POSIXct('2009-02-16', format="%Y-%m-%d"),as.POSIXct('2014-08-10', format="%Y-%m-%d"))  )

Upvotes: 2

Views: 3000

Answers (1)

gsun
gsun

Reputation: 427

Try as.Date(),

plot(as.Date(NF$Dts),NF$x, ylab="Y", xlab="Date", ylim=c(0,5),
                     xlim=c(min(as.Date(NF$Dts)),max(as.Date(NF$Dts))))

Sample code:

NF<-data.frame(Dts=c('2009-02-16','2009-02-26','2009-03-08'),x=c(3.29,3.29,1.32))
plot(as.Date(NF$Dts),NF$x, ylab="Y", xlab="Date", ylim=c(0,5),
                               xlim=c(min(as.Date(NF$Dts)),max(as.Date(NF$Dts))))

Upvotes: 3

Related Questions