4

我正在尝试为 R 中的配对 wilcox 测试添加 p 值。我正在使用以下代码。下面的代码为两种饮食(治疗)创建结果读数(二头肌)的小提琴(密度分布)。这些小提琴在时间 1、时间 2、时间 3 上动画。图的顶部打印 p 值。我希望这些 p 值是成对的值,这样

对于饮食“a”,将时间 2 的二头肌读数与时间 1 进行比较,并将时间 3 的二头肌读数与时间 1 进行比较。

饮食“b”也是如此。因此,在时间 2 和时间 3 的小提琴顶部应该有两个单独的 p 值。指示饮食“a”和饮食“b”的配对测试(时间 2 与时间 1 和时间 3 与时间 1) .

这个测试的正确代码应该是什么?我根据昨天收到的建议在下面尝试了一些方法,但遇到了错误。我还认为下面的代码只是对时间 2 与时间 1 和时间 3 与时间 2 进行配对测试。这不是我想要的。

谢谢阅读。

 library(ggplot2)
 library(ggpubr)
 library(gganimate)
 library(tidyverse)

示例数据

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

示例代码

example3 %>%
  group_by(time) %>% 
  mutate(p=pairwise.wilcox.test(example3$bicep, interaction(example3$diet, example3$time), p.adjust.method = "none")$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')

这是我得到的错误

 Error in mutate_impl(.data, dots) : 
  Column `p` must be length 8 (the group size) or one, not 25
In addition: There were 15 warnings (use warnings() to see them)
4

1 回答 1

1

如果我正确理解您的问题,那么解决方案非常简单。你得到这个错误是因为里面有错误的语法mutate。使用和 管道$时无需调用值:mutate%>%

此代码提供了一个带有轻微警告的所需动画:

example3 %>%
  group_by(time) %>% 
  mutate(p=pairwise.wilcox.test(bicep, interaction(diet, time), p.adjust.method = "none")$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')

在此处输入图像描述

更新

例如,在独立 p 值的情况下,您只需要添加facet_wrap()。这似乎是最简单的:

example3 %>%
  group_by(time) %>% 
  mutate(p = pairwise.wilcox.test(bicep, interaction(diet, time), p.adjust.method = "none")$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, y = max+.5, label = as.character(round(p,2))),
            size = 12) +
  facet_wrap(~diet, scales = "free_x") + # add facets
  theme(legend.position = "none") +
  transition_time(time) +
  ease_aes('linear')

在此处输入图像描述

于 2018-11-22T10:22:15.423 回答