I try to add p-values to my ggplot
using the stat_compare_means
function. However, the p-values I get within the ggplot differs from the result of a basic wilcox.test.
I used paired testing in both cases, and also used the wilcoxon test within the ggplot.
I tried to search my question and but couldn't find an exact answer. I updated R (v. 3.5.2), R-Studio (v. 1.1.463), and all packages. In the following I added a few lines of codes with an example. I am new to R and the statistic, so forgive me if I ask in a newbie way.
library("ggplot2")
library("ggpubr")
c1 <- c( 798.3686, 2560.9974, 688.3051, 669.8265, 2750.6638, 1136.3535,
1335.5696, 2347.2777, 1149.1940, 901.6880, 1569.0731 ,3915.6719,
3972.0250 ,5517.5016, 4616.6393, 3232.0120, 4020.9727, 2249.4150,
2226.4108, 2582.3705, 1653.4801, 3162.2784, 3199.1923, 4792.6118)
c2 <- c(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1)
test <-data.frame(c2,c1)
test$c2 <- as.factor(test$c2)
ggplot(test, aes(x=c2, y=c1)) +
stat_compare_means(paired = TRUE)
wilcox.test( test$c1~ test$c2, paired= TRUE)
Result of the stat_compare_means within the ggplot
Result of the Wilcoxon signed rank test:
data: test$c1 by test$c2
V = 0, p-value = 0.0004883
alternative hypothesis: true location shift is not equal to 0
As you can see, the result is p = 0.0025 within the ggplot and p= 0.0004883 with the basic wilcox.test function. Do you know why it's different? And which value is the correct one?
PS: I tried to do the same with ToothGrowths. In that case the result of the stat_compare_means
and the wilcox.test
show the same results: p = 0.004313. I have no clue why it doesn’t work with my data :/