Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

Table of Contents (11 chapters)
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating graphs with dates


Sometimes, you may wish to create a time series graph that involves dates along the horizontal axis. We can create such graphs using qplot. Let's try plotting a time series graph. We use the built-in economics dataset (see http://docs.ggplot2.org/current/economics.html) and plot the population against the date. Let's see the first six rows using head():

head(economics)

Here are the first six rows of the data:

      date    pce   pop    psavert  uempmed unemploy
1 1967-06-30 507.8 198712     9.8     4.5     2944
2 1967-07-31 510.9 198911     9.8     4.7     2945
3 1967-08-31 516.7 199113     9.0     4.6     2958
4 1967-09-30 513.3 199311     9.8     4.9     3143
5 1967-10-31 518.5 199498     9.7     4.7     3066
6 1967-11-30 526.2 199657     9.4     4.8     3018

Now we look at the last six rows using the following command:

tail(economics)

The output is as follows:

We can see that the economics dataset runs from the year 1967 to 2007 and contains dates in a particular...