[R] plotting dates in x axis

Jim Lemon drjimlemon at gmail.com
Mon Feb 27 22:11:05 CET 2017

Hi Maria,
First, Excel files don't make it through the Mexican Wall. A CSV with
the extension changed to .txt might. You can get all of the years like

# set the tick marks at the middle of each year

Obviously you don't want all of the months, so just add the months to the years:



On Tue, Feb 28, 2017 at 2:26 AM, Maria Lathouri via R-help
<r-help at r-project.org> wrote:
> Dear all,
> I have an excel file of 180 observations with dates and one variable, from 1998 to 2012 by random months (there are some years that I might not have all the months or I might have two observations in one month). I am trying to plot the dates in x axis and the variable in y axis. I have already used as.Date for the dates so I can import them into R.
> Here is my script:> aa<-read.csv("aa.csv")> attach(aa)> names(aa)#"SDATE" "var1"
> I convert the dates into R: > sdate<-as.Date(SDATE, format="%Y-%m-%d")
> I am plotting the dates with my var1:> plot(sdate, var1, type="l")
> Up to now, everything seems ok. However, in the x-axis I only get three years, 2000, 2005 and 2010. As I want to show all the years or at least as many as it could be, I am using the following:
>> plot(sdate, var1, type="l", xaxt="n")
>> d1<-c((sdate[1]), (sdate[183]))> d2<-as.Date((d1[1])+365*(0:15))
>> axis(side=1, at=0:15, labels=strftime(d2, format="%Y"), cex.axis=0.8,las=2);  I tried also to plot the dates in a month-Year form:
>> d2<-as.Date((d1[1])+150*(0:20))
>> plot(sdate, var1, type="l", xaxt="n")> axis(side=1, at=0:15, labels=strftime(d2, format="%m-%Y"), cex.axis=0.8,las=2)
> But nothing happened. I cannot understand why it doesn't show anything.
> I have attached the file as well in case you want to have a more clear picture.
> I really appreciate it if you can help me on this.
> Thank you very much in advance.
> Kind regards,Maria
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

More information about the R-help mailing list