Home > Back-end >  How to make animation of items for a specific subject's responses over time?
How to make animation of items for a specific subject's responses over time?

Time:10-21

Data

Here is the simulated data for my question. It consists of subjects, items (stimuli), and a T/F response to each item:

#### Load Tidyverse ####
library(tidyverse)
library(gganimate)

#### Create Tibble ####
set.seed(123)
subject <- factor(rep(1:5,100))
score <- rbinom(n=500,
                size=1,
                prob=.5)
tib <- tibble(subject,
           score) %>% 
  group_by(subject) %>% 
  mutate(item = row_number())
tib

Problem

I'm trying to figure out how to animate either a single subject or many subject responses over time. If I plot the change over time in this way:

#### Plot Change Over Items ####
tib %>% 
  ggplot(aes(x=item,
             y=score,
             color=subject)) 
  geom_point() 
  geom_smooth(se=F)

enter image description here

I can at least see generally speaking where the trends lie. However, I would like to have something animated which shows the progression of responses as they happen. I tried using gganimate, but it wouldn't use geom_smooth and the points alone are lacking a lot of useful information:

#### Plot Change Over Items ####
tib %>% 
  ggplot(aes(x=item,
             y=score,
             color=subject)) 
  geom_point() 
  transition_manual(item)

I tried a cumulative sum plot as well:

#### Plot Cumulative Sum ####
tib %>% 
  mutate(cum_score = cumsum(score)) %>% 
  ggplot(aes(x=item,
             y=cum_score,
             color=subject)) 
  geom_line()

enter image description here

But animating it still comes out poor:

#### Plot Cumulative Sum ####
tib %>% 
  mutate(cum_score = cumsum(score)) %>% 
  ggplot(aes(x=item,
             y=cum_score,
             color=subject)) 
  geom_line() 
  transition_manual(cum_score)

Am I messing up the arguments here? Is there a better alternative?

CodePudding user response:

I figured it out. I was trying to figure out how to use the cumulative argument and I realized it was a logical argument:

#### Plot Cumulative Sum ####
tib %>% 
  mutate(cum_score = cumsum(score)) %>% 
  ggplot(aes(x=item,
             y=cum_score,
             color=subject)) 
  geom_line() 
  geom_point() 
  transition_manual(cumulative = T,
                    frames = cum_score)

Which gives me a nice gif:

enter image description here

  • Related