簡體   English   中英

帶有p值的動畫小提琴/箱形圖用於配對的Wilcoxon測試

[英]Animated violin/boxplots plots with p-values for paired Wilcoxon tests

我試圖在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)

如果我理解你的問題,那么很容易解決。 由於mutate語法錯誤,您收到此錯誤。 使用mutate和pipe %>%時,無需使用$調用值:

此代碼提供帶有輕微警告的所需動畫:

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

在此輸入圖像描述

UPDATE

在獨立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')

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM