Tuesday, September 13, 2011

Y-axis on both sides in Kaplan-Meier Survival Curve

Here is an example of how to draw Y-axis on both sides in Kaplan-Meier Survival Curve, with same label, axis-titles and positions. This should work for the general plots as well.




require(survival)
#fit a Kaplan-Meier and plot it
fit <- survfit(Surv(time, status) ~ strata(x), data=aml)
plot(fit)


# Method 1
# KM plot with observed ranges in the axis
z=3.5
par(mar=c(z, z, z, z))
plot(fit, axes=FALSE, main = "Kaplan-Meier Survival Curve", lty = 1:2)
mtext("Observation time", SOUTH <-1, at=mean(range(fit$time)), line=2)
mtext("Cumulative Probability", WEST <-2, at=.5, line=2)
mtext("Cumulative Probability", EAST <-4, at=.5, line=2)
axis(2, at =fit$surv, labels = round(fit$surv,2))
axis(4, at =fit$surv, labels = round(fit$surv,2))
axis(1, at=fit$time,labels=fit$time)
box(lty = 1)
legend("bottomright", c("Maintained", "Nonmaintained"), lty = c(1,2))


The resulting graph is as follows




# Method 2
# KM plot with equally spaced ranges in the axis
z=3.5
par(mar=c(z, z, z, z))
plot(fit, axes=FALSE, main = "Kaplan-Meier Survival Curve", lty = 1:2)
mtext("Observation time", SOUTH <-1, at=mean(range(fit$time)), line=2)
mtext("Cumulative Probability", WEST <-2, at=.5, line=2)
mtext("Cumulative Probability", EAST <-4, at=.5, line=2)
y = seq(0,1,length=5)
axis(2, at =y, labels = y)
axis(4, at =y, labels = y)
x = seq(min(fit$time),max(fit$time),length=5)
axis(1, at=x,labels=x)
box(lty = 1)
legend("bottomright", c("Maintained", "Nonmaintained"), lty = c(1,2))

The resulting graph is as follows

No comments:

Post a Comment