# Load required libraries
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.3
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(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
# Preview the dataset
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num [1:1704] 28.8 30.3 32 34 36.1 ...
##  $ pop      : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
summary(gapminder)
##         country        continent        year         lifeExp     
##  Afghanistan:  12   Africa  :624   Min.   :1952   Min.   :23.60  
##  Albania    :  12   Americas:300   1st Qu.:1966   1st Qu.:48.20  
##  Algeria    :  12   Asia    :396   Median :1980   Median :60.71  
##  Angola     :  12   Europe  :360   Mean   :1980   Mean   :59.47  
##  Argentina  :  12   Oceania : 24   3rd Qu.:1993   3rd Qu.:70.85  
##  Australia  :  12                  Max.   :2007   Max.   :82.60  
##  (Other)    :1632                                                
##       pop              gdpPercap       
##  Min.   :6.001e+04   Min.   :   241.2  
##  1st Qu.:2.794e+06   1st Qu.:  1202.1  
##  Median :7.024e+06   Median :  3531.8  
##  Mean   :2.960e+07   Mean   :  7215.3  
##  3rd Qu.:1.959e+07   3rd Qu.:  9325.5  
##  Max.   :1.319e+09   Max.   :113523.1  
## 
# Define country groups
south_asia <- c("Bangladesh", "India", "Pakistan", "Nepal", "Sri Lanka",
                "Bhutan", "Afghanistan", "Maldives", "Myanmar", "Iran")

developed <- c("United States", "Germany", "United Kingdom", "Japan", "France")

underdeveloped <- c("Ethiopia", "Haiti", "Mozambique", "Chad", "Burundi")

selected_countries <- c(south_asia, developed, underdeveloped)

# Filter data
filtered_data <- gapminder %>%
  filter(country %in% selected_countries)



# Create static ggplot
p <- ggplot(filtered_data, aes(x = year, y = gdpPercap, color = country)) +
  geom_point(alpha = 0.6, size = 2) +  # Add points with alpha and size
  geom_smooth(method = "lm", se = FALSE) +  # Add regression line
  labs(title = "GDP per Capita (1952–2007)",
       subtitle = "20 Countries (Developing, Developed, Underdeveloped)",
       x = "Year", y = "GDP per Capita") +
  theme_minimal()

p
## `geom_smooth()` using formula = 'y ~ x'

# Convert static plot to interactive
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# Create animation
anim <- ggplot(filtered_data, aes(x = year, y = gdpPercap, color = country)) +
  geom_point(aes(size = pop), alpha = 0.7) +
  scale_size(range = c(2, 10)) +
  labs(title = "GDP per Capita Over Time",
       subtitle = "Year: {frame_time}",
       x = "Year", y = "GDP per Capita") +
  transition_time(year) +
  ease_aes('linear') +
  theme_minimal()

# Render animation
animate(anim, renderer = gifski_renderer())

anim_save("gdp_animation.gif", animation = anim)
knitr::include_graphics("gdp_animation.gif")