Load libraries
library(dplyr)
library(ggplot2)
library(gapminder)
library(scales)
Check data
?gapminder
dim(gapminder)
## [1] 1704 6
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
Plot population for the US
usa <- gapminder %>% filter(continent == "Americas", country == "United States")
ggplot(usa, aes(x = year, y = pop)) +
geom_line(color = "#0099f9", size = 1) +
geom_point(color = "#0099f9", size = 5) +
## label the point, nudge need to related to the y value
geom_label(aes(label = round(pop/1e6, 0)), nudge_y = 20e6, label.size = 0.1) +
theme_classic() +
labs(title = "Average life expectancy in US",
subtitle = "Data from 1952 to 2007",
caption = "Source: Gapminder dataset",
x = "Year",
y = "Population") +
theme(plot.title = element_text(color = "#0099f9", size = 20, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 12, face = "bold", hjust = 0.5),
plot.caption = element_text(face = "italic", hjust = 0),
axis.title.x = element_text(size = 16, face = "bold"),
axis.title.y = element_text(size = 16, face = "italic"),
# add a border to the whole plot
plot.background = element_rect(color = "black", fill = NA, size = 1)) +
#use limits to expand y axis to get more space in the up and bottom
expand_limits(y = c(125000000, 325000000)) +
# add unit to y label, 1e-6 means y/1e6
scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))

Multiple lines on the same chart
north <- gapminder %>% filter(continent == "Americas", country %in% c("United States", "Canada", "Mexico"))
ggplot(north, aes(x = year, y = lifeExp, group = country)) +
geom_line(aes(color = country), size = 1) +
geom_point(aes(color = country), size = 5) +
theme_classic()
