首页 > 解决方案 > R:带有刻面的ggplot2图中的geom_signif;为什么我不能指定不包括第一组的比较?

问题描述

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

我只想制作一个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之后访问它以包含我的实际值,所以让我们尝试保持这种方式。

如您所见,我遇到的问题是由于某种原因我只能包含comparisons该包含rep1...当我尝试包含比较rep3rep4,例如,它被忽略了!为什么会这样?

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

您可以在 中看到rep3vsrep4比较是如何被完全忽略的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为所有面板中的所有最终括号指定?如果所有面板都相同,我知道该怎么做,但请注意DcellsPopulation 具有不同的值范围,因此我想保留“自由”比例。

非常感谢!

标签: rggplot2bracketssignificance

解决方案


看起来您需要将 '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()

示例_1.png

您可能还需要考虑更改“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()

示例_2.png


推荐阅读