gganimate 基于 ID 和时间的动画

问题描述 投票:0回答:1

我对 gganimate 还很陌生,而且我在这方面遇到了很多困难。

这是我当前的动画;出现的每组线对应一个新参与者:

您可能会注意到,我的 x 轴代表两个时间点(事件之前和之后)。因此,我想对每一行进行动画处理,以便数据点首先出现在“之前”,然后跟踪路径/线以达到“之后”值。所有这一切,同时保持我当前拥有的参与者逐个显示。有什么办法可以实现这个目标吗?

这是我当前动画的代码:

library('ggplot2')
library('gifski')
library('gganimate')
library('transformr')

anim <- df %>% ggplot(aes(x = factor(prepost, levels = c("Before", "After")), y = value)) +
  geom_point() +
  geom_line(aes(group = participant), size = 0.8) +
  scale_y_continuous(breaks = seq(10, 50, 5)) +
  scale_color_identity() +
  facet_wrap(~factor(group, levels = c("Positive", "Negative")), ncol = 2) +
  transition_time(participant) +
  shadow_mark(past = T, future = F, size = 0.2, alpha = 0.1) 
animate(anim, fps = 2)

我尝试过使用

transition_reveal()
transition_states()
,但没有结果,我尝试使用我的“prepost”变量,甚至是人为创建的“time”变量进行转换,但一切都失败了。

这是我的数据集的一部分,用于重现我所做的事情(它来自长格式的数据帧):

df <- structure(list(participant = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 
3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 
8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 
17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 
21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 
25, 25, 25, 25), variable = c("POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post", "POS_pre", "NEG_pre", 
"POS_post", "NEG_post"), value = c(24, 23, 26, 13, 
22, 11, 38, 17, 28, 15, 27, 16, 31, 16, 40, 10, 30, 20, 40, 12, 
23, 11, 26, 10, 36, 19, 37, 15, 34, 10, 43, 14, 26, 10, 26, 10, 
32, 10, 38, 17, 35, 10, 33, 11, 24, 10, 17, 15, 33, 12, 34, 30, 
18, 10, 30, 13, 27, 13, 28, 13, 23, 10, 34, 19, 28, 15, 32, 23, 
26, 25, 36, 20, 27, 11, 39, 15, 29, 10, 35, 10, 18, 10, 22, 13, 
30, 12, 23, 11, 24, 12, 22, 11, 28, 10, 24, 13, 29, 16, 31, 27
), group = c("Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative", 
"Positive", "Negative", "Positive", "Negative", "Positive", "Negative"
), prepost = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), levels = c("Before", 
"After"), class = "factor")), row.names = c(NA, -100L), class = "data.frame")

非常感谢您的时间和关注!

r ggplot2 tween gganimate
1个回答
1
投票

我认为您需要事先进行一些数据操作才能获得您正在寻找的动画。时间变量应该在“之前”和“之后”之间连续,然后针对每个 ID 递增。您可以使用

geom_segment
显示生长的线条。请记住,由于您使用的是阴影标记,因此在屏幕上扩展的线条会在左侧留下比右侧更重的痕迹,导致阴影标记在面板上看起来“淡出”。

library(ggplot2)
library(gifski)
library(gganimate)
library(transformr)
library(tidyverse)

anim <- df %>% 
  mutate(prepost = factor(prepost, levels = c("Before", "After"))) %>%
  group_by(participant, group) %>%
  arrange(prepost, .by_group = TRUE) %>%
  reframe(value = seq(first(value), last(value), length = 10),
          prepost = seq(0, 1, length = 10)) %>%
  group_by(participant, group) %>%
  mutate(start_x = first(prepost), start_y = first(value)) %>%
  mutate(grad = last(value) - start_y) %>%
  ungroup() %>%
  mutate(frame_var = prepost + participant) %>%
  ggplot(aes(x = start_x, y = start_y, color = grad)) +
  geom_segment(aes(xend = prepost, yend = value, group = participant), 
               linewidth = 0.8) +
  scale_y_continuous('value', breaks = seq(10, 50, 5)) +
  scale_x_continuous('PrePost', breaks = 0:1, labels = c('Before', 'After'),
                     expand = expansion(0, 0.6)) +
  scale_color_gradientn(colors = rep(c('deepskyblue4', 'orange'), each = 2),
                        values = c(0, 0.45, 0.55, 1), guide = 'none') +
  facet_wrap(~factor(group, levels = c("Positive", "Negative")), ncol = 2) +
  transition_time(frame_var) +
  shadow_mark(past = TRUE, future = FALSE, linewidth = 0.2, alpha = 0.05) 

animate(anim, nframes = 30*25, fps = 30)

© www.soinside.com 2019 - 2024. All rights reserved.