Created
February 7, 2022 18:24
-
-
Save aliaksandrkazlou/40c578a08b1ea144d9a2c2ecbe099dc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Libraries -------------------- | |
library(WDI) | |
library(data.table) | |
library(ggplot2) | |
#### Data -------------------- | |
# uv data is from here | |
# https://apps.who.int/gho/data/view.main.35300 | |
dt_uv <- fread('~/tg_channel/data/uv_by_country_2004.csv') | |
setnames(dt_uv, colnames(dt_uv), c("country", "uv")) | |
# Download WDI data on suicide | |
dt_wdi <- WDI(country = "all", | |
indicator = "SH.STA.SUIC.P5", | |
start = "2004", | |
end = "2004", | |
extra = FALSE, | |
cache = NULL) | |
# merge date, ignore different names | |
dt_main <- dt_uv[as.data.table(dt_wdi), nomatch=0, on = "country"] | |
# some data cleaning | |
setnames(dt_main, "SH.STA.SUIC.P5", "suicide_rate") | |
dt_main <- na.omit(dt_main, cols=c("suicide_rate", "uv")) | |
dt_main <- dt_main[order(suicide_rate,decreasing=TRUE),] | |
#### Plots -------------------- | |
p1 <- ggplot(dt_main, aes(x=uv, y=suicide_rate)) + | |
geom_point(color='red') + | |
geom_smooth(method=lm, color='gray', se = FALSE) + | |
theme_minimal(base_size = 9) + | |
theme(plot.subtitle = element_text(color = "#666666"), | |
plot.caption = element_text(color = "#AAAAAA")) + | |
labs(title = "Колькасць суіцыдаў на 100.000 супраць ультрафіялетавага (UV) выпраменьвання", | |
subtitle = "Дадзеныя за 2004 год (па краінах)", | |
caption = "by Aliaksandr Kazlou — akazlou.github.io") | |
ggsave(plot = p1, "d_suicide_p1.png", dpi = "retina") | |
p2 <- ggplot(rbind(dt_main[, head(.SD, 20)], dt_main[, tail(.SD, 20)]), | |
aes(x=reorder(country, suicide_rate), y=uv)) + | |
geom_bar(stat="identity") + | |
geom_vline(xintercept = 20.5, color="red") + | |
theme_minimal(base_size = 7) + | |
theme(plot.subtitle = element_text(color = "#666666"), | |
plot.caption = element_text(color = "#AAAAAA")) + | |
labs(title = "Ультрафіялетавае (UV) выпраменьванне", | |
subtitle = "20 найбольш/найменш суіцыдальных краін", | |
caption = "by Aliaksandr Kazlou — akazlou.github.io") + | |
coord_flip() | |
ggsave(plot = p2, "d_suicide_p2.png", dpi = "retina") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment