Below is a dataset I extracted from a survfit object using the survival
package.
> km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
I need reconstruct the KM curve using the table km_data
above (rather than plotting directly from the survfit object), but now sure how. Any ideas on how I can do this in R, ideally using ggplot2
? Thank you!
Use the ggsurvplot
function of the package survminer
which is built on top of ggplot2
and can be used to create Kaplan-Meier plots.
library(survminer)
km_data %>% ggsurvplot()
Update 1
Update for new data
km_data <- data.table(time = c(72, 109, 143),
n.risk = c(80, 77, 76),
n.event = c(1, 1, 2),
n.censor = c(3, 2, 0),
surv = c(0.9875, 0.974675324675325, 0.949025974025974),
strata = c(0, 0, 0),
type = "right")
library(survminer)
km_data %>% ggsurvplot()