Báo cáo The 2018 Atlas of Sustainable Development Goals (gọi tắt là SDG 2018) của World Bank (WB) có hơn 180 charts và maps được thực hiện gần như hoàn toàn bằng R, ngoại trừ một số bản đồ phức tạp được vẽ bằng ArcGIS.
Series bài viết này sẽ hướng dẫn tái lập lại các charts được thực hiện bằng R trong báo cáo SDG đó của WB. Dưới đây là SDG 1.1 tại trang 2 của báo cáo này:
Còn dưới đây là R codes:
# Clear R environment:
rm(list = ls())
# Load some packages:
library(readxl)
library(dplyr)
library(stringr)
library(ggplot2)
# Load data:
<- read_excel("SDG1_f1_global_dist_histogram.xlsx")
df
# Select years:
<- c(1990, 2013)
years_selected
# Prepare data:
%>%
df filter(regioncode == "WLD") %>%
filter(year %in% years_selected) %>%
select(year, povertyline, people, p_label) %>%
mutate(people = people / 1000) %>%
mutate(p_label = case_when(p_label == ".5-1" ~ "0.5-1",
== "1-1.9" ~ "1-1.90",
p_label == "1.9-4" ~ "1.90-4",
p_label TRUE ~ p_label)) %>%
mutate(p_label_fct = fct_reorder(p_label, povertyline)) %>%
mutate(under1.9 = case_when(povertyline <= 1.9 ~ "Yes", TRUE ~ "No")) -> data_for_income_hist
# Select color for our plot:
<- "#cc0641"
pink_color
<- "#A6A6A6"
grey_color
<- "#cb6a72"
pink_box_color
# Make a draft:
%>%
data_for_income_hist ggplot(aes(x = p_label_fct, y = people, fill = under1.9)) +
geom_col(show.legend = FALSE) +
facet_wrap(~ year, ncol = 1) +
scale_fill_manual(values = c("Yes" = pink_color, "No" = grey_color)) +
theme_minimal() +
theme(plot.margin = unit(c(0.7, 1, 1, 1), "cm")) -> f_hist_poverty
#-----------------------------------------------
# Make the draft more elegant
# https://theloop.worldbank.org/typography
#-----------------------------------------------
library(showtext) # -> Package for using extra fonts.
showtext_auto() # -> Automatically render text.
<- "Ubuntu" # -> Set Work Sans for title.
title_font
font_add_google(name = title_font, family = title_font) # -> Load font
<- "PT Sans"
subtitle_font
font_add_google(name = subtitle_font, family = subtitle_font) # -> Load font
%>%
data_for_income_hist filter(year == 1990) %>%
slice(1) -> df_text_1990
<- "In 1990, 1.9 billion people - or 35 percent\nof the world - live on less than\n$1.9 a day. By 2013, this had fallen to\n769 million - or 10.7 percent of people."
ann_text1
%>%
data_for_income_hist filter(year != 1990) %>%
slice(1) -> df_text_2013
<- "In 2013, 4 billion peopel - over half the world's\npopulation - lived on between $1.90 and $10 a day."
ann_text2
+
f_hist_poverty labs(x = "Income or consumption per day (2011 PPP $)",
title = "Ending extreme poverty is at the heart of the SDG agenda. Between 1990 and 2013\nthe number of people living below $1.90 a day fell by over 1 billion.",
subtitle = "People (billions)",
caption = "Source: World Bank PovcalNet (database). http://iresearch.worldbank.org/PovcalNet/home.aspx\nGraphic Designer: Nguyen Chi Dung") +
theme(panel.grid.minor = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.title.x = element_text(family = title_font, size = 11, color = "grey30", vjust = -1)) +
scale_y_continuous(limits = c(0, 2.3)) +
theme(plot.title = element_text(family = title_font, size = 15, hjust = 0, vjust = 2), plot.title.position = "plot") +
theme(plot.caption = element_text(family = subtitle_font, size = 10.5, color = "grey40", hjust = 0, vjust = -5), plot.caption.position = "plot") +
theme(plot.subtitle = element_text(family = title_font, size = 10, color = "grey20")) +
theme(axis.text = element_text(family = title_font, size = 11, color = "grey40")) +
theme(strip.text = element_text(family = title_font, size = 10.5, color = "grey40", hjust = 0)) +
geom_text(data = df_text_1990, aes(label = ann_text1, x = 6.5, y = 1.6), family = subtitle_font, size = 3.6, color = "grey20") +
geom_text(data = df_text_2013, aes(label = ann_text2, x = 8, y = 1.8), family = subtitle_font, size = 3.6, color = "grey20") +
geom_label(data = df_text_1990,
aes(label = "SDG 1.1", x = 10, y = 2.1),
family = subtitle_font,
fontface = "bold",
size = 3.6,
color = "white",
fill = pink_box_color,
vjust = 0.2,
hjust = 0.2,
show.legend = FALSE)