Seasonal Plots in R

A seasonal plot is a graphical representation used to visualize and analyze seasonal patterns in time series data. It helps identify recurring patterns, such as monthly or quarterly fluctuations, within a dataset.

For example, let’s consider a retail store that sells winter clothing. The store collects sales data for sweaters over the past few years. By creating a seasonal plot of sweater sales data, the store can observe if there is a consistent increase in sales during the colder months (fall and winter) compared to the warmer months (spring and summer). This visualization can help the store better understand the seasonal demand for sweaters and plan their inventory accordingly.

Types of Seasonal Plots

  1. Time Series Plot: This is a basic plot that displays the data over time. While not inherently a seasonal plot, it forms the foundation for understanding seasonal patterns and trends.
  2. Seasonal Subseries Plot: This plot breaks down the time series data into subseries based on each season (e.g., months or quarters). It allows for a more detailed examination of seasonal variations within the data.
  3. Seasonal Decomposition Plot: Seasonal decomposition separates the time series into its constituent components: trend, seasonal, and residual. The seasonal plot visualizes the seasonal component, making it easier to identify seasonal patterns independent of other trends.

Creating a Time Series Object

  • Before we start plotting time series data, we need to convert our data into a time series object. This allows us to work with time-based data and perform various time series analyses.
  • We can use functions like ts() and xts() to create time series objects in R Programming Language. The ts() function is part of the base R package, while the xts() function is available in the xts package.
  • These functions require specifying the data vector, start date, and frequency of observations to create the time series object.
R
# Load required packages
library(xts)
library(lubridate)

# Sample data
data <- c(10, 20, 15, 25, 30)
dates <- as.Date(c("2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04",
                   "2024-01-05"))

# Create time series object using ts()
ts_data <- ts(data, start = c(year(dates[1]), month(dates[1])), frequency = 12)

# Create time series object using xts()
xts_data <- xts(data, order.by = dates)
xts_data

Output:

           [,1]
2024-01-01 10
2024-01-02 20
2024-01-03 15
2024-01-04 25
2024-01-05 30

Visualizing Seasonality

  • Once we have our time series object, we can visualize seasonality in various ways, such as by year, month, or other relevant time units.
  • Visualizing seasonality helps us understand recurring patterns in our data and identify any trends or irregularities.

Typical Patterns Indicating Seasonality

  1. Regular Fluctuations: Seasonal patterns often exhibit regular fluctuations over a specific period, such as daily, weekly, monthly, or yearly.
  2. Consistent Peaks and Troughs: Data may consistently peak or trough during certain times of the year, indicating seasonal highs and lows.
  3. Repeated Patterns: Seasonality can manifest as repeated patterns across multiple cycles, showing similar shapes and amplitudes.
  4. Predictable Variations: Variations occur predictably within a seasonal cycle, with similar magnitudes and timings each year.

Creating Basic Seasonal Plots

  • After understanding how to create a time series object and visualize seasonality, we can proceed to create basic seasonal plots in R.
  • We can use popular plotting libraries like ggplot2, lattice, or fpp3 to create these plots. These libraries offer flexible options for customizing the appearance and style of the plots.
  • We’ll use popular plotting libraries like ggplot2 to create basic seasonal plots.
R
# Load required packages
library(ggplot2)

# Example with ggplot2
# Assuming you have a time series object 'ts_data' or 'xts_data'

# Convert xts object to data frame
df <- data.frame(date = index(xts_data), value = coredata(xts_data))

# Plotting using ggplot2
ggplot(df, aes(x = date, y = value)) +
  geom_line() + # Line plot
  labs(title = "Seasonal Plot", x = "Date", y = "Value")

Output:

Seasonal Plots in R

Reading and Interpreting Seasonal Plots

  • Once we’ve created seasonal plots, it’s essential to understand how to interpret them. Seasonal plots help us visualize trends, cycles, and irregularities in our data.
  • By analyzing the patterns in the plots, we can gain insights into the underlying seasonality of our data and make informed decisions based on these insights.

Identifying Trends, Cycles, and Irregularities

  • Trends: Trends indicate long-term directional movements in the data. They can be identified by observing the overall upward or downward movement over an extended period.
  • Cycles: Cycles represent repetitive patterns or fluctuations that occur over a medium-term period, typically longer than seasonality but shorter than trends.
  • Irregularities: Irregularities, or residuals, are random fluctuations that cannot be explained by trends or seasonal patterns. They appear as noise around the trend and seasonal components.

Customizing Seasonal Plots

  • Line Plot: A basic line plot connects data points with straight lines, making it easy to visualize trends and seasonal patterns.
  • Point Plot: Point plots display individual data points without connecting lines, providing a clearer view of the distribution and variability of the data.
  • Bar Plot: Bar plots represent data using bars of different heights, suitable for comparing values across categories or time periods.
  • Area Plot: Area plots fill the area below the line in line plots, providing a visual representation of cumulative values over time.
  • Box Plot: Box plots summarize the distribution of the data, showing the median, quartiles, and outliers, helpful for identifying variability and outliers within seasonal periods.
R
# Customizing the previous ggplot2 plot with a red line
ggplot(df, aes(x = date, y = value, color = "red")) +
  geom_line() +
  labs(title = "Customized Seasonal Plot", x = "Date", y = "Value") +
  theme_minimal()

Output:

Seasonal Plots in R

Finally, we can customize our seasonal plots to better suit our needs and preferences.

  • We can adjust various aspects of the plot, such as colors, labels, and plot types, to enhance readability and convey information effectively.
  • After understanding these concepts, we can proceed to implement them in R to create and analyze seasonal plots using real-world data.

Seasonal Plot Plotting Techniques

1. Seasonal Subseries Plot

