knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
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(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.2
library(knitr)
## Warning: package 'knitr' was built under R version 4.4.2
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.2
# Load Gapminder dataset
data <- gapminder::gapminder
# Filter for years after 1990 and select relevant columns
filtered_data <- data %>%
filter(year >= 1990) %>%
select(country, continent, year, lifeExp, gdpPercap, pop)
# Add a new column: GDP in billions
filtered_data <- filtered_data %>%
mutate(gdp_in_billions = (gdpPercap * pop) / 1e9)
# Display first few rows of the filtered data
head(filtered_data)
## # A tibble: 6 × 7
## country continent year lifeExp gdpPercap pop gdp_in_billions
## <fct> <fct> <int> <dbl> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1992 41.7 649. 16317921 10.6
## 2 Afghanistan Asia 1997 41.8 635. 22227415 14.1
## 3 Afghanistan Asia 2002 42.1 727. 25268405 18.4
## 4 Afghanistan Asia 2007 43.8 975. 31889923 31.1
## 5 Albania Europe 1992 71.6 2497. 3326498 8.31
## 6 Albania Europe 1997 73.0 3193. 3428038 10.9
# Create summary table
summary_table <- filtered_data %>%
group_by(continent) %>%
summarise(
avg_lifeExp = mean(lifeExp, na.rm = TRUE),
avg_gdpPercap = mean(gdpPercap, na.rm = TRUE)
) %>%
arrange(desc(avg_gdpPercap))
# Display the table
kable(summary_table, caption = "Average Life Expectancy and GDP per Continent (1990 Onwards)")
Average Life Expectancy and GDP per Continent (1990
Onwards)
| Oceania |
78.89862 |
25416.797 |
| Europe |
76.07362 |
20726.141 |
| Asia |
68.63002 |
10280.225 |
| Americas |
71.68725 |
9306.236 |
| Africa |
53.83978 |
2587.247 |
ggplot(filtered_data, aes(x = year, y = lifeExp, color = continent)) +
geom_line(stat = "summary", fun = "mean") +
labs(
title = "Life Expectancy Trends (1990 Onwards)",
x = "Year",
y = "Life Expectancy",
color = "Continent"
) +
theme_minimal()

ggplot(filtered_data, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
geom_point(alpha = 0.7) +
scale_x_log10() +
labs(
title = "GDP Per Capita vs. Life Expectancy (1990 Onwards)",
x = "GDP Per Capita (log scale)",
y = "Life Expectancy",
color = "Continent",
size = "Population"
) +
theme_minimal()

ggplot(filtered_data, aes(x = continent, y = pop, fill = continent)) +
geom_boxplot() +
scale_y_log10() +
labs(
title = "Population Distribution by Continent",
x = "Continent",
y = "Population (log scale)"
) +
theme_minimal()

filtered_gdp <- filtered_data %>%
group_by(continent, year) %>%
summarise(total_gdp = sum(gdp_in_billions, na.rm = TRUE))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
ggplot(filtered_gdp, aes(x = year, y = total_gdp, color = continent)) +
geom_line(size = 1) +
labs(
title = "Total GDP by Continent Over Time",
x = "Year",
y = "Total GDP (in billions)",
color = "Continent"
) +
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.

# Findings and Interpretation
#
# 1. Life Expectancy Trends Over Time:
# - Life expectancy has shown a consistent upward trend across all continents since 1990.
# - Europe and Oceania have the highest average life expectancies,
# while Africa lags behind, although it has shown significant improvement over time.
#
# 2. GDP Per Capita vs. Life Expectancy:
# - A positive correlation exists between GDP per capita and life expectancy.
# - Higher GDP per capita tends to be associated with better health outcomes (higher life expectancy).
# - Africa shows lower GDP and life expectancy, whereas Europe and the Americas cluster
# in the high GDP-high life expectancy range.
#
# 3. Population Distribution by Continent:
# - Asia has the largest population, as seen in the wide range of values in the boxplot.
# - Africa also has significant population variance, indicating the presence of both
# very populous and smaller countries.
# - Oceania has the smallest populations overall.
#
# 4. Total GDP by Continent Over Time:
# - Asia has shown the most rapid increase in total GDP since 1990, driven by high
# population and increasing per capita income.
# - Europe and the Americas have steady GDP growth, while Africa lags behind but
# shows gradual improvement.
#
# Interpretation:
# - Economic development directly impacts life expectancy, but it’s not the sole factor.
# Improvements in healthcare, education, and governance also play significant roles,
# especially in Africa and Asia.
# - The disparity between continents highlights the need for targeted policies to promote
# sustainable growth in less-developed regions, such as Africa.
# - Population distribution influences GDP trends, with populous regions like Asia
# seeing rapid economic growth, while sparsely populated regions like Oceania show slower growth.