Skip to content

Instantly share code, notes, and snippets.

@tukachev
Created December 4, 2025 18:20
Show Gist options
  • Select an option

  • Save tukachev/28776709b3dc5f9e2115bf125422ac7f to your computer and use it in GitHub Desktop.

Select an option

Save tukachev/28776709b3dc5f9e2115bf125422ac7f to your computer and use it in GitHub Desktop.
library(ggplot2)
library(dplyr)
#статика
df <- data.frame(
category = c("Деньги", "Стабильность", "Уважение\nв обществе"),
value = c(33.33333, 33.33333, 33.33333),
sector_color = c("#FF00FF", "#FFA500", "#7CFC00"),
legend_color = c("red", "blue", "yellow")
)
ggplot(df, aes(x = "", y = value, fill = category)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
scale_fill_manual(values = df$sector_color) +
guides(fill = guide_legend(override.aes = list(fill = df$legend_color))) +
theme_void() +
labs(title = "Чем хороша\nпрофессия датавизера", fill = "") +
theme(
legend.position = "left",
legend.text = element_text(size = 14),
legend.key.size = unit(0.8, "cm"),
plot.margin = margin(20, 20, 20, 20),
plot.title.position = "plot",
plot.title = element_text(
family = "PT Sans",
size = 22,
face = "bold",
hjust = 0.5,
margin = margin(b = 10)
)
)
#анимация
library(ggplot2)
library(dplyr)
library(purrr)
library(magick)
df <- tibble::tibble(
frame = rep(1:4, each = 3),
category = rep(c(
"Деньги", "Стабильность", "Уважение в обществе"
), times = 4),
value = c(33, 34, 33, 40, 35, 25, 20, 40, 40, 40, 15, 45),
sector_fill = c(
"#FF00FF", "#FFA500", "#7CFC00", "red", "#FFA500", "#7CFC00",
"#FF00FF", "blue", "#7CFC00", "#FF00FF", "#FFA500", "yellow"
),
legend_fill = c(
"red", "blue", "yellow", "#FF00FF", "blue", "yellow",
"red", "#FFA500", "yellow", "red", "blue", "#7CFC00"
)
)
plots <- df %>%
split(.$frame) %>%
map( ~ {
sector_named <- setNames(.x$sector_fill, .x$category)
legend_named <- setNames(.x$legend_fill, .x$category)
ggplot(.x, aes(x = "", y = value, fill = category)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
scale_fill_manual(values = sector_named) +
guides(fill = guide_legend(override.aes = list(fill = unname(legend_named)))) +
theme_void() +
labs(title = "Чем хороша\nпрофессия датавизера", fill = "") +
theme(
legend.position = "left",
legend.text = element_text(size = 20),
legend.key.size = unit(0.8, "cm"),
plot.margin = margin(20, 20, 20, 20),
plot.title.position = "plot",
plot.title = element_text(
family = "PT Sans",
size = 26,
face = "bold",
hjust = 0.5,
margin = margin(b = 10)
)
)
})
dir.create("frames", showWarnings = FALSE)
iwalk(plots, ~ {
ggsave(
filename = sprintf("frames/frame_%02d.png", as.integer(.y)),
plot = .x, width = 7, height = 6, dpi = 150, bg = "white"
)
})
#GIF
png_files <- dir("frames", pattern = "\\.png$", full.names = TRUE) %>%
sort()
img_list <- image_read(png_files)
gif <- image_animate(img_list, delay = 250, fps = 10, loop = 0)
image_write(gif, "dataviz.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment