# Plot the Kaplan-Meier (K-M) curves from two different K-M calculations on the same plot

The motivating data set for this example is the `Rtestdata.sav` SPSS data set.

```library(Hmisc)
library(Design)
test <- spss.get("Rtestdata.sav", lowernames=TRUE)
contents(test)
```
NOTE: You;ll most likely receive a warning when you read in the SPSS data set, but just ignore it.

The problem is that the data set has two subjects for which we are not too sure when the died --- whether within or after two years.

```subset(test, event != died)
```

Therefore, we would like to calculate the K-M survival estimates in two separate ways and plot them on the same plot.

Before we do this, we need to make one minor change to the data set --- we need to recode all `follup2d` values of > 730 with 730.

```test <- upData(test,
trunc.folup2d = ifelse(folup2d > 730,
730, folup2d))
```

Now let's calculate the K-M survival estimates in two different ways: (1) assuming the two questionable subjects died within 2 years; and (2) assuming the two questionable subjects died after 2 years.

```# (1) Died within 2 years
f.win2y <- survfit(with(test, Surv(trunc.folup2d, event=="Yes")))
# (2) Died after 2 years
f.after2y <- survfit(with(test, Surv(trunc.folup2d, died=="Yes")))
```

And now let's plot the two K-M calculations on the same plot. We can actually generate the plot in several ways, which are shown below. See the 'Survival analysis with the survival and Design packages' link for more information.

```# (1) Plot using the plot.survfit() function (survival package)
plot(f.win2y, xmax=730, ymin=0.5,
xlab="Time in days", ylab="Survival function")
par(new=TRUE)
plot(f.after2y, xmax=730, ymin=0.5,
xlab="", ylab="", col="red")
par(col="black")
box()
legend(x=25, y=0.5,
legend=c("Assuming died within 2 years",
"Assuming died after 2 years"),
col=c("black", "red"), lty=1,
xjust=0, yjust=0)

# Can also add the f.after2y Survival curve and CIs
#   using the lines() function, but it does not show
#   censoring tick marks
plot(f.win2y, xmax=730, ymin=0.5,
xlab="Time in days", ylab="Survival function")
# NOTE: lines function does not properly _step_ down
#  the survival curve.  Will want to use the stepfun
with(f.after2y, lines(time, lower, lty=2, col="red"))
lines(f.after2y, col="red", mark.time=TRUE)
with(f.after2y, lines(time, lower, lty=2, col="red"))

# (2) Plot using survplot --> survplot.Design (Design package)
# Doesn't show censoring
survplot(f.win2y,
conf="bands", time.inc=60,
xlim=c(0, 730), ylim=c(0.5, 1.0))
conf="bands", time.inc=60,
xlim=c(0, 730), ylim=c(0.5, 1.0))
box()
legend(x=25, y=0.5,
legend=c("Assuming died within 2 years",
"Assuming died after 2 years"),
col=c("black", "red"), lty=1,
xjust=0, yjust=0)
```
Topic revision: r2 - 14 Nov 2006, TheresaScott

• Biostatistics Webs Copyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback