library(survival)
library(ggplot2)
library(broom) # tidy() model data
library(grid) # grid.draw() finished plot
# Data with group names specified
data_diabetic <- diabetic
data_diabetic$trt <- as.factor(data_diabetic$trt)
levels(data_diabetic$trt) <- c('None', 'Laser')
# Survival Model
fit <- survfit(Surv(time, status) ~ trt, data = data_diabetic)
# Kaplan Meier (KM) Plot
plot_km <- ggplot(
data = tidy(fit),
mapping = aes(x = time, y = estimate)
) +
geom_step(aes(color = strata)) +
geom_stepconfint(aes(ymin = conf.low, ymax = conf.high, fill = strata), alpha = 0.3) +
coord_cartesian(c(0, 50)) + # Note scale set here!
scale_x_continuous(expand = c(0.02,0)) +
labs(x = 'Time', y = 'Freedom From Event') +
scale_color_manual(
values = c('#d83641', '#1A45A7'),
name = 'Treatment',
labels = c('Laser', 'None'),
aesthetics = c('colour', 'fill')) +
theme_basic()
# Risk Table
tbl_risk <- ggrisktable(fit, c(0, 10, 20, 30, 40, 50)) +
coord_cartesian(c(0, 50)) +
scale_x_continuous(expand = c(0.02,0)) +
theme_risk()
# Combine KM plot and risk table
plot_cmbd <- append_table(
plot = plot_km,
table = tbl_risk
)
# Draw in RStudio viewer
grid.newpage()
grid.draw(plot_cmbd)
Run the code above in your browser using DataLab