如何自动在ggplot2的barplot上放置显着性水平(p值)?

时间:2019-10-10 09:09:28

标签: r ggplot2 bar-chart p-value significance

我在这里已经看到了类似的答案,但是我的却有点不同。

有没有一种方法可以计算出显着性并将星星自动添加到ggplot中?

我有一个带有条形图的图,它代表了整个季节的平均值,对于某些变量(化合物),另外三层具有点,线和误差线,它们表示整个季节中特定时间窗口(物候阶段)的平均值

我只想在整个赛季平均水平的条形图上加上星号。 请注意,在我的代码中,我根据构图使用了多面网格来绘制增塑剂的几种化合物。

目前,我只是尝试将函数stat_compare_means放在绘图代码中。

我与您分享我的部分数据集和我拥有的代码。

structure(list(phen = c("GS0", "GS1", "GS2", "GS3", "H", "GS0", 
"GS1", "GS2", "GS3", "H", "GS0", "GS1", "GS2", "GS3", "H"), var = c("mz33_flux", 
"mz33_flux", "mz33_flux", "mz33_flux", "mz33_flux", "mz45_flux", 
"mz45_flux", "mz45_flux", "mz45_flux", "mz45_flux", "mz137_flux", 
"mz137_flux", "mz137_flux", "mz137_flux", "mz137_flux"), value = c(-0.0201807203084833, 
0.00699548966597077, 0.2982090471597, 0.763140160427808, 0.0115715254237288, 
-0.00141967461669506, 0.0216389158168574, 0.152685557877813, 
0.0861748924731184, 0.00783050847457625, -0.0568113340318524, 
-0.0283260888657648, 0.0584874208715596, -0.0014408875, -0.0199876076811594
), err = c(0.00891612382048675, 0.00865982415221607, 0.0261407868072828, 
0.150893410160645, 0.000915642610907682, 0.00313669333464331, 
0.00539139343017679, 0.0109634891854869, 0.0244123650545713, 
0.000586325133571454, 0.0618423495252825, 0.0290907608410913, 
0.0405039109957796, 0.0135552560862062, 0.0016406585376453), 
    tot = c(0.104295388186188, NA, NA, NA, NA, 0.0462282825845532, 
    NA, NA, NA, NA, -0.0152536834035828, NA, NA, NA, NA)), row.names = c(6L, 
7L, 8L, 9L, 10L, 21L, 22L, 23L, 24L, 25L, 106L, 107L, 108L, 109L, 
110L), class = "data.frame")

我现在尝试的代码。

ggplot(higherperiod,aes(x=phen,y=value,group=1))+
  geom_bar(aes(x='GS2', y = tot,fill='Whole growing season'),colour="black",
           stat="identity",position='dodge', width = 5) +
  stat_compare_means(method = "anova", label.y = 1)+ # Add global p-value
  stat_compare_means(aes(label = ..p.signif..),
                     method = "t.test", ref.group = "0.5")+
  geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.2, 
                position=position_dodge(0.9),colour="black")+
  geom_line(aes(colour="Single phenological stage"), linetype="solid",size = 1)+
  geom_point(aes(colour="Single phenological stage"),shape = 21, fill='white',size = 2)+
  facet_grid(~var,scales="free_y",labeller = labeller(var=voclabh),switch = "x")+
  scale_colour_manual(values = c("black"))+
  scale_fill_manual(values = c('darkolivegreen4'))+
  scale_x_discrete(name = "Phenological stages") +
  scale_y_continuous(name = expression(bold(atop("Mean Flux",(nmol~m^bold("-2")~s^bold("-1"))))),breaks = seq(-0.25, 1 ,0.25),
                     limits=c(-0.25, 1))+
  theme_classic() +theme(legend.position = 'none',
                         legend.title = element_blank())+
  theme(plot.title = element_text(size = 16, family = "Tahoma", face = "bold"),
        text = element_text(size = 14, family = "Tahoma"),
        axis.title.y = element_text(face="bold",size=16),
        axis.text.y = element_text(color="black",size=16),
        axis.title.x=element_blank(), axis.text.x=element_blank(),axis.ticks.x=element_blank(),
        strip.text.x = element_text(size =  13.5, color = "black",face="bold"),
        strip.background = element_blank(), panel.spacing = unit(0, "lines"), 
        strip.placement = "outside",
        legend.text = element_text(color = "black", size = 16,face="bold"))+
  geom_hline(yintercept=0, linetype="solid", color = "black",size=1,alpha=0.9)

I just share with you the image that I already have. I want just to put the significance level on the barplot that rapresent the total average

0 个答案:

没有答案