【发布时间】:2019-03-22 16:44:37
【问题描述】:
我无法找到之前发布的足以回答此问题的问题。在之前的帖子中,已接受的答案使用 shadow_mark 来保持先前渲染的图层的持久性。
How to keep previous layers of data while doing animation in R 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。
可以将数据子集为 c(2000)、c(2000,2001) 和 c(2000,2001,2002) 的 3 个不同的 df,然后在创建新的 states 列后进行 rbind,但这似乎是一种非常老套的方法。
有没有更简洁的方法来使用 gganimate 内置的工具显示累积数据?
【问题讨论】:
标签: r animation ggplot2 gganimate