[R] Adding Year-Month-Day to X axis
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Mon May 7 05:54:43 CEST 2018
Hi Greg,
Easy:
plot (x_yyyymmdd, y_duration, type="l", xaxt="n", yaxt="n",
ylim=range(240,480))
abline (h=c(240,270,300,330,360,390,420,450,480,510,540), lty=2,
lwd=1.0, col="grey40")
axis(1,at=x_yyyymmdd,labels=format(x_yyyymmdd,"%Y-%m-%d"))
axis(2,at=seq(240,480,by=60),labels=c("4.00","5.00","6.00","7.00","8.00"))
However, you are only getting every third label. If you want to
display more of them you can use staxlab:
staxlab(1,at=x_yyyymmdd,labels=format(x_yyyymmdd,"%Y-%m-%d"),nlines=3)
OR
staxlab(1,at=x_yyyymmdd,labels=format(x_yyyymmdd,"%Y-%m-%d"),srt=45)
Jim
On Mon, May 7, 2018 at 1:26 PM, Gregory Coats <gregcoats using me.com> wrote:
> Jim,
> Thank you very much!
> How do I use the axis command for side=1 to label the x horizontal axis, in
> the format="%Y-%m-%d” style?
> Greg
>
> y_duration <- c (301.59050, 387.35700, 365.64366, 317.26150, 321.71883,
> 342.44950, 318.95350, 322.33233, 330.60333, 428.99516, 297.82066,
> 258.23166)
> x_yyyymmdd <-as.Date(c ("2018-04-25", "2018-04-26", "2018-04-27",
> "2018-04-28", "2018-04-29", "2018-04-30", "2018-05-01", "2018-05-02",
> "2018-05-03", "2018-05-04", "2018-05-05", "2018-05-06"), format="%Y-%m-%d")
> plot (x_yyyymmdd, y_duration, type="l", xaxt="n", yaxt="n",
> ylim=range(240,480))
> abline (h=c(240,270,300,330,360,390,420,450,480,510,540), lty=2, lwd=1.0,
> col="grey40")
> axis (side=2, at=240, cex.axis=1.0, label="4:00")
> axis (side=2, at=300, cex.axis=1.0, label="5:00")
> axis (side=2, at=360, cex.axis=1.0, label="6:00")
> axis (side=2, at=420, cex.axis=1.0, label="7:00")
> axis (side=2, at=480, cex.axis=1.0, label="8:00")
>
> On May 6, 2018, at 3:52 AM, Jim Lemon <drjimlemon using gmail.com> wrote:
>
> Hi Greg,
> By default, the "axis" function puts the labels on one line and drops
> labels that would overlap. When you have labels that are all the same
> length, this usually results in every second, or third, or fourth
> label being displayed. So you can probably get what you want by not
> using staxlab. However, if you really want to use staxlab, try this:
>
> oddones<-seq(1,length(x_yyyymmdd)-1,by=2)
> staxlab(1,at=x_yyyymmdd[oddones],
> labels=format(x_yyyymmdd,"%Y-%m-%d")[oddones])
>
> It will also work with plain "axis", which is what you seem to want.
>
> Jim
>
>
More information about the R-help
mailing list