Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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")

How it works...

In our original example involving plotting air pollution data in the last recipe, we only formatted the date/time vector to pass as a x argument to plot(), but the axis labels were chosen automatically by R as the...