The seasonal subseries plot is a specialized plot for visualizing seasonal patterns in time series data. It consists of small multiples, with each panel representing a specific season or month. This plot allows for the comparison of seasonal patterns across different time periods, aiding in the identification of recurring patterns and anomalies.

2. Seasonal Decomposition Plot

The seasonal decomposition plot is used to decompose a time series into its constituent components: trend, seasonal, and residual. This plot helps visualize the underlying trend and seasonal variations present in the data, enabling analysts to better understand the structure and behavior of seasonal patterns over time.

3. Seasonal Heatmap

A seasonal heatmap is a graphical representation of seasonal patterns using color gradients. Each row in the heatmap represents a year, and each column represents a specific time period (e.g., month or week). The intensity of color indicates the magnitude of a particular variable (e.g., temperature) for each time period, allowing for easy identification of seasonal trends and variations.

4. Seasonal Line Plot

A seasonal line plot is similar to a regular time series plot but emphasizes seasonal patterns by aggregating data at regular intervals (e.g., monthly or quarterly). By plotting the mean or median values for each time period over multiple years, seasonal fluctuations become more apparent, facilitating the visualization of seasonal trends and changes over time.

5. Seasonal Boxplot

A seasonal boxplot displays the distribution of a variable across different seasons or months. Each boxplot represents the distribution of the variable for a specific season, allowing for comparison of central tendency, spread, and variability between different seasons. This plot is particularly useful for identifying seasonal outliers and understanding the variability of the data across seasons.

Dataset Link: WeatherHistory

Step 1: Load the required libraries and dataset

  • Load the ggplot2 and lubridate library .
  • Dataset is read from the CSV file located at the specified file path and stored in the variable weather_data.
R
# Load the required libraries
library(ggplot2)
library(lubridate)

# Load the dataset
weather_data <- read.csv("C:/Users/Tonmoy/Downloads/Dataset/weatherHistory.csv")

Step 2: Prepare the data

  • Convert the ‘Formatted Date’ column to POSIXct format (datetime) using the as.POSIXct().
  • Extract the year from the ‘Formatted Date’ column and store it in a new column named ‘Year’.
  • Extract the month from the ‘Formatted Date’ column and store it in a new column named ‘Month’.
R
weather_data$Formatted.Date <- as.POSIXct(weather_data$Formatted.Date, 
                                          format="%Y-%m-%d %H:%M:%S", tz="UTC")
weather_data$Year <- year(weather_data$Formatted.Date)
weather_data$Month <- month(weather_data$Formatted.Date)

Step 3: Create a seasonal plot

  • Use the ggplot() function to create a plot using the weather_data data frame as the data source.
  • Add a line representing temperature for each month and year combination.
  • Add a smooth curve to the plot using locally weighted scatterplot smoothing (LOESS) method.
  • Add labels for x-axis, y-axis, and title of the plot.
  • Apply a minimal theme to the plot.
  • It prints the created seasonal plot to the R console, allowing the user to visually inspect the temperature variations over months.
R
# Create a seasonal plot
seasonal_plot <- ggplot(weather_data, aes(x = Month, 
                                          y = Temperature..C., group = Year)) +
  geom_line(aes(color = factor(Year))) +
  geom_smooth(method = "loess", se = FALSE) +
  labs(x = "Month", y = "Temperature (C)", title = "Seasonal Temperature Plot") +
  theme_minimal()

# Display the plot
print(seasonal_plot)

Output:

Seasonal Plots in R

Seasonal Plot of Humidity

We can plot humidity over months to visualize how humidity varies throughout the year.

R
# Create a seasonal plot of humidity
seasonal_humidity_plot <- ggplot(weather_data, 
                                 aes(x = Month, y = Humidity, group = Year)) +
  geom_line(aes(color = factor(Year))) +
  geom_smooth(method = "loess", se = FALSE) +
  labs(x = "Month", y = "Humidity", title = "Seasonal Humidity Plot") +
  theme_minimal()

# Display the plot
print(seasonal_humidity_plot)

Output:

Seasonal Plots in R

Seasonal Plot of Wind Speed

Plot wind speed over months to observe seasonal variations in wind patterns.

R
# Create a seasonal plot of wind speed
seasonal_wind_plot <- ggplot(weather_data, aes(x = Month, 
                                               y = Wind.Speed..km.h., group = Year)) +
  geom_line(aes(color = factor(Year))) +
  geom_smooth(method = "loess", se = FALSE) +
  labs(x = "Month", y = "Wind Speed (km/h)", title = "Seasonal Wind Speed Plot") +
  theme_minimal()

# Display the plot
print(seasonal_wind_plot)

Output:

Seasonal Plots in R

Seasonal Plot of Visibility

Plot visibility over months to see how visibility changes with seasons.

R
# Create a seasonal plot of visibility
seasonal_visibility_plot <- ggplot(weather_data, 
                                   aes(x = Month, y = Visibility..km., group = Year)) +
  geom_line(aes(color = factor(Year))) +
  geom_smooth(method = "loess", se = FALSE) +
  labs(x = "Month", y = "Visibility (km)", title = "Seasonal Visibility Plot") +
  theme_minimal()

# Display the plot
print(seasonal_visibility_plot)

Output:

Seasonal Plots in R

Conclusion

Seasonal plots provide a straightforward way to visualize and understand recurring patterns within time series data that change over time. By focusing on specific time frames, these plots highlight seasonal variations and track how they evolve over time, enabling analysts to identify differences between various seasons. Whether it’s analyzing sweater sales in a retail store or temperature fluctuations in weather data, seasonal plots help uncover insights that inform decision-making and planning. With the availability of various plotting techniques and tools like R, analysts can easily create and analyze seasonal plots to gain valuable insights into seasonal patterns in their data.