如何用两个面板复制情节?

How to replicate plot with two panels?

我正在尝试复制这个情节,在这里:

这是该图的来源,幻灯片 89:

http://www.drizopoulos.com/courses/Int/JMwithR_CEN-ISBS_2017.pdf

图的顶部是随时间变化的风险函数,而底部绿色曲线是随时间变化的拟合线性混合效应模型。

我已经能够分别绘制这两个图,但是,似乎无法使用 par(mfrow=c(2,1)) 或 gridExtra 包(因为只有一个是 ggplot 对象)将它们组合起来。

我在 R 中使用辅助工具和 aids.id 数据集(作为 JM 包的一部分)


# Load packages JM and lattice
library("JM")
library("lattice")
library("ggplot2")

#Fit models
lmeFit.aids <- lme(CD4 ~ obstime + obstime:drug,
    random = ~ obstime | patient, data = aids)

coxFit.aids <- coxph(Surv(Time, death) ~ drug, data = aids.id, x = TRUE)

#Plot longitudinal process
p1<-ggplot(data=aids,aes(x=obstime,y=fitted(lmeFit.aids)))
p1<-p1+geom_smooth(se=FALSE)
p1

#Plot survival process 
library(rms)
p2<-psm(Surv(Time,death)~1,data=aids.id)
survplot(p2,what='hazard')


谢谢!

首先,patchwork 允许您将 ggplot2 和基础图形合并到一个图中。改编自 ?wrap_elements

library(ggplot2)
library(patchwork)
gg <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
gg / wrap_elements(full = ~ plot(mtcars$mpg, mtcars$disp))

我能够使用 survest() 函数提取不同时间点的危险值。然后,我可以使用 ggplot 来绘制它,这意味着我可以使用 grid.arrange().

est<-survest(p2,,what='hazard')
hazard<-data.frame(time=est$time, hazard=est$surv)