How to put significance level (p-value) on barplot in ggplot2 automatically?

徘徊边缘 提交于 2019-12-13 04:18:19

问题


I saw a similar answer already asked here, but the mine is little bit different.

Is there a way to calculate significance and put the stars on the barplot, automatically in ggplot?

I have a plot with barplot that represent the mean of an entire season, for some variables (compounds), and other three layers with point, line and errorbars of the average of specific time windows (phenological stages) into the complete season.

I want just to put asterisks over barplots of the total season average. Note that in my code I use facet grid to plot togheter several compounds, depending on the scale.

For now I've just tried to put the function stat_compare_means in my plot code.

I share with you a part of my dataset and the code that I have.

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

The code that I tried to now.

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)

来源:https://stackoverflow.com/questions/58319296/how-to-put-significance-level-p-value-on-barplot-in-ggplot2-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!