简体   繁体   中英

Boxplot comparison in R - how do I display p-values in vertical lines within a boxplot?

I generated a boxplot and tested the differences in means of the to vectors x1 and x2 with wilcox.test . How can I implement the test result in the boxplot?

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

Thank you guys!

R version 3.1.0 (2014-04-10) -- "Spring Dance"

Platform: x86_64-w64-mingw32/x64 (64-bit), Windows 7

Try doing:

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

I am a newbie with R, but if you input your x and y coordinates and input your labels you could input your test results manually on your graph.

I am sure you already know this, but \\n would be used to make a brake in your labels.

You could do this as described 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM