Annotating axis labels in different human-readable time formats

In this recipe, we will learn how to choose the formatting of time axis labels, instead of just using the defaults.

Getting ready

We will only use the basic R functions for this recipe. Make sure that you are at the R prompt and load the openair.csv dataset:

air<-read.csv("openair.csv")

How to do it...

Let's redraw our original example involving plotting air pollution data from the last recipe, but with labels for each month and year pairing:

plot(air$nox~as.Date(air$date,"%d/%m/%Y %H:%M"),type="l", xaxt="n", xlab="Time", ylab="Concentration (ppb)", main="Time trend of Oxides of Nitrogen") xlabels<-strptime(air$date, format = "%d/%m/%Y %H:%M") axis.Date(1, at=xlabels[xlabels$mday==1], format="%b-%Y") ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.