library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite)
##
## Присоединяю пакет: 'jsonlite'
##
## Следующий объект скрыт от 'package:purrr':
##
## flatten
library(httr)
library(lubridate)
library(svglite)
library(readxl)
dividends_excel <- read_excel("moex_dividends_wide.xlsx")
dividends_excel <- dividends_excel %>%
mutate(date = as.Date(date))
# Преобразуем к годам
dividends_yearly <- dividends_excel %>%
mutate(year = year(date)) %>%
select(-date) %>%
group_by(year) %>%
summarise(across(everything(), ~sum(., na.rm = TRUE))) %>%
ungroup()
# --- Агрегируем дивиденды по годам ---
dividends_yearly <- dividends_excel %>%
mutate(date = as.Date(date),
year = year(date)) %>%
select(-date) %>%
group_by(year) %>%
summarise(across(everything(), ~sum(., na.rm = TRUE))) %>%
ungroup()
# --- Преобразуем в long формат для ggplot ---
dividends_long <- dividends_yearly %>%
pivot_longer(
cols = -year,
names_to = "company",
values_to = "dividend"
) %>%
mutate(
payout_status = case_when(
is.na(dividend) ~ "Нет данных",
dividend > 0 ~ "Да",
TRUE ~ "Нет"
)
)
# --- Строим бинарный график ---
p_company_year <- ggplot(dividends_long, aes(x = year, y = reorder(company, company), fill = payout_status)) +
geom_tile(color = "white", linewidth = 0.25) +
scale_fill_manual(
values = c("Да" = "darkblue", "Нет" = "lightblue", "Нет данных" = "grey80"),
name = "Статус"
) +
scale_x_continuous(breaks = sort(unique(dividends_long$year))) +
labs(
title = "Дивидендный статус компаний по годам",
x = "Год",
y = "Компания"
) +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(face = "bold", size = 15),
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid = element_blank()
)
# --- Сохраняем график ---
ggsave(
"~/Desktop/company_year_payout_status.svg",
plot = p_company_year,
width = 12,
height = 10,
device = svglite::svglite,
bg = "white"
)
# --- Просмотр графика ---
p_company_year
