刻面后缺少带有 ggpubr 的支架

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

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","Post","Post"),2),gender = c(rep("Male",8),rep("Female",8)),id = c(rep(c("1","2","3","4"),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”作为标签的括号,我想知道为什么括号消失了? (ps 我也试过 hide.ns 参数,但没有用)。现在看起来像这样:

刻面后缺少带有 ggpubr 的支架

jxdlzz 回答:刻面后缺少带有 ggpubr 的支架

我之前也遇到过类似的问题。似乎问题在于 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")

enter image description here

,

对于您的情节,一种解决方案可能是调用 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)

enter image description here

本文链接:https://www.f2er.com/40756.html

大家都在问