添加 p 值以比较组意味着在 gganimate gif 中有时不同 boxplots/violins

Adding p-values to compare groups means at different at times in gganimate gif with boxplots/violins

我当前使用 gganimate 打印小提琴的代码如下所示

  library(ggplot2); library(gganimate); library(ggpubr)
  ggplot(dat2, aes(x=diet, y=bicep, fill=diet)) + 
  geom_violin() +
  scale_fill_manual(values=c("#00AFBB", "#FC4E07")) +
  stat_compare_means(aes(label = ..p.format..), paired = FALSE, label.x.npc = 0.5) +
  labs(title = 'Week: {frame_time}') +
  transition_time(time) +
  ease_aes('linear')

此处打印了 p 值,但它们只是总体 p 值。我希望 p 值随时间变化(0、6 和 12 周)。在我的研究中,每个结果测量值(二头肌)都是在三个不同的时间(0、6 和 12 周或时间 1、时间 2、时间 3)进行的,如果我能在时间 0 显示变化的 p 值,那就太好了, 6、12。在这里,我将使用未配对的 t 检验来比较 diet/treatment.

的组均值

或者,在结束时显示 p 值(配对 t 检验),将两种饮食的时间“3”的二头肌与时间“1”的二头肌进行比较。

我该怎么做?感谢您阅读本文。

数据结构

 structure(list(code = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L), diet = c("a", 
"a", "a", "b", "b", "b", "a", "a", "a", "b", "b", "b", "a", "a", 
"a", "b", "b", "b", "a", "a", "a", "b", "b", "b"), time = c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L), bicep = c(8L, 7L, 7L, 9L, 9L, 9L, 
11L, 10L, 9L, 11L, 11L, 12L, 12L, 11L, 10L, 9L, 9L, 9L, 12L, 
10L, 8L, 12L, 12L, 12L)), class = "data.frame", row.names = c(NA, 
-24L))

可重现的 gganimate 代码

    ggplot(example3, aes(x=diet, y=bicep, fill=diet)) + 
  geom_violin() +
  scale_fill_manual(values=c("#00AFBB", "#FC4E07")) +
  stat_compare_means(aes(label = ..p.format..), paired = FALSE, label.x.npc = 0.5) +
  labs(title = 'Week: {frame_time}') +
  transition_time(time) +
  ease_aes('linear')

您可以尝试提前计算p.values。

library(gganimate)
library(tidyverse)
example3 %>%
  group_by(time) %>% 
  mutate(p=wilcox.test(bicep~diet, exact =F)$p.value,
         max=max(bicep, na.rm = T)) %>% 
  ggplot() + 
  geom_violin(aes(x=diet, y=bicep, fill=diet)) +
  geom_text(data = . %>% distinct(p, max, time), 
            aes(x=1.5, y = max+.5, label=as.character(round(p,2))),
            size=12) +
  transition_time(time) +
  ease_aes('linear')

您需要安装ggpubr_0.2