Share the Gallery !Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

The ggplot2 library allows to plot time series really efficiently with R. Let’s start by loading library and building an example dataset:

What’s important is to check that R understood that your date column is on the date format. To verify, use the str() function:

If the column is not recognized as a date, you need to convert it with the as.Date function. Here are a few examples showing how it works. Basically, you provide your column and explain to R in which format the date is provided.

This table explains how to provide the date format. It has been found on the amazing Quick-R website that gives more details.

Once your column is on the date format, ggplot will display it on a quite convenient way:

Then you can decide which format to use to write the date on the X axis:

And decide how often to write the date and add minor axis:

Two more tips: 1/ if date takes to much place: angle it. 2/ You can select a period of time giving the first and last date of the period.

Not what you are looking for ? Make a new search !



Leave a Reply

Be the First to Comment!

Notify of