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

Adding vertical markers to indicate specific time events


We might wish to indicate specific points of importance or measurements in a time series, where there is a significant event or change in the data. In this recipe, we will learn how to add vertical markers using the abline() function.

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 take our air pollution time series example again and draw a red vertical line on Christmas day, 25/12/2003:

plot(air$nox~as.Date(air$date,"%d/%m/%Y %H:%M"),type="l",
xlab="Time", ylab="Concentration (ppb)",
main="Time trend of Oxides of Nitrogen")

abline(v=as.Date("25/12/2003","%d/%m/%Y"))

How it works...

As we have seen before in the recipe introducing abline(), we drew a vertical line in the example by setting the v argument to the date we want to mark. We specified 25/12/2003 as the X coordinate by using the as...