Skip to content

Instantly share code, notes, and snippets.

@tukachev
Created October 5, 2022 07:18
Show Gist options
  • Save tukachev/1a66106cc24b8678a87c3398a21af9d3 to your computer and use it in GitHub Desktop.
Save tukachev/1a66106cc24b8678a87c3398a21af9d3 to your computer and use it in GitHub Desktop.
Доля нефтегазовых доходов в федеральном бюджете России по годам
library(tidyverse)
# library(readxl)
library(scales)
library(openxlsx)
# fedbud <- read_excel("fedbud_month.xlsx",
# sheet = "месяц", skip = 2)
url <-
"https://minfin.gov.ru/common/upload/library/2022/08/main/fedbud_month.xlsx"
fedbud <- read.xlsx(url, sheet = "месяц", startRow = 3)
fedbud <- fedbud %>%
mutate(income = paste(`X1`, `РАЗДЕЛ.I`)) %>%
slice(1:13) %>%
select(-`X1`,-`РАЗДЕЛ.I`,-contains("**")) %>%
pivot_longer(!income, names_to = "date", values_to = "income_rub") %>%
mutate(
date = lubridate::as_date(as.numeric(date), origin = "1899-12-30"),
year = lubridate::year(date),
month = lubridate::month(date)
)
fedbud %>%
filter(
month == 12 &
(
income == "1.1. Нефтегазовые доходы" |
income == "1.2. Ненефтегазовые доходы"
)
) %>%
mutate(income = factor(
income,
levels = c("1.2. Ненефтегазовые доходы",
"1.1. Нефтегазовые доходы"),
labels = c("Ненефтегазовые",
"Нефтегазовые")
)) %>%
group_by(year) %>%
mutate(sum_income = sum(income_rub)) %>% ungroup() %>%
mutate(
percent = round((income_rub / sum_income), 2),
text_color = ifelse(income == "Ненефтегазовые", "gray20", "brown")
) %>%
ggplot(aes(
fill = income,
y = percent,
x = year,
order = income
)) +
geom_bar(position = "fill", stat = "identity") +
geom_text(
aes(label = paste0(percent * 100, "%"), colour = income),
position = position_stack(vjust = 0.5),
size = 4.5,
show.legend = FALSE
) +
scale_color_manual(values = c("gray60", "#372917")) +
scale_x_continuous(breaks = seq(2011, 2022, 1), expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_manual(values = c("gray90", "#d1b490")) +
labs(title = "C 2015 года доля нефтегазовых доходов\nв федеральном бюджете России сокращается",
subtitle = "На июнь 2022 года доля нефтегазовых доходов составляет 45%",
caption = "Данные: Минфин России, август 2022\nВизуализация: Юрий Тукачев, октябрь 2022") +
theme(
text = element_text(family = "Open Sans", size = 18),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.margin = margin(25, 25, 10, 25),
plot.subtitle = element_text(size = 14, color = "gray50"),
plot.caption = element_text(
size = 12,
color = "gray50",
margin = margin(20, 0, 0, 0)
),
plot.title.position = "plot",
panel.background = element_blank(),
legend.position = "top",
legend.title = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank()
)
ggsave(
"budjet.png",
dpi = 300,
width = 5,
height = 5,
scale = 1.5
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment