R:geom_signif 在 ggplot2 图中,带有小平面;为什么我不能指定不包括第一组的比较?

R: geom_signif in a ggplot2 plot with facets; why can't I specify comparisons that do not include the first group?

我正在尝试做我过去成功完成的事情,但现在我不确定我是否遗漏了某些东西或新版本中的某些东西发生了变化,但对于我来说,我无法让它发挥作用...

我只想制作一个像下面这样的多面 ggplot2 图,在其顶部使用 geom_signif 实际包括每个绘制值与所有其他值相比的百分比变化。

然而,为了简单起见,我只是将“foo”添加到 geom_signif 括号中(还没有百分比变化值)。

所以我有这样一个数据框:

mydata <- data.frame(Rep=rep(paste0('rep',1:5), 4),
                     Population=rep(paste0(LETTERS[1:4],'cells'), each=5),
                     Value=c(runif(15,1,5), runif(5,30,40)))

    Rep Population     Value
1  rep1     Acells  3.906863
2  rep2     Acells  2.391534
3  rep3     Acells  2.417360
4  rep4     Acells  4.956607
5  rep5     Acells  1.018905
6  rep1     Bcells  3.348250
7  rep2     Bcells  1.979448
8  rep3     Bcells  3.499493
9  rep4     Bcells  4.161168
10 rep5     Bcells  4.705278
11 rep1     Ccells  1.854068
12 rep2     Ccells  4.514578
13 rep3     Ccells  3.430654
14 rep4     Ccells  4.418377
15 rep5     Ccells  1.228447
16 rep1     Dcells 39.763432
17 rep2     Dcells 36.528565
18 rep3     Dcells 31.575392
19 rep4     Dcells 34.956205
20 rep5     Dcells 39.882848

我只是尝试按照以下方式制作情节;请注意,我想将绘图保存在 P 中并在之后访问它以包含我的实际值,所以让我们尽量保持这种状态。

如您所见,我遇到的问题是出于某种原因我只能包含包含 rep1comparisons...我尝试包含比较 [=20] 的那一刻=] vs rep4 例如,它会被忽略!为什么会这样?

P <- ggplot2::ggplot(mydata, ggplot2::aes(x=Rep, y=Value, group=1)) +
  ggplot2::geom_line(color="black") +
  ggplot2::geom_point(color="red", shape=16, size=5) +
  
  ggplot2::facet_wrap(.~Population, scales="free", ncol=2) +
  ggplot2::theme_light() +
  ggsignif::geom_signif(comparisons=list(c("rep1","rep2"),c("rep1","rep3"),c("rep3","rep4")),
                        annotation="foo",
                        textsize=5,
                        size=1)
# build the plot (get a list of data frames out of it)
P2 <- ggplot2::ggplot_build(P)
# in list 3 we have access to each annotation
head(P2$data[[3]], 20)
# plot
grDevices::pdf.options(reset = TRUE, onefile = FALSE)
grDevices::pdf(file="test.pdf", height=10, width=10)
print(#or ggsave()
  graphics::plot(ggplot2::ggplot_gtable(P2))
)
grDevices::dev.off()

您可以看到 rep3rep4 的比较在 head(P2$data[[3]], 20) 中是如何被完全忽略的,参见面板 1:

   x xend         y      yend annotation       group PANEL shape colour textsize angle hjust vjust alpha family fontface
1  1    1  5.035361  5.153492        foo rep1-rep2-1     1    19  black        5     0   0.5     0    NA               1
2  1    2  5.153492  5.153492        foo rep1-rep2-1     1    19  black        5     0   0.5     0    NA               1
3  2    2  5.153492  5.035361        foo rep1-rep2-1     1    19  black        5     0   0.5     0    NA               1
4  1    1  5.035361  5.153492        foo rep1-rep3-2     1    19  black        5     0   0.5     0    NA               1
5  1    3  5.153492  5.153492        foo rep1-rep3-2     1    19  black        5     0   0.5     0    NA               1
6  3    3  5.153492  5.035361        foo rep1-rep3-2     1    19  black        5     0   0.5     0    NA               1

当然,最后的情节没有显示比较的括号(仅针对 rep1 比较):

知道为什么会这样吗?

作为一个附加问题:我如何为所有面板中的所有最终括号指定 y_position?如果所有面板都相同,我知道该怎么做,但请注意 Dcells 人口具有不同的值范围,因此我想保留“自由”比例。

非常感谢!

看来您需要将 'group = 1' 移动到 geom_line() 中,例如

library(tidyverse)
library(ggsignif)
mydata <- data.frame(Rep=rep(paste0('rep',1:5), 4),
                     Population=rep(paste0(LETTERS[1:4],'cells'), each=5),
                     Value=c(runif(15,1,5), runif(5, 30,40)))


P <- ggplot(mydata, aes(x = Rep, y = Value)) +
  geom_point(color = "red", shape = 16, size = 5) +
  geom_line(aes(group = 1)) +
  facet_wrap(. ~Population, scales = "free_y", ncol = 2) +
  theme_light() +
  geom_signif(comparisons=list(c("rep1", "rep2"),
                               c("rep1", "rep3"),
                               c("rep3", "rep4")),
              annotations = c("foo 1v2", "foo 1v3", "foo 3v4"),
              textsize=4,
              size=1,
              step_increase = 0.1)

# build the plot (get a list of data frames out of it)
P2 <- ggplot2::ggplot_build(P)
# in list 3 we have access to each annotation
head(P2$data[[3]], 20)
# plot
grDevices::pdf.options(reset = TRUE, onefile = FALSE)
grDevices::pdf(file="test.pdf", height=10, width=10)
print(#or ggsave()
  graphics::plot(ggplot2::ggplot_gtable(P2))
)
grDevices::dev.off()

您可能还想考虑更改 'free y' 比例,具体取决于您的应用,例如

P <- ggplot(mydata, aes(x = Rep, y = Value)) +
  geom_point(color = "red", shape = 16, size = 5) +
  geom_line(aes(group = 1)) +
  facet_wrap(. ~Population, ncol = 2) +
  theme_light() +
  ylim(c(0,60)) +
  geom_signif(comparisons=list(c("rep1", "rep2"),
                               c("rep1", "rep3"),
                               c("rep3", "rep4")),
              annotations = c("foo 1v2", "foo 1v3", "foo 3v4"),
              textsize=4,
              size=1,
              step_increase = 0.1)

# build the plot (get a list of data frames out of it)
P2 <- ggplot2::ggplot_build(P)
# in list 3 we have access to each annotation
head(P2$data[[3]], 20)
# plot
grDevices::pdf.options(reset = TRUE, onefile = FALSE)
grDevices::pdf(file="test.pdf", height=10, width=10)
print(#or ggsave()
  graphics::plot(ggplot2::ggplot_gtable(P2))
)
grDevices::dev.off()