向条形图添加星号会移动 R 中的条形图时该怎么办?

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

我在 R 中创建了一个条形图,现在我尝试将显着差异添加到条形图中。

我尝试使用 ggsignif 包中的 geom_signif 和 ggpubr 包中的 stat_compare_means(基于这些 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

我只能在使用 geom_signif 时添加显着性水平并选择 https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html 中的参数。

这是我想要得到的示例:

这就是我得到的:

所以当我想添加星号时,它会从条形图中移动条形。不知道怎么改...

这是我写的一部分:

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

这是 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

有谁知道我做错了什么以及我该如何解决?

谢谢!

我不认为 geom_signif 是为了跨越各个方面,但在您的情况下,我认为无论如何都没有真正需要各个方面。查看以下内容是否适合您:

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)