library(ggplot2)
library(gapminder)
library(dplyr)
##
## 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(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ tibble 3.1.8 ✔ purrr 0.3.4
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
Here I am cleaning the gapminder data set
gapminder_cleaned <- gapminder %>%
filter(year == "2007") %>%
mutate(
pop2 = pop + 1,
continent = case_when(
continent == "Oceania" ~ "Asia",
TRUE ~ as.character(continent)
) %>% as.factor %>%
fct_relevel("Asia", "Americas", "Europe", "Africa", "Oceania")
)
## Warning: 1 unknown level in `f`: Oceania
I am going to build a plot in order to visualize the association between life expectancy and income in different continents around the world.
ggplot(data = gapminder_cleaned, aes(x = gdpPercap, y = lifeExp)) +
annotate("text", x = 4000, y = 45, hjust = 0.5,
size = 85, color = "#999999",
label = "2007", alpha = .3,
family = "Helvetica Neue") +
annotate("text", x = 28200, y = 2,
label = "per person (GDP/capita, PPP$ inflation-adjusted)",
size = 2.8, color = "#999999") +
geom_point(aes(size = pop, color = continent)) +
geom_point(aes(size = pop2), color = "black", shape = 21) +
scale_x_log10(breaks = c(250, 500, 1000, 2000,
4000, 8000, 16000, 32000, 64000, 128000)) +
scale_y_continuous(breaks = seq(0, 90, by = 10)) +
scale_color_manual(values = c("#F15772", "#7EEB03",
"#FBE700", "#54D5E9")) +
scale_size_continuous(range = c(1, 20)) +
labs(
x = "Income",
y = "Life expectancy"
) +
theme_minimal() +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_line(size = 0.2,
color = "#e5e5e5"),
axis.title.y = element_text(margin = margin(r = 15),
size = 11,
family = "Helvetica Neue Light"),
axis.title.x = element_text(margin = margin(t = 15),
size = 11,
family = "Helvetica Neue Light"),
axis.text = element_text(family = "Helvetica Neue Light"),
axis.line = element_line(color = "#999999",
size = 0.2)
) +
coord_cartesian(ylim = c(4.1, 85))