Skip to content

Instantly share code, notes, and snippets.

@alexeyknorre
Created April 10, 2018 14:28
Show Gist options
  • Save alexeyknorre/58f3743a884c84c23c332093303a3735 to your computer and use it in GitHub Desktop.
Save alexeyknorre/58f3743a884c84c23c332093303a3735 to your computer and use it in GitHub Desktop.
Snippet deaths-in-russia-2017 with piping
install.packages(c("readxl","rio","dplyr","ggplot2"))
library(dplyr)
library(ggplot2)
rio::import("http://www.gks.ru/free_doc/2017/demo/t3_3.xls") %>%
slice(-c(1:7, 9:11, 13, 14, 17, 21:23, 27, 29, 38)) %>%
select(1:2) %>%
setNames(c("cause","number")) %>%
mutate(cause = gsub("из них от|в том числе от|1)|\\:", "", cause),
number = as.numeric(number)) %>%
ggplot(aes(y = number, x = reorder(cause,number))) +
geom_bar(stat = "identity") +
coord_flip() +
geom_text(aes(label=paste(
substr(number, 0, nchar(number)-3),
"тыс.")), hjust = -0.2) +
labs(x = "",
y = "Число умерших",
title = "Статистика смертности россиян в 2017 году.\nСмерть наступила от...",
caption = paste0("Источник: данные Росстата о причинах",
" смертности россиян в 2017 году\nURL",
": http://www.gks.ru/free_doc/2017/demo/t3_3.xls")) +
ylim(0,600000) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
ggsave(width = 12, height = 6, dpi = 500, filename = "deaths-in-russia-2017.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment