Created
June 27, 2022 12:54
-
-
Save dBenedek/c9b3b517c1781835a9e53e9aa4af6fc8 to your computer and use it in GitHub Desktop.
[KM plot] Plotting a KM survival plot #km #plot #survival
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("tidyverse") | |
library("survival") | |
library("survminer") | |
# Fit model on survival data: | |
fit01 <- survfit(Surv(time=Time_survival, | |
event=Status_survival) ~ as.factor(group), | |
data = clin_dat) | |
fit01_cph <- coxph(Surv(time=Time_survival, | |
event=Status_survival) ~ as.factor(group), | |
data = clin_dat) | |
# Extract HR, p-value: | |
fit01_hr <- summary(fit01_cph)$conf.int[1] %>% | |
as.numeric() %>% | |
round(3) | |
fit01_hr_ci_lower <- summary(fit01_cph)$conf.int[3] %>% | |
as.numeric() %>% | |
round(3) | |
fit01_hr_ci_upper <- summary(fit01_cph)$conf.int[4] %>% | |
as.numeric() %>% | |
round(3) | |
fit01_p <- summary(fit01_cph)$logtest[3] %>% | |
as.numeric() %>% | |
round(3) | |
# Annotation text: | |
annot <- paste0("HR=", fit01_hr, " (95% CI=", fit01_hr_ci_lower, "-", | |
fit01_hr_ci_upper, ")\n", | |
"p=", fit01_p) | |
# Create plot object: | |
plot <- ggsurvplot( | |
fit01, # survfit object with calculated statistics. | |
pval = F, # show p-value of log-rank test. | |
conf.int = TRUE, # show confidence intervals for | |
title="Disease-specific survival", | |
conf.int.style = "step", # customize style of confidence intervals | |
xlab = "Time in days", # customize X axis label. | |
break.time.by = 200, # break X axis in time intervals by 200. | |
ggtheme = theme_light(), # customize plot and risk table with a theme. | |
risk.table =T, # absolute number and percentage at risk. | |
risk.table.y.text.col = T,# colour risk table text annotations. | |
risk.table.y.text = T, # in legend of risk table. | |
ncensor.plot = F, # plot the number of censored subjects at time t | |
surv.median.line = "hv",#, # add the median survival pointer. | |
legend.labs = c("MPS1", "MPS2"), # change legend labels. | |
palette = c("#E7B800", "#2E9FDF")) | |
# Add annotation text: | |
plot$plot <- plot$plot + | |
ggplot2::annotate( | |
"text", | |
x = Inf, y = Inf, | |
vjust = 1, hjust = 1, | |
label = annot, | |
size = 5 | |
) | |
# Plot: | |
plot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment