XỠlý dữ liệu đầu và o
##
## 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(ggplot2)
library(stringr)
library(tidyr)
readr::read_csv("D:\\5.R DATA\\test\\globalterrorismdb_0718dist.csv")->glo1## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 181691 Columns: 135
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (55): approxdate, resolution, country_txt, region_txt, provstate, city, ...
## dbl (75): eventid, iyear, imonth, iday, extended, country, region, latitude,...
## lgl (5): gsubname3, weaptype4, weaptype4_txt, weapsubtype4, weapsubtype4_txt
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glo1 %>%
select(2,8,9,11,99,101) %>%
filter(iyear>= 2000, iyear <=2014) ->glo1
coalesce(glo1$nkill, 0) -> nkill1
coalesce(glo1$nkillter, 0) ->nkillter1
glo1 %>%
mutate(total = nkill1+nkillter1) -> glo1
glimpse(glo1)## Rows: 72,403
## Columns: 7
## $ iyear <dbl> 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000…
## $ country <dbl> 139, 139, 92, 1003, 182, 19, 92, 1003, 209, 185, 185, 19, …
## $ country_txt <chr> "Namibia", "Namibia", "India", "Kosovo", "Somalia", "Bangl…
## $ region_txt <chr> "Sub-Saharan Africa", "Sub-Saharan Africa", "South Asia", …
## $ nkill <dbl> 0, 0, 1, 0, 6, NA, 0, 0, 0, 0, 0, NA, NA, NA, NA, 0, NA, 0…
## $ nkillter <dbl> 0, 0, NA, 0, 0, NA, 0, 0, 0, 0, 0, NA, NA, NA, NA, 0, NA, …
## $ total <dbl> 0, 0, 1, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
Tạo nhóm
West_region <- c("North America","Western Europe","Australasia & Oceania")
glo1 %>%
filter(region_txt == West_region) -> West_countries## Warning: There was 1 warning in `filter()`.
## ℹ In argument: `region_txt == West_region`.
## Caused by warning in `region_txt == West_region`:
## ! longer object length is not a multiple of shorter object length
West_countries %>%
pull(country_txt) -> west_countries_1
glo1 %>%
mutate(groupCountry = case_when(country_txt == "Iraq" ~ "Iraq",
country_txt %in% c("Nigeria") ~ "Nigeria",
country_txt %in% c("Syria","Afghanistan","Pakistan") ~
"Syria, Afghanistan & Pakistan",
country_txt %in% west_countries_1 ~ "Western countries",
TRUE ~ "Rest of the world")) -> global_2
global_2 %>%
group_by(iyear, groupCountry) %>%
summarise(death = sum(total)/1000) %>%
ungroup()->df_global## `summarise()` has grouped output by 'iyear'. You can override using the
## `.groups` argument.
x <- c("Rest of the world","Western countries","Syria, Afghanistan & Pakistan",
"Nigeria","Iraq")
c("2000", "01","02","03","04","05","06","07","08","09","10","11","12","13","14") ->label_on_x
df_global$groupCountry <- factor(df_global$groupCountry,levels = x)Vẽ biểu đồ
## Loading required package: sysfonts
## Loading required package: showtextdb
library(grid)
font_main <- "Ubuntu"
font_add_google(name = font_main, family = font_main)
showtext_auto()
library(hrbrthemes)## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
ggplot(df_global, aes(x = iyear, y = death, fill = groupCountry)) +
geom_area(color = "white", size =1) +
scale_fill_manual(values = c("Rest of the world" = "#00a4dc",
"Western countries" = "#00526d",
"Syria, Afghanistan & Pakistan" = "#7b2713",
"Nigeria" = "#eca288",
"Iraq" = "#f15a40"))+
labs(title = " Global deaths from terrorism",
subtitle = " '000",
caption = "Source: START, IEP",
x = NULL, y = NULL) +
theme(axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(hjust = c(0, 0.5, 0.5, 0.5, 1), size = 20),
axis.text.y = element_text(size = 20),
legend.position = c(0.2, 0.71),
legend.title = element_blank(),
legend.text = element_text(size = 20),
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_rect(fill = "white"),
plot.background = element_rect(fill = "white")) +
scale_x_continuous(breaks = 2000:2014, labels = label_on_x, expand = c(0, 1)) +
scale_y_continuous(breaks = seq(0, 50, 5), expand = c(0, 0), position = "right") +
guides(fill = guide_legend(reverse = TRUE))+
theme(plot.title = element_text(size = 40, face = "bold"))+
theme(plot.subtitle = element_text(colour = "grey30",size = 20, hjust = 0))+
theme(plot.caption = element_text(color = "grey30",size = 20,hjust = 0))## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Vectorized input to `element_text()` is not officially supported.
## ℹ Results may be unexpected or may change in future versions of ggplot2.
## 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.001, y = 1, width = 2, height = 0.005,
just = c("left", "top"), gp = gpar(fill = "red", col = "red"))
grid.rect(x = 0.001, y = 1, width = 0.5*0.025, height = 0.085, just = c("left", "top"),
gp = gpar(fill = "red", col = "red"))