在条形图上添加星号以移动R中的条形时该怎么办?

时间:2018-10-03 10:52:31

标签: r ggplot2 significance

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

我尝试使用ggsignif软件包中的geom_signif和ggpubr软件包中的stat_compare_means(基于这些建议/示例:Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value)https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html

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

这是我想要得到的例子:

Planned bar graphs

这就是我得到的:

Eventual bar graphs

因此,当我想添加星号时,它会将条形图从条形图中移开。我不知道该如何更改...

这是我写的一部分:

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

有人知道我在做什么错吗,我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不认为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)

plot