Stat Tistician
Stat Tistician

Reputation: 883

Modify date format on x axis in R?

I want to change the format of my plot in R in month year.

So currently it is displaying the day too, I want to have only month year.

With my data and the R code:

plot(alvdate[1250:1600],c(NA,alvlloss)[1250:1600],type="l",lwd=1,main="",xlab="",ylab="log loss",cex.axis=1.2,cex.lab=1.2,xaxt="n")
axis.Date(1, at = seq(alvdate[1250], alvdate[1600], length.out=20),
        labels = seq(alvdate[1250], alvdate[1600], length.out=20),
        format= "%m/%y", las = 2)

I get the picture

enter image description here

I get the format yyyy-mm-dd but I want to have yyyy-mm so e.g.

2008-01
2008-02

and so on. I alredy set the format to "%m/%y" but this does not work?

Upvotes: 4

Views: 9893

Answers (1)

agstudy
agstudy

Reputation: 121568

You should use format= "%Y-%m", to write something like this :

axis.Date(1, at = seq(alvdate[1250], alvdate[1600], length.out=20),
        format= "%Y-%m", las = 2)

Upvotes: 6

Related Questions