刻面后缺少带有 ggpubr 的括号

Missing bracket with ggpubr after faceting

我有一个示例数据框如下:

demo = data.frame(percent = c(84.9,71.4,82.6,69.0,94.1,94.8,91.6,86.5,21.4,70.7,92.3,94.4,28.8,21.8,93.7,87.2),
                 status = rep(c("Pre","Pre","Pre","Pre","Post","Post","Post","Post"),2),
                 gender = c(rep("Male",8),rep("Female",8)),
                 id = c(rep(c("1","2","3","4"),2),rep(c("5","6","7","8"),2)))

然后我继续按性别对数据框进行分面,并使用以下代码使用状态作为 x 变量并使用百分比作为 y 变量为每个性别制作成对图:

compare = list(c("Pre","Post"))
demo %>% ggplot(aes(x=factor(status,c("Pre","Post")),y=percent,group=id)) + ylim(0,101) +
  geom_point(size = 2, aes(color = status)) + geom_line() + 
  facet_grid(~ gender,switch = "x") +
  theme(legend.position = "none",
        axis.title.x = element_blank(),
        strip.placement = "outside",
        strip.text.x = element_text(angle=0)) +
  stat_compare_means(comparisons = compare,label="p.signif",
                     method = "t.test",paired=T,label.y=100.5,label.x = 1.5,tip.length=0)

然而这个图的输出只有男性组的星号和括号,女性组没有,但我希望它也有一个括号显示“NS”作为标签,我想知道为什么括号消失了? (p.s。我也尝试过 hide.ns 参数,但它没有用)。现在看起来像这样:

对于您的情节,一种解决方案可能是先调用 ggline 然后调用 stat_compare_means() :

demo %>% 
mutate(status = relevel(status,ref="Pre")) %>%
ggline(x = "status",y="percent",group="id",
facet.by="gender",point.color="status") + 
stat_compare_means(method="t.test",aes(label=..p.signif..),
paired=TRUE,comparisons=compare)

我运行以前也遇到过类似的问题。问题似乎出在 ggplot() 语句中的分组变量。因此,将 group = id 移动到每个 geoms_*() 修复它。我还简化了最后一行 - 因为你的例子对我不起作用。

demo %>% 
  ggplot(aes(x=factor(status,c("Pre","Post")),y=percent)) + ylim(0,101) +
  geom_point(size = 2, aes(color = status, group = id)) + 
  geom_line(aes(group=id)) +
  facet_grid(~ gender,switch = "x") +
  theme(legend.position = "none",
        axis.title.x = element_blank(),
        strip.placement = "outside",
        strip.text.x = element_text(angle=0)) +
  stat_compare_means(comparisons = compare, label = "p.signif")