library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
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(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.3
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(gifski)
## Warning: package 'gifski' was built under R version 4.4.3
library(transformr)
## Warning: package 'transformr' was built under R version 4.4.3

The dataset used is the built-in mtcars dataset in R.

Variables:

A year variable has been added to demonstrate an animated time series.

set.seed(123)
mtcars$year <- rep(1970:1981, length.out = nrow(mtcars))

Static Visualization using ggplot2

This scatter plot visualizes the relationship between horsepower (hp) and fuel efficiency (mpg), colored by the number of cylinders and shaped by the number of gears.

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(aes(color = factor(cyl), shape = factor(gear)), size = 4, alpha = 0.8) +
  geom_smooth(method = "lm", se = FALSE, color = "black", linetype = "dashed") +
  labs(
    title = "Horsepower vs Fuel Efficiency",
    subtitle = "Colored by Cylinders, Shaped by Gear Count",
    x = "Horsepower (hp)",
    y = "Miles Per Gallon (mpg)",
    caption = "Data Source: mtcars dataset"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

This chart reveals a general negative correlation between horsepower and fuel efficiency. Vehicles with higher horsepower tend to have lower mpg. Cylinders and gear count add dimension to this analysis.

Interactive Visualization with Plotly

p <- ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point(aes(color = factor(cyl), text = paste("Model:", rownames(mtcars))), size = 4, alpha = 0.8) +
  geom_smooth(method = "lm", se = FALSE, color = "black", linetype = "dashed") +
  labs(
    title = "Interactive Horsepower vs MPG",
    x = "Horsepower",
    y = "Miles Per Gallon"
  ) +
  theme_minimal()
## Warning in geom_point(aes(color = factor(cyl), text = paste("Model:",
## rownames(mtcars))), : Ignoring unknown aesthetics: text
ggplotly(p, tooltip = c("x", "y", "text"))
## `geom_smooth()` using formula = 'y ~ x'

Animated Visualization with gganimate

mpg_trend <- mtcars %>%
  group_by(year) %>%
  summarise(avg_mpg = mean(mpg))

line_plot <- ggplot(mpg_trend, aes(x = year, y = avg_mpg)) +
  geom_line(color = "#2E86C1", size = 1.2) +
  geom_point(color = "#1F618D", size = 3) +
  labs(
    title = 'Average Fuel Efficiency Over Time',
    subtitle = 'Year: {frame_along}',
    x = 'Year',
    y = 'Average MPG',
    caption = 'Simulated trend using mtcars dataset'
  ) +
  transition_reveal(year) +
  theme_minimal()

line_gif <- gganimate::animate(line_plot, nframes = 60, fps = 10, width = 700, height = 500, renderer = gifski_renderer())
gganimate::anim_save("line_animation.gif", animation = line_gif, path = 'C:/Users/Taimoor Rabbani/Documents/R Workshop')
Line Graph of Average MPG Over Time
Line Graph of Average MPG Over Time