0

I had drawn kaplan meier plot for stanford heart transplant data.

library(tidyverse)
library(survival)
library(survminer)
library(flexsurv)
library(readxl)
data=read_excel("Desktop/stanford_heart_transplant_dataset_full.xlsx")
data
names(data)
data_log <- data %>%
  mutate(log_time = log(SURVIVAL_TIME))
data_log$log_time
names(data_log)[enter image description here][1]
survobj2_new=Surv(data_log$log_time, data_log$SURVIVAL_STATUS);survobj2_new
KM_new=survfit(survobj2_new~1,data_log);KM_new
p=ggsurvplot(KM_new, data = data_log, time = "log_time", conf.int = FALSE)
p

enter image description here

Next I had drawn survival plot for the LOLLW with the estimated parameters

t1=data_log$log_time;t1
t2=data_log$AGE;t2
t3=data_log$PRIOR_SURGERY;t3
t4=data_log$TRANSPLANT_STATUS;t4
alpha <- 4.6
sigma <- 6.2
b0 <- 8.7
b1 <- -0.07
b2 <- 1.4
b3 <- 2.5
surv_function=function(alpha,sigma,b0,b1,b2,b3)
{
  y=data_log$log_time
  z=(y-b0-b1*t2-b2*t3-b3*t4)/sigma;
  u2=exp(-exp(z))
  l2=(1-(((1-u2)^alpha)/(((1-u2)^alpha)+((u2)^alpha))))
  return(l2)[enter image description here][1]
}
x=data_log$log_time
y=surv_function(4.6,6.2,8.7,-0.07,1.4,2.5)
data <- data.frame(x = x, y = y)
p1=ggplot(data_log, aes(x = x, y = y)) +
  geom_smooth()
p1

Now I tried multiple ways to merge these two plot.I would like to get a sample plot like belowenter image description here.How to get the plots merged into a single plot.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • 2
    This is basically the same question as your [former](https://stackoverflow.com/questions/76845091/how-to-merge-two-survival-plots) one. And the solution still applies. But as you use a second dataset for the `geom_smooth` you have to do `p$plot + geom_smooth(data = data_log, aes(x = x, y = y))`. – stefan Aug 09 '23 at 17:25

0 Answers0