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(gapminder)
## Warning: package 'gapminder' was built under R version 4.4.2
# Data Wrangling
# Select the relevant columns - country, continent, year, lifeExp, gdpPercap, and pop
gapminder_selected <- gapminder %>%
select(country, continent, year, lifeExp, gdpPercap, pop)
# Filter the data for the years 2000 and onwards
gapminder_filtered <- gapminder_selected %>%
filter(year >= 2000)
# Create a new column for GDP (Gross Domestic Product) by multiplying population with GDP per capita
gapminder_transformed <- gapminder_filtered %>%
mutate(GDP = pop * gdpPercap)
# Table Output
# Generate a summary table showing the top 10 countries with the highest average life expectancy from 2000 onwards
life_exp_summary <- gapminder_transformed %>%
group_by(country) %>%
summarise(avg_lifeExp = mean(lifeExp, na.rm = TRUE)) %>%
arrange(desc(avg_lifeExp)) %>%
head(10)
print("Top 10 Countries with Highest Average Life Expectancy (2000 Onwards)")
## [1] "Top 10 Countries with Highest Average Life Expectancy (2000 Onwards)"
print(life_exp_summary)
## # A tibble: 10 × 2
## country avg_lifeExp
## <fct> <dbl>
## 1 Japan 82.3
## 2 Hong Kong, China 81.9
## 3 Switzerland 81.2
## 4 Iceland 81.1
## 5 Australia 80.8
## 6 Sweden 80.5
## 7 Italy 80.4
## 8 Spain 80.4
## 9 Israel 80.2
## 10 Canada 80.2
plot_gdp_lifeexp <- ggplot(gapminder_transformed, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point(alpha = 0.6) +
scale_x_log10() + # Log scale for GDP per capita
labs(title = "GDP per Capita vs Life Expectancy (2000 Onwards)",
x = "GDP per Capita (log scale)",
y = "Life Expectancy",
color = "Continent") +
theme_minimal()
print(plot_gdp_lifeexp)
life_exp_trend <- gapminder_transformed %>%
group_by(year, continent) %>%
summarise(avg_lifeExp = mean(lifeExp, na.rm = TRUE))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
plot_lifeexp_trend <- ggplot(life_exp_trend, aes(x = year, y = avg_lifeExp, color = continent)) +
geom_line(size = 1) +
labs(title = "Trend of Average Life Expectancy by Continent (2000 Onwards)",
x = "Year",
y = "Average Life Expectancy",
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.
print(plot_lifeexp_trend)
Key Findings: 1. The countries with the highest average life expectancy since 2000 are dominated by high-income nations.. There is a positive relationship between GDP per capita and life expectancy, with wealthier countries generally experiencing higher life expectancy.. Over the years, all continents have shown an upward trend in life expectancy, with some continents experiencing faster improvements than others.. The visualizations demonstrate the disparities in GDP and life expectancy across continents, highlighting the development gaps.