Plotting a date on the X axis in R

I am trying to make a graph with a date on the x axis with an interval of 1 month and date values ​​for clarity.

r=runif(100) d <- as.Date("2001/1/1") + 70*sort(r) plot(d,r,type="l",xaxt="n") axis.Date(1, at=seq(d[1],d[100],"month"), format="%m/%d/%Y") 

This does not work. I am trying to get something similar to the following graph:

Reference graph

+6
source share
1 answer

It does exactly what you ask for the function.

Three months, three ticks.

 > d[1] [1] "2001-01-01" > d[100] [1] "2001-03-11" 

Try it.

 r=runif(100) d <- as.Date("2001/1/1") + 70*sort(r) plot(d,r,type="l",xaxt="n") axis.Date(1, at = seq(d[1], d[100], length.out=25), labels = seq(d[1], d[100], length.out=25), format= "%m/%d/%Y", las = 2) 

It should be easy to set for week / month / year. You can play with the mar parameter in ?par .

+6
source

Source: https://habr.com/ru/post/885595/


All Articles