Как структурировать накопленную сумму по дате, ранжировать по дням - PullRequest
2 голосов
/ 15 мая 2019

Я собираюсь устроить "гонку в виде диаграммы", используя предоставленные сценарии здесь , однако у меня возникают трудности с кумулятивным представлением цифр:

enter image description here

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"))

Есть ли у вас какие-либо идеи о том, как обеспечить, чтобы сумма данных также совпадала по дням?

Спасибо!

...