numbers at risk on kaplan-meier curve

396 views
Skip to first unread message

Gary Collins

unread,
May 8, 2009, 3:30:40 AM5/8/09
to ggp...@googlegroups.com
I've been tweaking some lines, from a thread a few weeks ago, trying to
get a decent kaplan-meier curve with numbers at risk printed below the
x-axis.

Two things I'd welcome suggestions on (1) lining up the numbers at risk
with the time points on the k-m curve - it doesn't look to bad on the
screen, but doesn't look right when saved as a pdf and (2) there's a
white box around the legend which I can't seem to figure out how to
remove - unless there are other suggestions to prettify the legend.

I'm trying to generalise as much as possible and minimise on data
dependent tweaking so that the code would be useable with minimal
changes for different datasets.

Thanks

Gary

The code to reproduce the figure is

library(ggplot2)
require(survival)

fit <- survfit(Surv(time, status) ~ rx, data = colon)

df <- data.frame(
time = fit$time,
n.risk = fit$n.risk,
n.event = fit$n.event,
surv = fit$surv,
strata = gsub("rx=", "", summary(fit, censored = T)$strata),
upper = fit$upper,
lower = fit$lower
)

zeros <- data.frame(time = 0, surv = 1, strata = gsub("rx=", "",
levels(summary(fit)$strata)), upper = 1, lower = 1)

df <- rbind.fill(zeros, df)

p <- ggplot(df, aes(time, surv, colour = strata)) +
geom_step(size = 0.6) +
xlim(0, max(fit$time)) +
ylim(0, 1) +
xlab("days from randomization") +
ylab("survival probability") +
scale_colour_brewer(name = "Treatment", pal = "Set1") +
opts(legend.position = c(0.9, 0.9)) +
opts(legend.key = theme_rect(colour = NA))

times <- seq(0, max(fit$time), by = 500)

risk.data <- data.frame(strata = summary(fit, times = times, extend =
T)$strata,
time = summary(fit, times = times, extend = T)$time,
n.risk = summary(fit, times = times, extend = T)$n.risk)

data_table <- ggplot(risk.data, aes(x = time, y = strata, label =
format(n.risk, nsmall = 0)), colour = strata) +
geom_text(size = 3.5) +
theme_bw() +
scale_y_discrete(formatter = abbreviate, limits = c("rx=Obs",
"rx=Lev", "rx=Lev+5FU"), labels = c("Obs", "Lev", "Lev+5FU")) +
opts(panel.grid.major = theme_blank()) +
opts(panel.grid.minor = theme_blank()) +
opts(panel.border = theme_blank()) +
opts(axis.text.x = theme_blank()) +
opts(axis.ticks = theme_blank()) +
xlim(0, max(fit$time))

data_table <- data_table + opts(legend.position = "none") + xlab(NULL) +
ylab(NULL)

data_table <- data_table + opts(plot.margin = unit(c(-0.5, 0, 0, -0.75),
"lines"))

# + opts(plot.margin = unit(c(-0.5, 1, 0, 0.5), "lines")) + xlab(NULL) +
ylab(NULL)

Layout <- grid.layout(nrow = 2, ncol = 1,heights = unit (c(2, .25),
c("null", "null")))

vplayout <- function (...) {
grid.newpage()
pushViewport(viewport(layout = Layout))
}

subplot <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
mmplot <- function(a, b) {
vplayout()
print(a, vp = subplot(1, 1))
print(b, vp = subplot(2, 1))
}

#pdf("c:\\test.pdf", pointsize=11, paper="a4r", width=10, height=10)
mmplot(p, data_table)
#dev.off()

---------------------------------------------------------------
Dr Gary S Collins Tel: +44 (0)1865 284418
Centre for Statistics in Medicine Fax: +44 (0)1865 284424
Wolfson College Annexe www.csm-oxford.org.uk
University of Oxford
Linton Road
Oxford, OX2 6UD
---------------------------------------------------------------

Xie Chao

unread,
May 8, 2009, 5:08:09 AM5/8/09
to Gary Collins, ggp...@googlegroups.com
You can use opts(legend.background = theme_blank()) to remove the white box around the legend:
mmplot(p+opts(legend.background = theme_blank()), data_table)

Regards,
Xie Chao
Reply all
Reply to author
Forward
0 new messages