Final HW

Code:

#=================================
#   Bai cuoi khoa NK
#=================================


rm(list = ls())

library(ggplot2)
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(stringr)
library(tidyr)
library(showtext)
## Loading required package: sysfonts
## Loading required package: showtextdb
library(grid)

read.csv("C:\\Users\\nguye\\Downloads\\r15_day1\\globalterrorismdb_0718dist.csv") -> gt1

gt1  %>% 
  select(iyear, nkill, nkillter, country_txt, region_txt)  %>% 
  filter(iyear >= 2000, iyear <= 2014) -> gt1


gt1 %>% 
  mutate(teamcountry = case_when(country_txt == "Nigeria" ~ "Nigeria",
                                 country_txt == "Iraq" ~ "Iraq",
                                 region_txt  %in% c("North America", "Western Europe", "Australasia & Oceania") ~ 
                                   "Western countries",
                                 country_txt %in% c("Syria","Afghanistan","Pakistan") ~ 
                                   "Syria, Afghanistan & Pakistan",
                                 TRUE ~ "Rest of the world")) %>%
  mutate(nkillterA= replace_na(nkillter, 0)) %>% 
  mutate(nkillA= replace_na(nkill, 0))  %>% 
  mutate(ntotalkill = nkillA + nkillterA)  %>%
  group_by(iyear, teamcountry) %>% 
  summarise(totaldeath = sum(ntotalkill)/1700) %>% 
  ungroup() -> gt2
## `summarise()` has grouped output by 'iyear'. You can override using the
## `.groups` argument.
teamcountry_mat <- c("Rest of the world", 
                     "Western countries", 
                     "Syria, Afghanistan & Pakistan", 
                     "Nigeria", 
                     "Iraq")

gt2$teamcountry <- factor(gt2$teamcountry, levels = teamcountry_mat)



iyear_mat <-  as.character(2000: 2014)
label_fx <- case_when( iyear_mat %in% c("2000") ~ iyear_mat,
                      TRUE ~ str_sub(iyear_mat, start = 3, end = 4)) -> label_fx

label_fy <- c(seq(0,30,5), "35")


ubun_font <- "Ubuntu"
font_add_google(name = ubun_font, family = ubun_font)
showtext_auto()


gt2  %>% 
  ggplot(aes(x= iyear, y= totaldeath, fill = teamcountry)) +
  geom_area() +
  scale_x_continuous(breaks = 2000:2014, expand = c(0, 0.5), labels = label_fx) +
  scale_y_continuous(breaks = seq(0, 35, 5), expand = c(0, 1), labels = label_fy,limits = c(0,35), position = "right")+
  theme(axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text  = element_text(size = 22),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.y = element_line(color = "grey", size = 1),
        panel.background = element_blank(),
        plot.background = element_blank(),
        plot.caption = element_text(size = 20, hjust = 0),
        plot.title = element_text(size = 24, face = "bold", hjust = 0.05),
        plot.subtitle = element_text(size = 22, hjust = 0.05),
        legend.position = c(0.2, 0.71),
        legend.title = element_blank(),
        legend.text = element_text(size = 20)) +
  scale_fill_manual(values = c("Rest of the world" = "#00a4dc",
                               "Western countries" = "#00526d",
                               "Syria, Afghanistan & Pakistan" = "#7b2713",
                               "Nigeria" = "#eca288",
                               "Iraq" = "#f15a40"),
                    breaks = c("Iraq" ,
                               "Nigeria",
                               "Syria, Afghanistan & Pakistan",
                               "Western countries",
                               "Rest of the world")) +
  labs(title = " Global deaths from terrorism",
       subtitle = "  '000",
       caption = "Source: START, IEP")
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
grid.rect(x = 0.015, y = 0.91, hjust = 1, vjust = 0, gp = gpar(fill = "#e5001c", lwd = 0))
grid.rect(x = 1, y = 1 - 0.005, hjust = 1, vjust = 0,  gp = gpar(fill = "#e5001c", lwd = 0))