knitr::opts_chunk$set(echo = TRUE)
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.2
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
# Filtering data for 2007 and mutating to add GDP in billions
selected_data <- gapminder %>%
filter(year == 2007) %>%
select(country, continent, gdpPercap, lifeExp, pop) %>%
mutate(gdpBillions = (gdpPercap * pop) / 1e9)
# Display the first few rows
head(selected_data)
## # A tibble: 6 × 6
## country continent gdpPercap lifeExp pop gdpBillions
## <fct> <fct> <dbl> <dbl> <int> <dbl>
## 1 Afghanistan Asia 975. 43.8 31889923 31.1
## 2 Albania Europe 5937. 76.4 3600523 21.4
## 3 Algeria Africa 6223. 72.3 33333216 207.
## 4 Angola Africa 4797. 42.7 12420476 59.6
## 5 Argentina Americas 12779. 75.3 40301927 515.
## 6 Australia Oceania 34435. 81.2 20434176 704.
# Summarizing average GDP per capita and life expectancy by continent
summary_table <- selected_data %>%
group_by(continent) %>%
summarise(
avg_gdpPercap = mean(gdpPercap, na.rm = TRUE),
avg_lifeExp = mean(lifeExp, na.rm = TRUE),
total_gdpBillions = sum(gdpBillions, na.rm = TRUE)
) %>%
arrange(desc(avg_gdpPercap))
# Printing the summary table
summary_table
## # A tibble: 5 × 4
## continent avg_gdpPercap avg_lifeExp total_gdpBillions
## <fct> <dbl> <dbl> <dbl>
## 1 Oceania 29810. 80.7 807.
## 2 Europe 25054. 77.6 14795.
## 3 Asia 12473. 70.7 20708.
## 4 Americas 11003. 73.6 19418.
## 5 Africa 3089. 54.8 2380.
# Scatter plot of GDP vs Life Expectancy
scatter_plot <- ggplot(selected_data, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point(size = 3, alpha = 0.7) +
scale_x_log10() +
labs(
title = "GDP per Capita vs Life Expectancy (2007)",
x = "GDP per Capita (log scale)",
y = "Life Expectancy",
color = "Continent"
) +
theme_minimal()
# Display the plot
scatter_plot
# Bar plot of average life expectancy by continent
bar_plot <- ggplot(summary_table, aes(x = continent, y = avg_lifeExp, fill = continent)) +
geom_bar(stat = "identity", show.legend = FALSE) +
labs(
title = "Average Life Expectancy by Continent (2007)",
x = "Continent",
y = "Average Life Expectancy"
) +
theme_minimal()
# Display the plot
bar_plot
#1. There is a clear positive correlation between GDP per capita and life expectancy across countries. Higher GDP per capita often corresponds to better living standards, leading to longer life expectancy.
#2. Europe has the highest average GDP per capita and life expectancy, showcasing strong economic and healthcare systems. The Americas also exhibit similar trends but with slightly lower averages.
#3. Africa, while having the lowest average GDP per capita, demonstrates significant diversity in life expectancy. This indicates varying access to healthcare and differing socioeconomic conditions across the continent.
#4. Asia contributes the highest total GDP in billions due to its large population size, even though the GDP per capita is moderate compared to other continents.
#5. The scatter plot reinforces the disparity between continents, with Africa clustered at the lower GDP and life expectancy spectrum, while Europe and the Americas are at the higher end.
#These findings emphasize the link between economic prosperity and health outcomes. Countries with higher GDP per capita often invest more in healthcare infrastructure, education, and public services, resulting in better quality of life and longer lifespans.
#The disparities between continents highlight the need for targeted policies to address socioeconomic and healthcare inequalities, particularly in lower-income regions.
#This analysis underscores the importance of sustainable economic development as a pathway to improving global health outcomes.