R example code for KM plot with number of subjects at risk
- User
R example code for KM plot with number of subjects at risk
- Assistant
Here's an example R code for creating a Kaplan-Meier plot with the number of subjects at risk at each time point:
code# Load necessary libraries library(survival) library(survminer) # Load example dataset data(lung) # Create a survival object lung_surv <- Surv(lung$time, lung$status) # Create a Kaplan-Meier plot with the number of subjects at risk ggsurvplot(survfit(lung_surv) , risk.table = TRUE, risk.table.col = "strata", risk.table.y.text = FALSE, legend.labs = c("Chemo", "Control"), palette = c("#E69F00", "#56B4E9"), title = "Kaplan-Meier Plot", xlab = "Time (days)", ylab = "Survival Probability", ylim = c(0, 1), pval = TRUE)