我正在尝试使用here提供的脚本进行“ barchart竞赛”,但是我很难累积表示这些数字:
该gif显示的是每天的前n个累积数字,但没有结转到前一天的前n个。
创建此代码的代码:
library(tidyverse)
library(gganimate)
cas <- read_csv("https://raw.githubusercontent.com/pembletonc/blog/master/content/post/casualities.csv")
cas_clean <- cas %>%
select(age_text, date_of_death, rank, Division, regiment, cemeterymemorial) %>%
mutate(date_of_death = as.Date(dmy(date_of_death )),
age = as.numeric(age_text)) %>%
complete(date_of_death = seq.Date(from = min(.$date_of_death), to = max(.$date_of_death), by = "day")) %>%
group_by(regiment, date_of_death) %>%
tally %>%
mutate(cumulative_deaths = cumsum(n)) %>%
ungroup()
gap <- cas_clean %>%
group_by(date_of_death) %>%
mutate(rank = min_rank(-cumulative_deaths)*1,
cumulative_deaths_rel = cumulative_deaths/cumulative_deaths[rank==1],
cumulative_deaths_lbl = paste0(" ", cumulative_deaths)) %>%
filter(rank <=10) %>%
ungroup()
p <- ggplot(gap, aes(rank, group = regiment,
fill = as.factor(regiment), color = as.factor(regiment))) +
geom_tile(aes(y = cumulative_deaths/2,
height = cumulative_deaths,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(regiment, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(y=cumulative_deaths, label = cumulative_deaths_lbl, hjust = 0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
labs('{closest_state}', x = "", y = "Cumulative Deaths", caption = "Sources: Project44 Casualty Data") +
theme(plot.title = element_text(hjust = 0, size = 22),
axis.ticks.y = element_blank(), # These relate to the axes post-flip
axis.text.y = element_blank(), # These relate to the axes post-flip
plot.margin = margin(1,1,1,4, "cm")) +
transition_states(date_of_death, transition_length = 4, state_length = 1) +
ease_aes('cubic-in-out')
animate(p, 200, fps = 10, duration = 40, width = 800, height = 600, renderer = gifski_renderer("gganim.gif"))
关于如何确保几天内累计的任何想法?
谢谢!