R-stat_compare_means返回Kruskal-Wallis检验的不同值 编辑

我想使用ggplot包中的R函数stat_compare_means将Kruskal-Wallis测试的p值绘制到我的ggpubr

但是,如果我只运行该函数,则绘制的值与该值不同:

  

kruskal.test(value ~ type,data = Profile_melt)

我绘制p值的代码是:

ggplot(Profile_melt,aes(type,value)) + 
  geom_boxplot(aes(fill = factor(type),alpha = 0.5),outlier.shape = NA,show.legend = FALSE) +
  geom_jitter(width = 0.2,size = 2,show.legend = FALSE,aes(colour = factor(type)),alpha = 0.5) +
  theme_bw() +
  facet_grid(Case ~ Marker,scales = 'free') +
  stat_compare_means(comparison = list(c("Real","Binomial")),method = 'kruskal.test')+
  background_grid(major = 'y',minor = "none") + # add thin horizontal lines 
  xlab('Category') +
  ylab('Cell counts (Frequencies)')+
  theme(axis.text = element_text(size = 15),axis.title = element_text(size = 20),legend.text = element_text(size = 38),legend.title = element_text(size = 30),strip.background = element_rect(colour="black",fill="white"),strip.text = element_text(margin = margin(10,10,10),size = 25)) +
  panel_border()

这是我的数据sample data

book1928 回答:R-stat_compare_means返回Kruskal-Wallis检验的不同值 编辑

有许多代码行可能与问题无关。也许,您的问题可能是:

为什么

kruskal.test(value ~ type,data = Profile_melt)

#Kruskal-Wallis chi-squared = 4.9673,df = 1,p-value = 0.02583

产生与

不同的p值
ggboxplot(Profile_melt,x="type",y = "value") + 
  stat_compare_means(comparison = list(c("Real","Binomial")),method = 'kruskal.test')

# p-value = 0.49

您可以通过检查原始代码来找出原因。 ggpubr的开发人员可能会对此做更好的解释,如果有问题,也许可以在那里进行修复。要获得正确且一致的p值,请删除comparison = list(c("Real","Binomial"))

ggboxplot(Profile_melt,y = "value") + 
  stat_compare_means(method = 'kruskal.test')

编辑

ggboxplot(Profile_melt,"Binomial")))

使用您的其他代码,图形如下所示:

enter image description here

, 来自ggpubr的

stat_compare_means调用compare_means,默认情况下使用wilcox.test。因此,正如@ZhiqiangWang指出的那样,如果删除方法或比较,它将使用默认值,该默认值与您首先获得的p值相似,因为2个样本的wilcoxon和kruskal非常相似:

kruskal.test(value ~ type,data = Profile_melt)
#Kruskal-Wallis chi-squared = 4.9673,p-value = 0.02583
wilcox.test(value ~ type,data = Profile_melt)
#W = 1034939,p-value = 0.02583

现在,对于您拥有的数据,您很可能希望为每个单独的案例和标记提供一个p值,而不是使用kruskal.test(value ~ type,data = Profile_melt)进行平移比较。为所有构面打印相同的p值没有意义。

我们首先检查所需的p值:

compare_means(value ~ type,Profile_melt,group.by = c("Case","Marker"),method="kruskal")
# A tibble: 30 x 8
   Case    Marker .y.            p   p.adj p.format p.signif method        
   <fct>   <fct>  <chr>      <dbl>   <dbl> <chr>    <chr>    <chr>         
 1 Case 1A CD3    value 0.000470   0.0085  0.00047  ***      Kruskal-Wallis
 2 Case 1A CD4    value 0.00000915 0.00022 9.2e-06  ****     Kruskal-Wallis
 3 Case 1A CD8    value 0.00695    0.09    0.00695  **       Kruskal-Wallis
 4 Case 1A CD20   value 0.707      1       0.70724  ns       Kruskal-Wallis
 5 Case 1A FoxP3  value 0.00102    0.014   0.00102  **       Kruskal-Wallis
 6 Case 1B CD3    value 0.0000415  0.00091 4.1e-05  ****     Kruskal-Wallis

类似于:

Profile_melt %>% 
group_by(Case,Marker) %>% 
summarize(k_p=kruskal.test(value ~ type)$p.value)

# A tibble: 30 x 3
# Groups:   Case [6]
   Case    Marker        k_p
   <fct>   <fct>       <dbl>
 1 Case 1A CD3    0.000470  
 2 Case 1A CD4    0.00000915
 3 Case 1A CD8    0.00695   
 4 Case 1A CD20   0.707     
 5 Case 1A FoxP3  0.00102   

我们可以绘图,使用ggpubr软件包中的ggboxplot必须更容易:

p = ggboxplot(Profile_melt,y="value",add="jitter",facet.by=c("Case",scales="free_y",ggtheme=theme_pubclean())

p+stat_compare_means(
aes(label =paste("p=",scientific(as.numeric(..p.format..)))),method="kruskal",size=2)

enter image description here

本文链接:https://www.f2er.com/2856075.html

大家都在问