# “Exploring Global Development Trends with Gapminder Data”

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.2
## 
## 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(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.2
# Focus on data from the year 2000 and later
clean_gapminder <- gapminder %>%
  filter(year >= 2000) %>%
  mutate(total_gdp = gdpPercap * pop) %>%
  select(country, continent, year, lifeExp, gdpPercap, pop, total_gdp)


# Grouped by continent 
summary_table <- clean_gapminder %>%
  group_by(continent) %>%
  summarise(
    avg_lifeExp = mean(lifeExp, na.rm = TRUE),
    avg_gdpPercap = mean(gdpPercap, na.rm = TRUE),
    total_population = sum(pop)
  ) %>%
  arrange(desc(avg_lifeExp))

# Print table
print(summary_table)
## # A tibble: 5 × 4
##   continent avg_lifeExp avg_gdpPercap total_population
##   <fct>           <dbl>         <dbl>            <dbl>
## 1 Oceania          80.2        28374.         48004776
## 2 Europe           77.2        23383.       1164322398
## 3 Americas         73.0        10145.       1748643946
## 4 Asia             70.0        11324.       7413756030
## 5 Africa           54.1         2844.       1763263608
# Scatter plot: GDP per capita vs. Life Expectancy
ggplot(clean_gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
  geom_point(alpha = 0.7) +
  scale_x_log10() +  # Log scaling for better visualization
  labs(
    title = "Life Expectancy vs. GDP Per Capita (2000-2007)",
    x = "GDP Per Capita (Log Scale)",
    y = "Life Expectancy"
  ) +
  theme_minimal() +
  theme(legend.position = "bottom")

# Bar chart: Population by Continent
pop_summary <- clean_gapminder %>%
  group_by(continent) %>%
  summarise(total_population = sum(pop))

ggplot(pop_summary, aes(x = continent, y = total_population, fill = continent)) +
  geom_bar(stat = "identity") +
  labs(
    title = "Total Population by Continent (2000-2007)",
    x = "Continent",
    y = "Total Population"
  ) +
  theme_minimal() +
  scale_y_continuous(labels = scales::comma)  

# Faceted scatter plot
ggplot(clean_gapminder, aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point(alpha = 0.6) +
  scale_x_log10() +
  facet_wrap(~continent, scales = "free") +
  labs(
    title = "Life Expectancy vs. GDP Per Capita by Continent (2000-2007)",
    x = "GDP Per Capita (Log Scale)",
    y = "Life Expectancy"
  ) +
  theme_minimal()