Search code examples
rggplot2significance

What to do when adding asterisks to bar graph shifts the bars in R?


I've created a bar graph in R and now I tried to add the significant differences to the bar graph.

I've tried using geom_signif from the ggsignif package and stat_compare_means from the ggpubr package (based on these suggestions/examples: Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value) or https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html)

I was only able to add the significance levels when using geom_signif and choose the parameters as in https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html.

This is an example of what I would like to get:

Planned bar graphs

And this is what I get:

Eventual bar graphs

So when I want to add the asterisks, it shifts the bars from the bar graph. I don't know how to change it...

This is a part of what I wrote:

bargraph = ggplot(dataPlotROI, aes(x = ROI, y=mean, fill = Group))

bargraph + 
  geom_bar(position = position_dodge(.5), width = 0.5, stat = "identity") +
  geom_errorbar(position = position_dodge(width = 0.5), width = .2, 
                aes(ymin = mean-SEM, ymax = mean+SEM)) +
  geom_signif(y_position = c(4.5,10,10), xmin=c(0.85,0.85,4.3), xmax = c(5,4,7.45),
              annotation=c("***"), tip_length = 0.03, inherit.aes = TRUE) +
  facet_grid(.~ROI, space= "free_x", scales = "free_x", switch = "x")

This is the output from dput(dataPlotROI):

> Dput <- dput(dataPlotROI)
structure(list(Group = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("1", 
"2"), class = "factor"), ROI = structure(c(1L, 2L, 3L, 1L, 2L, 
3L), .Label = c("LOT", "MO", "ROT"), class = "factor"), mean = c(2.56175803333696, 
7.50825658538044, 3.34290874605435, 2.41750375190217, 6.90310020776087, 
3.03040666678261), SD = c(1.15192431061913, 4.30564383354597, 
2.01581544982848, 1.11404900115086, 3.35276625079825, 1.23786817391241
), SEM = c(0.120096411333424, 0.448894400545147, 0.210163288684092, 
0.11614763735292, 0.349550045127766, 0.129056678481624)), class = "data.frame", row.names = c(NA, 
-6L))
> Dput
  Group ROI     mean       SD       SEM
1     1 LOT 2.561758 1.151924 0.1200964
2     1  MO 7.508257 4.305644 0.4488944
3     1 ROT 3.342909 2.015815 0.2101633
4     2 LOT 2.417504 1.114049 0.1161476
5     2  MO 6.903100 3.352766 0.3495500
6     2 ROT 3.030407 1.237868 0.1290567

Does anyone know what I am doing wrong and how I can fix it?

Thanks!


Solution

  • I don't think geom_signif is meant to span across the facets, but in your case, I don't see any real need for facets anyway. See if the following works for you:

    ggplot(dataPlotROI,
           aes(x = ROI, y = mean, fill = Group)) +
    
      # geom_col is equivalent to geom_bar(stat = "identity")
      geom_col(position = position_dodge(0.5), width = 0.5) +
    
      geom_errorbar(position = position_dodge(0.5), width = 0.2,
                    aes(ymin = mean - SEM, ymax = mean + SEM)) +
    
      # xmin / xmax positions should match the x-axis labels' positions
      geom_signif(y_position = c(4.5, 10, 10),
                  xmin = c(1, 1, 2.05),
                  xmax = c(3, 1.95, 3),
                  annotation = "***",
                  tip_length = 0.03)
    

    plot