gganimate 0.9.9.9 中的累积图

Cumulative plots in gganimate 0.9.9.9

我找不到之前发布的足以回答此问题的问题。在以前的帖子中,已接受的答案使用 shadow_mark 来保持先前渲染的层的持久性。

在散点图中显示输出时,这是一个不错的解决方法,但它不是累积测量,并且在尝试执行(例如,堆叠条形图)时会失败。

考虑以下数据。我想建立一个累积堆叠条形图,在我的 df 中使用过渡状态。

df <- data.frame(t = c(2000, 2000, 2001, 2001, 2002, 2002), 
                 f = c("y", "n", "y", "n", "y", "n"),
                 x = c("a", "a", "b", "c", "a", "c"),
                 y = c(2,3,5,1,4,8))
> df
     t f x y
1 2000 y a 2
2 2000 n a 3
3 2001 y b 5
4 2001 n c 1
5 2002 y a 4
6 2002 n c 8

我想显示2000年的数据,在下一层我想添加2001年的数据作为上一层的累加。同样,对于下一层,我想将 2002 年的数据添加为 2000 年和 2001 年的累积数据。

这说明了为什么 shadow_mark 不是累积数据的解决方案:

ggplot(df, aes(x=x, y=y, fill=f)) +
geom_col() + labs(x=NULL, y=NULL, fill=NULL, title="{closest_state}") +
transition_states(t, transition_length = 2, state_length = 1) +
shadow_mark() + enter_fade() + exit_shrink() + ease_aes('sine-in-out') + theme_bw() 

添加对 shadow_mark 的调用将无法获得累积图的预期结果。 "a" 总计应为 9。

可以将数据子集化为 c(2000)c(2000,2001)c(2000,2001,2002) 的 3 个不同的 df,然后在创建新的状态列后进行 rbind,但这似乎就像一个非常 hacky 的方法。

是否有更清晰的方法来使用 gganimate 内置的工具来显示累积数据?

您可以在数据中创建一个新列,其中包含每年的相加结果并直接绘制。在下面的代码中,我们使用 cumsum 函数来执行此操作。我们还使用 complete 来确保 fx 的每个组合都有一个 t 行(在这些添加的行中设置 y=0)。如果我们不这样做,当 fx 的某些组合缺少某些年份(t 值)时,累积和将不正确。所有数据转换都是使用 dplyr 管道即时完成的:

library(tidyverse)
library(gganimate)

ggplot(df %>% 
         complete(t, nesting(f, x), fill=list(y=0)) %>% 
         arrange(t) %>%  
         group_by(x,f) %>%
         mutate(y_cum = cumsum(y)), 
       aes(x=x, y=y_cum, fill=f)) +
  geom_col() + 
  labs(x=NULL, y=NULL, fill=NULL, title="{closest_state}") +
  transition_states(t, transition_length = 2, state_length = 1) +
  enter_fade() + ease_aes('sine-in-out') + 
  theme_bw() +
  scale_y_continuous(breaks=0:10)

我发现直方图的诀窍是复制早期的过渡值以确保累积构建。

# packages my mac needs for gganimate to work
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, gganimate, gifski, png)

# vector of values to plot in histogram
sampling_dist_v1 <- rnorm(1e3)

# create a transition sequence variable
init_seq <- c(1, rep(2,10), rep(3,10))

observed_rates <- 
  tibble(
    observed_rate = sampling_dist_v1,
    transition_sequence = c(init_seq, rep(4, length(sampling_dist_v1) - length(init_seq)))
  )

# duplicate earlier entries to ensure animation is cumulative
t_sub_4 <- 
  observed_rates %>% 
  filter(transition_sequence < 4) %>% 
  mutate(transition_sequence = 4)

t_sub_3 <- 
  observed_rates %>% 
  filter(transition_sequence < 3) %>% 
  mutate(transition_sequence = 3)

t_sub_2 <- 
  observed_rates %>% 
  filter(transition_sequence < 2) %>% 
  mutate(transition_sequence = 2)

observed_rates <- 
  bind_rows(
    observed_rates,
    t_sub_4, 
    t_sub_3, 
    t_sub_2
  ) 

# animate
anim <- observed_rates %>% 
  ggplot(aes(x = observed_rate)) + 
  geom_histogram(binwidth = .25, fill = 'blue') + 
  transition_states(
    transition_sequence,
    state_length = 4,
    wrap = FALSE
    )

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/vyJ8m.gif