library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
data(economics_long)
View(economics_long)

Data Description

The built-in dataset economics_long has been used for this project which comes from the ggplot2 package.This dataset contains economic indicators of the United States over time, including variables like: 1) Date: Month of data collection. 2) variable: Type of economic measure(e.g., personal consumption expenditures (pce), total population (pop), personal savings rate (psavert), median duration of unemployment (uempmed), number of unemployed (unemploy). 3) Value: The numeric value for the given measure.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.4.3
library(gifski)
## Warning: package 'gifski' was built under R version 4.4.3
library(paletteer)
## Warning: package 'paletteer' was built under R version 4.4.3

#Static Visualisation with ggplot2

Filter for selected variables

selected_vars <- c("pce", "unemploy")
econ_filtered <- economics_long %>%
  filter(variable %in% selected_vars)

Static plot with manual colors

p<- ggplot(econ_filtered, aes(x = date, y = value, color = variable)) +
  geom_line(alpha = 0.7, size = 1.2) +
  geom_smooth(method = "loess", se = FALSE, linetype = "dashed", size = 0.8) +
  scale_color_manual(values = c(
    "pce" = "#491212FF",      
    "unemploy" = "#F27127FF"
  )) +
  labs(
    title = "Trends in US Economic Indicators Over Time",
    x = "Date",
    y = "Value",
    color = "Indicator"
  ) +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p
## `geom_smooth()` using formula = 'y ~ x'

##Static Plot Description The static plot shows how over the years, personal consumer expenditure and unemployment rose in the US. Alpha was used to make the lines semi-transparent while size was used to emphasize the lines and as a regression trend line geom_smooth was used to undertand the overall trend.

#Interactive Visualisation with plotly

library(plotly)
trends_interactive_plot <- ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
trends_interactive_plot

#Animation with gganimate

###Animated plot for pce (Personal Consumption Expenditure)

# Filter for pce
pce_data <- economics_long %>%
  filter(variable == "pce")

# Create animation
pce_anim <- ggplot(pce_data, aes(x = date, y = value)) +
  geom_line(color = "#CE471CFF", size = 1.2) +
  geom_point(color = "#491212FF", size = 2) +
  labs(title = "Personal Consumption Expenditure Over Time",
       x = "Date",
       y = "PCE Value") +
  theme_minimal() +
  transition_reveal(date)

#Render animation

animate(pce_anim, renderer = gifski_renderer(loop = FALSE))

##Save animation

anim_save("pce_animation.gif", animation = pce_anim)

###Animated plot for unemploy (Number of unemployed)

# Filter for unemploy
unemploy_data <- economics_long %>%
  filter(variable == "unemploy")

# Create animation
unemploy_anim <- ggplot(unemploy_data, aes(x = date, y = value)) +
  geom_line(color = "#ff7f0e", size = 1.2) +
  geom_point(color = "#BF281BFF", size = 2) +
  labs(title = "Number of Unemployed Over Time",
       x = "Date",
       y = "Unemployment") +
  theme_minimal() +
  transition_reveal(date)

# Render animation
animate(unemploy_anim, renderer = gifski_renderer(loop = FALSE))

##Save animation

anim_save("unemploy_animation.gif", animation = unemploy_anim)

###Animated plot for psavert (Personal Savings Rate)

# Filter for psavert
psavert_data <- economics_long %>%
  filter(variable == "psavert")

# Create animation
psavert_anim <- ggplot(psavert_data, aes(x = date, y = value)) +
  geom_line(color = "#491212FF", size = 1.2) +
  geom_point(color = "#CE471CFF", size = 2) +
  labs(title = "Personal Savings Rate Over Time",
       x = "Date",
       y = "Savings Rate (%)") +
  theme_minimal() +
  transition_reveal(date)

# Render animation
animate(psavert_anim, renderer = gifski_renderer(loop = FALSE))

##Save animation

anim_save("psavert_animation.gif", animation = psavert_anim)