R: ggplot2 图中的 geom_signif 和 facets;为什么我不能指定不包括第一组的比较?

问题描述

我正在尝试做一些我过去成功完成的事情,但现在我不确定我是否遗漏了什么或在新版本中发生了什么变化,但对于我的生活,我无法让它工作.. .

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

不过,为了简单起见,我只是在 geom_signif 括号中添加了“foo”(尚无百分比变化值)。

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

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 中,然后访问它以包含我的实际值,所以让我们尽量保持这种方式。

正如您所看到的,我遇到的问题是由于某种原因,我只能包含包含 comparisonsrep1...当我尝试包含比较 rep3 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 比较):

test

知道为什么会这样吗?

作为一个附加问题:我将如何为所有面板中的所有最终括号指定 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()

example_1.png

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

P <- ggplot(mydata,ncol = 2) +
  theme_light() +
  ylim(c(0,60)) +
  geom_signif(comparisons=list(c("rep1",width=10)
print(#or ggsave()
  graphics::plot(ggplot2::ggplot_gtable(P2))
)
grDevices::dev.off()

example_2.png

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...