Mục tiêu: Tạo ra area plot sao cho giống nhất với https://www.economist.com/graphic-detail/2015/11/18/the-plague-of-global-terrorism
Updated with minor change
#--------------------------
# Project: Terrorism
# Author: Minh
#--------------------------
rm(list = ls())
#-------------------
# Preparation
#-------------------
library(tidyverse)
library(showtext)
library(grid)
data <- readr::read_csv("C:\\bivnn_FTU\\Cased_R\\Terrorism\\globalterrorismdb_0718dist.csv")
#Adding font
font_add_google("Ubuntu Condensed", "ubuntu")
showtext_auto()
#------------------------
# Data Cleaning
#------------------------
#case/deaths
#country: Iraq, Nigeria, Syria&Afghan&Pakis, western countries, ROW -> country, country_txt, region, region_txt
#Year iyear 2000 -> 2014
#Keep related years, countries, nkill variables
data %>%
filter(iyear %in% c(2000:2014)) %>%
select(country_txt, region_txt, iyear, nkill, nkillter) %>%
mutate(legend = case_when(country_txt == "Iraq" ~ "Iraq",
country_txt == "Nigeria" ~ "Nigeria",
country_txt %in% c("Syria", "Afghanistan", "Pakistan") ~ "Syria, Afghanistan & Pakistan",
region_txt %in% c("North America", "Western Europe", "Australasia & Oceania") ~ "Western countries",
TRUE ~ "Rest of the world"))-> data_legend
# Calculation
data_legend %>%
mutate(nkill = replace_na(nkill, 0)) %>%
mutate(nkillter = replace_na(nkillter, 0)) %>%
mutate(totalkill = nkill + nkillter) %>%
group_by(iyear, legend) %>%
summarise(sum_totalkill = sum(totalkill) / 1000,
sum_nkill = sum(nkill)/1000 ) %>%
ungroup() -> data2
# Preparation for plotting
fill_values <- c("Iraq" = "#f15a40",
"Nigeria" = "#eca289",
"Syria, Afghanistan & Pakistan" = "#7b2713",
"Western countries" = "#00526d",
"Rest of the world" = "#00a4dc")
legend_order <- c("Iraq" ,
"Nigeria",
"Syria, Afghanistan & Pakistan",
"Western countries",
"Rest of the world")
legend_fill_order <- c("Rest of the world",
"Western countries",
"Syria, Afghanistan & Pakistan",
"Nigeria",
"Iraq")
# Factoring legend
data2$legend <- factor(data2$legend, levels = legend_fill_order)
#Labels for x
as.character(2000:2014) -> iyear_x
case_when(iyear_x %in% c("2000") ~ iyear_x,
TRUE ~ str_sub(iyear_x, start = 3, end = 4)) -> iyear_labelx
#------------------------
# Data Visualization
#------------------------
data2 %>%
ggplot(aes(x = iyear, y = sum_totalkill, fill = legend)) +
geom_area() +
scale_x_continuous(breaks = 2000:2014, expand = c(0, 0.5), labels = iyear_labelx) +
scale_y_continuous(expand = c(0, 0), labels = seq(0, 60, 10), breaks = seq(0, 60, 10), limit = c(0, 60), position = "right") +
scale_fill_manual(values = fill_values, breaks = legend_order) +
theme(
panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = 'grey60'),
legend.position = c(0.15, 0.8),
legend.title = element_blank(),
legend.text = element_text(family = "ubuntu", size = 22),
legend.key.height = unit(10, "pt"),
legend.key.width = unit(20, "pt"),
plot.caption = element_text(hjust = 0.017, family = "ubuntu", size = 22), #
plot.margin = margin(10, 20, 10, 20, unit = "pt"),
plot.title = element_text(family = "ubuntu", face = "bold", size = 28),
plot.subtitle = element_text(family = "ubuntu", size = 22),
axis.text = element_text(family = "ubuntu", color = "black", size = 22),
axis.title = element_blank(),
axis.ticks.y = element_blank()
) +
labs(
title = "Global deaths from terrorism",
subtitle = "'000",
caption = "Source: START, IEP"
)
grid.rect(x = 0.012, y = 0.945, hjust = 1, vjust = 0, gp = gpar(fill = "#e5001c", col = "#e5001c"))
grid.rect(x = 1, y = 0.999, hjust = 1, vjust = 0, gp = gpar(fill = "#e5001c", col = "#e5001c"))