如何在多面图中将垂直和水平线仅添加到一个面板?

问题描述

我正在使用interact_plot函数绘制3向交互。 我想通过添加一些垂直和水平线来添加重要的Johnson-Neyman区域。 当我使用jtools进行简单绘图时,唯一的重要曲线是mod2-1 SD组。 我只想在mod2 -1 SD面板(左面板)中包括JN间隔(vline和hline)。

这是我的脚本,可以运行,但是将垂直和水平线应用于所有3个mod2面板。

png(filename = 'interaction_plot.png',width=1000,height=400)
P=interact_plot(fit4,pred = mean_centered_Pren_CESD,modx = gender_male,mod2 = 
mean_centered_auc_post_cesd,interval = TRUE,plot.points = TRUE,colors = c("blue","orange"),x.label = "Prenatal depression",y.label = "ADHD",pred.labels = "Prenatal depression",modx.labels = c("Girls"=0,"Boys"=1),mod2.labels = c("A) Low postnatal depression","B) Average postnatal 
                depression","C) High postnatal depression"),main.title = NULL,legend.main = "Sex")
P +
  drop_gridlines() +
  geom_vline(xintercept=c(-0.06,1.74),linetype='dashed',size=1) +
  geom_hline(yintercept=c(-0.14,1.33),linetype='solid',size=1)
dev.off()

在这些代码行中,我只想应用 “

geom_vline(xintercept=c(-0.06,size=1)

” 基于默认-1SD分组的“低”的mod2组。 有人可以帮我吗,我尝试了很多事情,但我不知道该怎么称呼左面板。

非常感谢您。

解决方法

使用mtcars作为示例数据集,通常可以像这样实现:

  1. 在数据框中放置垂直或水平线的坐标。
  2. 将faceting变量添加到此数据框。
  3. xinterceptyintercept放在aes()

这样,您可以指定想要geom_vline和/或geom_hline的面板。在我的示例中,两者均仅在cyl = 4的第一个面板中绘制。

library(ggplot2)

vh_line <- data.frame(
  xintercept = c(100,200),yintercept = c(10,20),cyl = c(4,4)
)

ggplot(mtcars,aes(hp,mpg,color = factor(cyl))) +
  geom_point() +
  geom_vline(data = vh_line,aes(xintercept = xintercept),linetype='dashed',size=1) +
  geom_hline(data = vh_line,aes(yintercept = yintercept),linetype='solid',size=1) +
  facet_wrap(~cyl)

相关问答

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