【问题标题】:Boxplot comparison in R - how do I display p-values in vertical lines within a boxplot?R中的箱线图比较-如何在箱线图中以垂直线显示p值?
【发布时间】:2014-08-23 06:31:31
【问题描述】:

我生成了一个箱线图,并使用wilcox.test 测试了向量 x1 和 x2 的均值差异。如何在箱线图中实现测试结果?

> x1 <- rnorm(1000)
> x2 <- rnorm(1000) +10
> wilcox.test(x1,x2, paired=TRUE)

    Wilcoxon signed rank test with continuity correction

data:  x1 and x2
V = 0, p-value < 2.2e-16
alternative hypothesis: true location shift is not equal to 0

> boxplot(x1, x2)

谢谢你们!

R 版本 3.1.0 (2014-04-10) -- 《春之舞》

平台:x86_64-w64-mingw32/x64(64 位),Windows 7

【问题讨论】:

    标签: r boxplot p-value


    【解决方案1】:

    尝试做:

    text(x=1, y=10, labels="p-value<0.0001")
    

    我是 R 新手,但如果您输入 xy 坐标并输入标签,您可以在图表上手动输入测试结果。

    我确定您已经知道这一点,但 \n 将被用来在您的标签中制止。

    【讨论】:

      【解决方案2】:

      您可以按照here 的说明进行操作:

      library(tidyverse)
      library(ggpubr)
      
      x1 <- rnorm(1000)
      x2 <- rnorm(1000) +10
      dt =  tibble(x1, x2) %>% pivot_longer(1:2)
      
      my_comparisons <- list( c("x1", "x2") )
      ggboxplot(dt, x = "name", y = "value",
                color = "name", palette = "jco")+ 
          stat_compare_means(comparisons = my_comparisons, method.args = list(paired=TRUE))
      

      【讨论】: