Open In App

Time Series Visualization with ggplot2 in R

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A time series is the series of data points listed in the order timeline i.e. one of the axes in the form of dates, years or months.

Time-series analysis consists of methods for analyzing time-series data in a way to retrieve some meaningful insight from data. It is useful in many industries like financial industries, pharmaceuticals, social media companies, web service providers, research and many more as it helps to predict future events based on trends of past data. 

Visualizing Time Series Data

To better understand our data, we need effective tools to visualize it. The R programming language offers set of visualization tools through the ggplot2 package.

We can use the geom_line() function to visualize the time-series data using a line plot.

Syntax:

ggplot(dataframe , aes(x, y)) + geom_line()

Parameter:

  • dataframe: determines the dataframe variable for plotting chart
  • x: determines the time variable vector
  • y: determines the data variable vector for time vector.

Example: A basic line chart depicting time series data. 

  • set.seed() ensures reproducible random values
  • data.frame() creates the dataset with day labels and values
  • paste0() generates string labels for the day
  • runif() generates random values within a range
  • round() rounds the values to two decimal places
  • ggplot() initializes the plot with data and mappings
  • geom_line() draws the line chart
  • scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) prevents overlapping x-axis label
R
library(ggplot2)

set.seed(123) 

dataframe <- data.frame(
  Date = paste0("Day_", 1:30),
  High = round(runif(30, min = 100, max = 200), 2)
)

ggplot(dataframe, aes(x = Date, y = High, group = 1)) +
  geom_line() +
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE))

Output:

plot
Simple Plot of time series data

Data Label format

Since one of the axis data is in the form of date, we can format it in multiple forms of date. We can use the scale_x_date() function to choose the format displayed on the X-axis.

Syntax:

scale_x_date(date_labels = date-format-identifiers)

Here, date format identifiers are:

IdentifiersExplanationExample
%dday as a number21
%ashort-form weekdayMon
%Along-form weekdayMonday
%mmonth as number10
%bshort-form monthOct
%Blong-form monthOctober
%y2 digit year21
%Y4 digit year2021

Example: A time series using data label identifier to display only month in long-form.

  • data.frame() creates the dataset with dates and values
  • as.Date() generates date objects for the time series
  • runif() adds random variation to the values
  • ggplot() initializes the plot with data and mappings
  • geom_line() draws the line chart
  • scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) prevents overlapping x-axis labels
  • scale_x_date(date_labels = "%B") formats x-axis dates to show full month names
R
library(ggplot2)

dataframe <- data.frame(
  Date = as.Date("2021-10-21") - 0:364,
  High = runif(365) + seq(-140, 224)^2 / 10000
)

ggplot(dataframe, aes(x = Date, y = High, group = 1)) +
  geom_line() +
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) +
  scale_x_date(date_labels = "%B")

Output:

Breaks and Minor breaks

We can create different breakpoints by using minor or major breaks in the plot using the date_breaks and date_minor_breaks argument of the scale_x_date() function of ggplot2.

Syntax:

 scale_x_date(date_breaks, date_minor_breaks)

Example: A basic time series plot with manual date breaks and minor date breaks.

  • data.frame() creates the dataset with dates and values
  • as.Date() generates date objects for the time series
  • runif() adds random variation to the values
  • seq() generates a sequence used to create a cubic trend
  • ggplot() initializes the plot with data and mappings
  • geom_line() draws the line chart
  • scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) prevents overlapping x-axis labels
  • scale_x_date(date_labels = "%B", date_breaks = "1 week") formats x-axis dates to show full month
R
library(ggplot2)

dataframe <- data.frame(
  Date = as.Date("2021-10-21") - 0:364,
  High = runif(365) + seq(-140, 224)^3 / 10000
)

ggplot(dataframe, aes(x = Date, y = High, group = 1)) +
  geom_line() +
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) +
  scale_x_date(date_labels = "%B", date_breaks = "1 week")

Output:

Limit Axis Data

While dealing with large datasets, we might need to focus on a small time frame. To do so we use the limit option of the scale_x_date() function to select a time frame in the data.

Syntax:

scale_x_date(limit)

Example: A plot with limited data from October 2021 to July 2021.

  • data.frame() creates the dataset with dates and values
  • as.Date() generates date objects for the time series
  • runif() adds random variation to the values
  • seq() generates a sequence used to create a quadratic trend
  • ggplot() initializes the plot with data and mappings
  • geom_line() draws the line chart
  • scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) prevents overlapping x-axis labels
  • scale_x_date(limits = c(...)) limits the x-axis to a specific date range
R
library(ggplot2)

dataframe <- data.frame(
  Date = as.Date("2021-10-21") - 0:364,
  High = runif(365) + seq(-140, 224)^2 / 10000
)

ggplot(dataframe, aes(x = Date, y = High, group = 1)) +
  geom_line() +
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) +
  scale_x_date(limits = c(as.Date("2021-09-01"), as.Date("2021-10-21")))

Output:

In this article, we discussed time-series visualization with the ggplot2 package in the R programming Language.


Article Tags :

Similar Reads