Time Series | the R Graph Gallery (2024)


Time series aim to study the evolution of one or several variables through time. This section gives examples using R. A focus is made on the tidyverse: the lubridate package is indeed your best friend to deal with the date format, and ggplot2 allows to plot it efficiently. The dygraphs package is also considered to build stunning interactive charts.

Struggling with the Date format? You will the lubridate package.

  • Building time series requires the time variable to be at the date format. The first step of your analysis must be to double check that R read your data correctly, i.e. at the date format. This is possible thanks to the str() function:

  • Time Series | the R Graph Gallery (1)
  • Getting this date format can be a pain, and the lubridate package is such a life saver. It offers several function which name are composed by 3 letters: year (y), month (m) and day (d). Example:

  • Time Series | the R Graph Gallery (2)

Time series with ggplot2

ggplot2 offers great features when it comes to visualize time series. The date format will be recognized automatically, resulting in neat X axis labels. The scale_x_data() makes it a breeze to customize those labels. Last but not least, plotly can turn the resulting chart interactive in one more line of code.

Most basic Most basic line chart with R and ggplot2 for time series data visualization
X labels Customize the X axis labels with any date format
Time frame Select the time frame of interest in your input data
Annotation Annotation allows to highlight main features of a chart. Learn how to add text, circles, lines and more.
Dual Y axis A few examples showing how to use the sec.axis argument to add a second Y axis.

Interactive version: plotly

The ggplotly() function of the plotly library makes it a breeze to build an interactive version. Try to hover circles to get a tooltip, or select an area of interest for zooming. Double click to reinitialize.

Get code

Time series with dygraph

The dygraphs package is a html widget. It allows to make interactive time series chart: you can zoom and hover data points to get additional information. Start by reading the chart #316 for quick introduction and input description. Then, the graph #317 gives an overview of the different types of charts that are offered. To go further, check the graph #318 (interactive version below).

Most basic The most basic time sery visual you can build with the dygraphs library. Explanation about possible input formats.
Several series How to display several series on the same chart.
Area chart Build an interactive area chart for time series with dygraphs.
Candlestick Build an interactive Candlestick chart for time series with dygraphs.
Confidence interval Add confidence interval around interactive line chart for time series with dygraphs.
Step plot Build an interactive step plot for time series with dygraphs.

The dygraph package offers zooming, hovering, minimaps and much more. Try it on the example below!

Get code

Heatmap for time series

Heatmaps can be a very good alternative to visualize time series, especially when the time frame you study is repeating, like weeks. Here is a customized example, but visit the heatmap section for more.

Code Heatmap section

Dual Y axis with latticeExtra or ggplot2

Warning: a dual Y axis line chart represents the evolution of 2 series, each plotted according to its own Y scale. This kind of chart must be avoided, since playing with Y axis limits can lead to completely different conclusions. Visit data-to-viz for more info.

Why you should avoid it

Line chart with 2 series A classic line chart with 2 series, only 1 Y axis.
Dual Y axis line chart Add a second Y axis for the second sery. See how chart conclusion gets different and wrong.
Add legend Add a legend to specify what color is linked to what value.
Most basic dual Axis Most basic usage of sec.axis attribute to display a second Y axis.
More style Customize the chart style with title, colors, theme and more.
Mix geoms A dual axis chart mixing a line chart with a barplot.

Bump chart with ggbump

A bump chart is a variation of the parallel coordinate plot. It visualizes the rank order and changes in rank of categorical data over time. It is thus suitable for timeseries.

In R, the ggbump package makes it a breeze to build one as shown in the following examples:

Most basic bump plot How to build a simple bump plot with ggplot2.
Customize bump plot Learn how to change colors and add individual points to a bump plot.
Labels and annotations Discover how to add labels and annotations to a bump plot.

🌐 From the web

The web is full of astonishing R charts made by awesome bloggers. The R graph gallery tries to display some of the best creations and explain how their source code works. If you want to display your work here, please drop me a word or even better, submit a Pull Request!

Lollipop chart for timeseries A very highly customized lollipop chart representing Mario Kart world records. A work by Cédric Scherer for the TidyTuesday challenge.
Line and Area charts by The Economist Learn how to mimick the style of The Economist with libraries like ggplot2, patchwork, grid, ggtext and more.
Line chart with filled intersection This example uses geo-faceting: the dataset is split and plotted using geographical regions. For each charts, 2 lines are displayed and the intersection between both is filled.
Line chart with small multiple A line chart with small multiple where all groups are repeated (but greyed-out) on each pannel.

Related chart types

Line plot

Area

Stacked area

Streamchart

Time Series

Time Series | the R Graph Gallery (2024)

References

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5707

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.