# Load necessary libraries
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## 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(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(gganimate)
## Warning: package 'gganimate' 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(gifski)
## Warning: package 'gifski' was built under R version 4.4.3
# Set seed for reproducibility
set.seed(2025)
# Define years and vehicle info
years <- 2010:2024
types <- c("Car", "Bike")
cars <- data.frame(
type = "Car",
brand = c("Toyota", "Toyota", "Honda", "Nissan", "Mitsubishi"),
model = c("Corolla", "Axio", "Civic", "X-Trail", "Pajero")
)
bikes <- data.frame(
type = "Bike",
brand = c("Yamaha", "Honda", "TVS", "Bajaj", "Suzuki"),
model = c("FZS-FI V3", "CB Hornet", "Apache RTR", "Pulsar 150", "Gixxer")
)
vehicles <- rbind(cars, bikes)
# Expand by year
vehicle_data <- expand.grid(
year = years,
model = vehicles$model
)
# Join back vehicle type and brand
vehicle_data <- left_join(vehicle_data, vehicles, by = "model")
# Simulate values
vehicle_data <- vehicle_data %>%
mutate(
units_sold = round(runif(n(),
min = ifelse(type == "Car", 1000, 5000),
max = ifelse(type == "Car", 5000, 20000))),
price_lakh = round(runif(n(),
min = ifelse(type == "Car", 15, 1.5),
max = ifelse(type == "Car", 40, 3.5)), 2),
popularity_index = round(runif(n(), 40, 100), 1)
)
# Preview
head(vehicle_data)
## year model type brand units_sold price_lakh popularity_index
## 1 2010 Corolla Car Toyota 3930 36.02 58.8
## 2 2011 Corolla Car Toyota 2903 36.96 59.2
## 3 2012 Corolla Car Toyota 3057 22.32 66.4
## 4 2013 Corolla Car Toyota 2994 36.36 47.9
## 5 2014 Corolla Car Toyota 4121 38.36 62.0
## 6 2015 Corolla Car Toyota 3017 25.10 77.5
ggplot(vehicle_data, aes(x = year, y = units_sold, color = model)) +
geom_line(size = 1) +
facet_wrap(~type, scales = "free_y") +
labs(title = "Units Sold by Vehicle Model (2010–2024)",
x = "Year", y = "Units Sold") +
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.
pop_plot <- ggplot(vehicle_data, aes(x = year, y = popularity_index, color = model)) +
geom_line(size = 1.2) +
facet_wrap(~type) +
labs(title = "Popularity Index Over Time",
x = "Year", y = "Popularity Index (0–100)") +
theme_minimal()
ggplotly(pop_plot)
# Create the animation object
anim <- ggplot(vehicle_data, aes(x = year, y = price_lakh, size = units_sold, color = model)) +
geom_point(alpha = 0.7) +
facet_wrap(~type) +
labs(title = "Price & Sales Animation: {frame_time}",
x = "Year", y = "Price (Lakh BDT)") +
scale_size(range = c(3, 10)) +
transition_time(year) +
ease_aes("linear") +
theme_minimal()
# Save the animation as a GIF
anim_save("vehicle_price_trend.gif", animation = anim)
# Display the GIF in the HTML output
knitr::include_graphics("vehicle_price_trend.gif")
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## type brand model
## Length:5 Length:5 Length:5
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.