# load tidyverse
library(tidyverse)
library(gapminder)
library(scales)
# load dataset
df <- gapminder
# scatterplot
df %>%
filter(year == 2007) %>%
ggplot(aes(lifeExp, gdpPercap, color = continent)) +
geom_point(size = 5, alpha = 0.6) +
scale_y_continuous(label = unit_format(unit = "K", scale = 1e-3, sep = "")) +
labs(title = 'Life expectancy vs GDP in 2007',
x = '',
y = '',
color = '') +
theme_light() +
theme(legend.position = 'bottom')
The higher the GDP, the higher the life expectancy.
# bar plot
df %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarise(avg_lifeexp = mean(lifeExp)) %>%
ggplot(aes(continent, avg_lifeexp, fill = continent)) +
geom_col(show.legend = FALSE) +
labs(title = 'Average life expectancy by continent in 2007',
x = '',
y = '') +
theme_light()
The highest average life expectancy was in Oceania and Europe.
# line graph
df %>%
group_by(continent, year) %>%
summarise(avg_lifeexp = mean(lifeExp)) %>%
ggplot(aes(as.factor(year), avg_lifeexp, group = continent, color = continent)) +
geom_line(linewidth = 2) +
labs(title = 'Average life expectancy 1952-2007',
x = '',
y = '',
color = '') +
theme_light() +
theme(legend.position = 'bottom')
The average life expectancy has increased across all continents.