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
lubridate package is indeed your best friend to
deal with the date format, and
ggplot2 allows to plot it efficiently. The
is also considered to build stunning interactive charts. Note that this
online course is dedicated to timeseries analysis with R.
Dateformat? You will ♥ the
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
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 (
Note: this course teaches how to deal with dates and times more in depth.
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.
Annotation allows to highlight main features of a chart. Learn how to add text, circles, lines and more.
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.
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).
The most basic time sery visual you can build with the dygraphs library. Explanation about possible input formats.
Add confidence interval around interactive line chart for time series with dygraphs.
dygraph package offers zooming, hovering, minimaps and much more. Try it on the example below!
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
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
Dual Y axis line chart
Add a second Y axis for the second sery. See how chart conclusion gets different and wrong.
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 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.