Created
July 3, 2023 15:53
-
-
Save tukachev/62f0dfe6478d8bdd0fed86cc2694f4f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
library(tidyverse) | |
library(lubridate) | |
birthdate <- ymd("1978-06-28") | |
# Вычисление количества прожитых полных лет и недель | |
age_years <- | |
floor(as.numeric(difftime(Sys.Date(), birthdate, units = "days")) / 365.25) | |
age_in_weeks <- age_years * 52 | |
# Вычисление количества недель с последнего дня рождения | |
last_birthday <- | |
paste0(year(birthdate) + age_years, "-", format(birthdate, "%m-%d")) | |
weeks_since_last_birthday <- | |
floor(as.numeric(difftime(Sys.Date(), last_birthday, units = "weeks"))) | |
age_all <- (age_in_weeks + weeks_since_last_birthday) / 52 | |
full_weeks <- age_in_weeks + weeks_since_last_birthday | |
# Создаем данные для визуализации | |
weeks <- rep(1:52, 90) # Недели | |
age <- rep(90:1, each = 52) # Возраст (в обратном порядке) | |
age_weeks <- | |
((90 - age) * 52) + weeks | |
# Создаем список цветов (синий для прожитых недель, светло-серый для оставшихся) | |
color_list <- | |
rep(c("#3F51B5", "gray80"), c(full_weeks, length(weeks) - full_weeks)) | |
color_list[full_weeks] <- "red" #текущая неделя | |
# Собираем данные в один датафрейм | |
life_df <- data.frame(age = age, | |
age_weeks = age_weeks, | |
color = color_list) | |
# Создаем график | |
ggplot(life_df, aes(x = weeks, y = rev(age), fill = color)) + | |
geom_tile( | |
linetype = 1, | |
color = "white", | |
linewidth = 0.25 | |
) + | |
scale_fill_identity() + | |
labs(title = "My Life in Weeks", | |
x = "Week of the Year →", | |
y = "← Age", | |
caption = "@weekly_charts") + | |
scale_y_continuous( | |
labels = c(0, 1, seq(5, 90, 5)), | |
expand = c(0, 0), | |
breaks = c(0, 1, seq(5, 90, 5)), | |
trans = "reverse" | |
) + | |
scale_x_continuous( | |
labels = seq(0, 52, 5), | |
expand = c(0, 0), | |
breaks = seq(0, 50, by = 5), | |
position = "top" | |
) + | |
coord_fixed(ratio = 1) + | |
theme( | |
plot.background = element_blank(), | |
plot.title = element_text(hjust = 0.5), | |
plot.caption = element_text(size = 13, color = "gray40"), | |
text = element_text(family = "Sans", size = 24), | |
axis.text = element_text(size = 10, color = "gray40"), | |
axis.title = element_text(size = 16), | |
axis.title.y = element_text(hjust = 1), | |
axis.title.x = element_text(hjust = 0), | |
legend.position = "none", | |
axis.ticks.y = element_blank(), | |
axis.ticks.x = element_blank() | |
) | |
ggsave( | |
"age.png", | |
bg = "white", | |
dpi = 600, | |
width = 5, | |
height = 6, | |
scale = 1.5, | |
) | |
# Сохранение графика в PDF | |
ggsave( | |
filename = "my_plot.pdf", | |
# plot = my_plot, | |
device = cairo_pdf, | |
width = 8.27, # Ширина A4 в дюймах | |
height = 11.69, # Высота A4 в дюймах | |
units = "in", | |
dpi = 600 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment