Plotting Time Series data using ggplot2

September 30, 2010

(This article was first published on Software for Exploratory Data Analysis and Statistical Modelling, and kindly contributed to R-bloggers)

There are various ways to plot data that is represented by a time series in R. The ggplot2 package has scales that can handle dates reasonably easily.

Fast Tube
Fast Tube by Casper

As an example consider a data set on the number of views of the you tube channel ramstatvid. A short snippet of the data is shown here:

> head(yt.views)
        Date Views
1 2010-05-17    13
2 2010-05-18    11
3 2010-05-19     4
4 2010-05-20     2
5 2010-05-21    23
6 2010-05-22    26

The ggplot function is used by specifying a data frame and the aes maps the Date to the x-axis and the number of Views to the y-axis.

ggplot(yt.views, aes(Date, Views)) + geom_line() +
  scale_x_date(format = "%b-%Y") + xlab("") + ylab("Daily Views")

The axis labels for the Date variable are created with the scale_x_date function where the format is specified as a Month/Year combination with the %b and %Y formatting strings. The graph that is produced is shown here:

Time Series Example

Time Series Plot Example with ggplot2 package

Other useful resources are provided on the Supplementary Material page.

To leave a comment for the author, please follow the link and comment on their blog: Software for Exploratory Data Analysis and Statistical Modelling. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , , , , , , ,

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)