Plotting Time Series Data Using ggplot2 & plotly Packages In R

Hi. This is a short page on how to plot time series data in R using the ggplot2 and plotly packages. (I no longer like base R for plots.) I have recently worked on refreshing my time series knowledge and skills (in R). The contents below is stuff I have played around with through trial and error.

The airpass Dataset 

In here I am using the airpass dataset found in the faraway package in R. Load the three packages in R as follows:

We put the airpass dataset into a variable called air_data. The head() function previews the first six rows of the data while str() gives the dimensions of the dataset  and the variable types.

(The years for the dataset is from 1949 to the end of 1960 instead of 1949 to 1951 stated in the help section of the dataset.)

The variable pass represents the number of passengers in thousands and year is the year in decimal form.

In the year column, the dates are in decimal form where 49 stands for 1949. The next line adds 1900 to each value in the year column.

Here is a check:

The column names at the moment are not very great. These column names can be renamed using colnames():

A Time Series Plot Using ggplot2

The ggplot2 package in R is quite helpful when it comes to plotting time series data. I have the year in the x-axis and the number of passengers in the y-axis.

It appears that the number of passengers increase steadily over time from 1949 to 1961. This increase in passengers is probably from an increase in population and/or rising (disposable) incomes. Also, there is some seasonality where there are cyclical growths and decays.

A Time Series Plot Using plotly

Another data visualization package is plotly. Instead of plus signs, plotly uses the pipe operator %>%.

This plotly plot does look a bit cleaner and there are more x-axis ticks for the year. The plotly package is somewhat new to me. I would need to play around with plotly a little bit more.

Leave a Reply