0

Please modify the code so that it includes "table at risk" under each KM curve (i.e., for each landmark set).

library(survival)
library(tidyverse)

mod1 - survfit (Surv(time, status) ~ sex, data = lung)

df     <- as.data.frame(unclass(mod1)[c(2:7, 15:16)])
df$sex <- rep(c("Male", "Female"), times = mod1$strata)

df %>%
  filter(time < 300) %>%
  group_by(sex) %>%
  mutate(period = factor(100 * floor(time / 100))) %>%
  group_by(sex, period) %>%
  mutate(surv = surv / first(surv)) %>%
  ggplot(aes(time, surv, color = sex, group = interaction(period, sex))) + 
  geom_step(size = 1) +
  geom_vline(xintercept = c(0, 100, 200, 300), linetype = 2) +
  scale_color_manual(values = c("deepskyblue4", "orange")) +
  theme_minimal(base_size = 16) +
  theme(legend.position = "top")

This code was provided by Allan Cameron. Build a plot made up of multiple plots

Is there a simpler way to produce KM curves before and after a given landmark time (like in the image enclosed)?

![KM curve with a landmark] 1

I was unable to add "table at risk". Not even with chatGPT :-)

0 Answers0