Animate ggplot Time Series Plot with a Sliding Window using R
Last Updated :
23 Jul, 2025
Animating time series data with a sliding window in R is a visually engaging way to represent changes over time, especially in trends or forecasts. The gganimate package builds on the ggplot2 framework to create smooth, animated visualizations, and by using a sliding window approach, we can focus on a rolling subset of data. In this article, we will walk through the steps of animating a time series plot with a sliding window in R Programming Language.
Make sure to install the necessary packages:
library(ggplot2)
library(gganimate)
library(dplyr)
Step 1: Simulating a Time Series Dataset
To demonstrate the sliding window animation, we will generate a time series dataset using the seq() function for time points and rnorm() for random normal values representing observations.
R
# Create a simulated time series dataset
set.seed(123)
n <- 100
time <- seq(as.Date("2022-01-01"), by = "day", length.out = n)
value <- cumsum(rnorm(n, mean = 0.2, sd = 1)) # Random walk
time_series_data <- data.frame(time, value)
head(time_series_data)
Output:
time value
1 2022-01-01 -0.3604756
2 2022-01-02 -0.3906531
3 2022-01-03 1.3680552
4 2022-01-04 1.6385636
5 2022-01-05 1.9678513
6 2022-01-06 3.8829163
- time: Date values representing time points.
- value: Randomly generated values representing the observations at each time point.
Step 2: Visualizing the Time Series Plot
First, let’s create a simple static ggplot2 line plot to visualize the entire time series.
R
# Basic time series plot
ggplot(time_series_data, aes(x = time, y = value)) +
geom_line(color = "blue") +
labs(title = "Time Series Plot",
x = "Time",
y = "Value") +
theme_minimal()
Output:
Visualizing the Time Series PlotThis static plot gives us an overview of the time series data. Now, let’s animate it with a sliding window effect.
Step 3: Adding a Sliding Window
We will use the gganimate package to animate the plot by showing only a sliding window of data that changes over time. Let’s define a window size of 20 observations. The sliding window will only show these observations at any given frame in the animation.
R
# Define the window size
window_size <- 20
# Create a new column to indicate frame number for the animation
time_series_data <- time_series_data %>%
mutate(frame = row_number()) # Frame number corresponds to row number
Step 4: Creating the Animated Plot
We will create an animated plot where only a subset of data within the sliding window is displayed at each frame.
R
# Create the animated time series plot with a sliding window
animated_plot <- ggplot(time_series_data, aes(x = time, y = value)) +
geom_line(color = "blue") +
labs(title = 'Time Series Animation: {frame_time}',
x = "Time",
y = "Value") +
theme_minimal() +
# Animate the sliding window by showing only the last 'window_size' points
transition_time(frame) +
view_follow(fixed_y = TRUE) +
# Limit the number of points shown in each frame
enter_fade() +
exit_fade() +
shadow_mark(past = TRUE, future = FALSE, alpha = 0.2)
# Render the animation
animate(animated_plot, nframes = n - window_size, fps = 10)
Output:
Rendering [============================================>-------] at 8.7 fps ~ eta: 1s`
ℹ Do you need to adjust the group aesthetic?
Rendering [=============================================>------] at 8.7 fps ~ eta: 1s`
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?
Rendering [==============================================>-----] at 8.7 fps ~ eta: 1s`
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?
Rendering [===============================================>----] at 8.7 fps ~ eta:
ℹ Do you need to adjust the group aesthetic?
Rendering [================================================>---] at 8.7 fps ~ eta:
ℹ Do you need to adjust the group aesthetic?
Rendering [================================================>---] at 8.7 fps ~ eta:
ℹ Do you need to adjust the group aesthetic?
Rendering [=================================================>--] at 8.7 fps ~ eta:
ℹ Do you need to adjust the group aesthetic?
Rendering [==================================================>-] at 8.7 fps ~ eta: 0s`
ℹ Do you need to adjust the group aesthetic?
`geom_line()`: Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?
# A tibble: 80 × 7
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 gif 480 480 sRGB FALSE 0 72x72
2 gif 480 480 sRGB TRUE 0 72x72
3 gif 480 480 sRGB TRUE 0 72x72
4 gif 480 480 sRGB TRUE 0 72x72
5 gif 480 480 sRGB TRUE 0 72x72
6 gif 480 480 sRGB TRUE 0 72x72
7 gif 480 480 sRGB TRUE 0 72x72
8 gif 480 480 sRGB TRUE 0 72x72
9 gif 480 480 sRGB TRUE 0 72x72
10 gif 480 480 sRGB TRUE 0 72x72
Animate ggplot Time Series Plot with a Sliding Window using Rtransition_time(): This function is used to transition between frames based on the frame column, which represents each point in time.view_follow(fixed_y = TRUE): Ensures that the y-axis limits follow the values within the window. fixed_y = TRUE ensures that the y-axis limits stay consistent across frames.shadow_mark(): This function highlights the trailing points within the sliding window, giving a smooth fading effect for points outside the window.enter_fade() and exit_fade(): Controls how points appear and disappear in the animation.
The resulting animation will show a sliding window of 20 points moving along the time axis.
Conclusion
Animating a time series plot with a sliding window using gganimate provides an insightful way to visualize trends in your data over time. In this guide, we covered how to:
- Create a static time series plot using
ggplot2. - Animate the plot with a sliding window using
gganimate.
Explore
Introduction
Fundamentals of R
Variables
Input/Output
Control Flow
Functions
Data Structures
Object Oriented Programming
Error Handling
File Handling