Create simulated wide and long datasets

set.seed(6)
df <- data.frame(
  id = 1:6,
  group = c("light","light", "light", "moderate", "moderate", "moderate")
  ) |>
  mutate(
  id = as.factor(id),
  group = as.factor(group),
    #baseline
  w1 = round(rnorm(6,mean = 45, sd=3)),
  w2 = w1 + round(runif(6,min= -2, max = 2)),
  w3 = w2 - round(runif(6,min= -2, max = 2)),
  w4 = w3 + round(runif(6,min= -2, max = 2)),
  #treatment
  w5 = w4 + round(runif(6,min= 2, max = 4)),
  w6 = ifelse(
    group == "light", 
          w5 - round(runif(3,min= 6, max = 9)), 
          w5 - round(runif(3,min= 8, max = 12))),
  w7 = ifelse(
    group == "light", 
          w6 - round(runif(3,min=6, max = 9)),  
          w6 - round(runif(3,min= 9, max = 15))),
  w8 = ifelse(
    group == "light", 
          w7 - round(runif(3,min= 6, max = 9)), 
          w7 - round(runif(3,min= 9, max = 15))),
  #baseline
  w9 = ifelse(
    group == "light", 
          w8 - round(runif(3,min= 6, max = 9)), 
          w8 - round(runif(3,min= 9, max = 15))),
  w10 = ifelse(
    group == "light", 
          w9 + round(runif(3,min = -1, max = 3)), 
          w9 + round(runif(3,min= -2, max = 2))),
  w11 = ifelse(
    group == "light", 
          w10 + round(runif(3,min = -1, max = 3)), 
          w10 + round(runif(3,min= -2, max = 2))),
  w12 = ifelse(
    group == "light", 
          w11 + round(runif(3,min = -1, max = 3)), 
          w11 + round(runif(3,min= -2, max = 2))),
  )

df_long <- df |>
  pivot_longer(
    cols = starts_with("w"),
    names_to = "week",
    names_prefix = "w",
    values_to = "pcl"
  ) |>
  mutate(
  week = as.factor(as.integer(week))
  )

df
##   id    group w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12
## 1  1    light 46 44 45 46 49 42 36 28 19  19  19  21
## 2  2    light 43 42 41 40 42 36 27 18  9  12  15  16
## 3  3    light 48 49 47 46 50 41 32 26 17  19  21  24
## 4  4 moderate 50 49 48 50 54 43 29 18  6   6   4   2
## 5  5 moderate 45 45 47 46 48 37 27 15  4   2   2   2
## 6  6 moderate 46 47 48 48 52 41 29 15  5   5   5   7
df_long
## # A tibble: 72 × 4
##    id    group week    pcl
##    <fct> <fct> <fct> <dbl>
##  1 1     light 1        46
##  2 1     light 2        44
##  3 1     light 3        45
##  4 1     light 4        46
##  5 1     light 5        49
##  6 1     light 6        42
##  7 1     light 7        36
##  8 1     light 8        28
##  9 1     light 9        19
## 10 1     light 10       19
## # ℹ 62 more rows

Figure 1

Differences in symptoms over treatment for experimental condition and active control

Figure

# intial save
p <- df_long |>
  ggplot(aes(x = week, y = pcl, group = id, color = group))

# plot figure
p + stat_smooth(aes(group = group), alpha = .1) +
  geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", 
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_markdown(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5)
  )
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Animation of figure

toSave <- list(
  #frames with full thing
    p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", #remove the legend
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_markdown(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5),
  )
    #break with the full thing
      , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", #remove the legend
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_markdown(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5),
  )   
      #break with the full thing
      , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", #remove the legend
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_markdown(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5),
  ) 
      #break with the full thing
      , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", #remove the legend
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_markdown(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5),
  ) 
# break 1
  , p + stat_smooth(aes(group = group), alpha = .1)
  # break 2
  , p + stat_smooth(aes(group = group), alpha = .1) +
      geom_point() 
  #break 3
  , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
        ylim(0,60) 
  #break 4
  , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) 
  #break 5
  , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed"))
  #break 6
  , p + stat_smooth(aes(group = group), alpha = .1) +
       geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") 
  # break 7
  , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") 
  # lab part 2 break
    , p + stat_smooth(aes(group = group), alpha = .1) +
      geom_point() +
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.")
  # break lab part 3
    , p + stat_smooth(aes(group = group), alpha = .1) +
        geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")
  # break 9
    , p + stat_smooth(aes(group = group), alpha = .1) +
       geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()
  #Final break
    , p + stat_smooth(aes(group = group), alpha = .1) +
       geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", #remove the legend
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_text(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5),
  )
   #break with the full thing
      , p + stat_smooth(aes(group = group), alpha = .1) +
       geom_point(position = position_dodge(0.1), size =2)+
  geom_line(alpha = .25) + 
      ylim(0,60) +  
      scale_color_manual(values = c("#eb372a","#1a60ed")) +
      geom_vline(xintercept = 4.5, linetype = "dashed") +
      geom_vline(xintercept = 8.5, linetype = "dashed") +
    annotate('text', x = 2.5,y= 59, label = "Baseline\nAssessment") +
  annotate('text', x = 6.5,y= 59, label = "Control or Experimental \nIntervention Sessions") +
  annotate('text', x = 10.5,y= 59, label = "Post Treatment\nAssessment") +
  labs(
    y = "Symptom Severity",
    x = "Weeks In Study",
    title = "Hypothesized reductions of symptoms for <span style='color:#1a60ed'>experimental</span> and <span style='color:#eb372a'>active control</span> groups.",
    caption = "Hypothesized simulated data (n = 6) | Github: Troy-Hubert | X: @THubert95")+
    theme_classic()+
    theme(
    panel.grid.minor = element_blank(),
    plot.title = element_markdown(hjust = 0.5),
    legend.position = "none", #remove the legend
    plot.title.position = "plot",
    plot.caption.position = "plot",
    plot.subtitle = element_markdown(hjust = .16),
    plot.caption = element_markdown(hjust = 0.5),
  )
)

library(animation)

saveGIF(
  {lapply(toSave, print)}
  , "animationTest.gif"
 )
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Output at: animationTest.gif
## [1] TRUE