4

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

在 R gganimate 中做动画时如何保留以前的数据层?

在 gganimate 中保留积分

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

考虑以下数据。我想建立一个累积堆积条形图,使用我的 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。

可以将数据子集为 、 和 的 3 个不同的 df c(2000)c(2000,2001)然后c(2000,2001,2002)在创建新的 states 列后 rbind,但这似乎是一种非常老套的方法。

有没有更简洁的方法来使用 gganimate 内置的工具显示累积数据?

4

2 回答 2

2

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

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)

在此处输入图像描述

于 2018-10-17T23:04:52.593 回答
0

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

# 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
于 2021-05-28T20:40:44.637 回答