【问题标题】:Adding p-values to compare groups means at different at times in gganimate gif with boxplots/violins添加 p 值以比较组意味着在 gganimate gif 中使用箱线图/小提琴有时会有所不同
【发布时间】:2019-04-21 07:11:42
【问题描述】:

我当前使用 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 检验来比较饮食/治疗的组均值。

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

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

数据结构

 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')

【问题讨论】:

  • 您提供的数据示例似乎与您在代码中使用的字段不同。我有兴趣尝试提供帮助,但此代码不能自行运行。
  • @JonSpring 感谢您的检查,我只是查看了示例代码和实际代码几次。我似乎无法注意到差异。你能指出我有什么不同吗?我会改正的。
  • @JonSpring 我已经更改了可重现的数据和示例代码并对其进行了测试。它适用于我。谢谢。

标签: r ggplot2 gganimate ggpubr


【解决方案1】:

您可以尝试提前计算 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

【讨论】:

  • 这太好了,谢谢!只是为了检查这是比较两组意味着饮食 a 与饮食 b 对吗?我需要做什么比较饮食“a”二头肌在时间 3 的平均值与饮食“a”二头肌在时间 1 的读数?饮食 b 也是如此?
  • 只做pairwise.wilcox.test(example3$bicep, interaction(example3$diet, example3$time), p.adjust.method = "none")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2014-08-23
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2019-10-09
  • 2020-11-08
相关资源
最近更新 更多