You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot双图布局中居中theme_void图的p-value标题?

问题:右侧p-value标题居中导致坐标系出现的解决办法

我是从事中风研究的医学科研人员,因统计师离职需自行处理统计绘图。我要制作左为森林图、右为p值的组合图,已完成图的搭建,但无法将右侧theme_void()生成的图的“p-value”标题居中。尝试使用theme(plot.title = element_text(hjust = 0.5))后,p值区域出现了坐标系,特此求助解决办法。

当前代码

mod_coco_bsl <- lmer(coco ~ exposure + age.bsl.centered + pat.sex + education + 
                    bmi.centered + sport + active_smoker + prev.stroke + prev.diabetes + prev.hypertonie + gds.cat + 
                    (1|center) + crphs + il6, data=dt_bsl)
#summary(mod_coco_bsl)
ci_coco <- confint(mod_coco_bsl, oldNames=FALSE)

output <- tidy(mod_coco_bsl, conf.int=TRUE, exponentiate=FALSE, effects="fixed")[-1]
kable(output[,c(1,2,7,8,6)], digits=c(2,2,2,2,3))

cocopvalue <- c(0,0,0,0)
i <- 1
while (i <= length(cocopvalue)) {
  cocopvalue[i] <- (output[i+1,6])
  i<-i+1
}
cocopvalue <- unlist(cocopvalue, recursive = TRUE, use.names = TRUE)
cocopvalue <- round(cocopvalue, digits = 4)


# plot
res_coco <- data.frame("term"=c("1", "2-3", "4-5", ">5"), 
                  "estimate" = summary(mod_coco_bsl)$coefficients[2:5,1], 
                  "conf.low" = ci_coco[4:7,1], 
                  "conf.high"= ci_coco[4:7,2])
res_coco$term <- factor(res_coco$term, levels=c("1", "2-3", "4-5", ">5") )

(plot_coco <- ggplot(res_coco, aes(y = estimate, x = term, col=term)) +
    geom_pointrange(aes(ymin = conf.low, ymax = conf.high), size = 0.8)+
    geom_hline(yintercept = 0, linetype = "dotted", size = 1) +
    labs(title= "CoCo-Scores in different coffee consumer groups",y = "Effect estimate on CoCo score", x = "Consumed cups of coffee per day") +
    coord_flip(ylim = c(-0.1, 0.35)) +
    theme_bw()+
    theme(legend.position="none", axis.text=element_text(size=14, face = "bold"), axis.title=element_text(size=18), plot.title =   element_text(size=22)) + theme(plot.title = element_text(vjust = 3)) #+ annotate(geom="text", x=1.1, y=1.6, label="B", size=8)
  )


p_right <- 
  ggplot() +  
  geom_text(
    aes(x = 0, y = c("1","2-3","4-5",5), label = cocopvalue ),
    hjust = 0,
    fontface =  "plain") + 
    labs(title="p-value", fontface ="bold") +
  theme_void()


layout <- c(
  area(t = 0, l = 0, b = 30, r = 9),
  area(t = 1, l = 9, b = 30, r = 11)
)

plot_coco + p_right +plot_layout(design = layout)

效果展示

  • 当前效果图(未设置标题居中):
    当前效果图

  • 使用标题居中代码后的效果图(出现坐标系):
    标题居中后的效果图

解决办法

问题出在主题设置的顺序和覆盖逻辑上:theme_void()会清除所有坐标轴、网格等元素,但如果先设置theme(plot.title = ...)再调用theme_void(),后者会覆盖标题的对齐设置;反之如果先调用theme_void()再设置标题属性,又会不小心恢复部分坐标系元素(因为默认主题的部分设置会被引入)。

正确的做法是在theme_void()之后,明确指定仅修改标题的对齐方式,同时保留theme_void()的无坐标轴状态,修改后的p_right代码如下:

p_right <- 
  ggplot() +  
  geom_text(
    aes(x = 0, y = factor(c("1","2-3","4-5",">5"), levels = c("1","2-3","4-5",">5")), label = cocopvalue ),
    hjust = 0,
    fontface =  "plain") + 
  labs(title="p-value") +
  theme_void() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    # 确保所有坐标轴相关元素保持隐藏
    axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    panel.grid = element_blank()
  )

另外,将右侧p值的y轴因子设置为和左侧森林图一致的水平,可以让p值和对应的分组完全对齐,提升图的美观度。

内容的提问来源于stack exchange,提问作者Massimo Barbagallo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 20:57:29