ggSurv | R Documentation |
Display a non-parametric or semi-parametric survival curve.
ggSurv(object, ...)
## S3 method for class 'survfit'
ggSurv(object, ...)
## S3 method for class 'coxph'
ggSurv(object, data = NULL, newdata = NULL, confint = FALSE, ...)
## S3 method for class 'data.table'
ggSurv(
object,
format = "data.table",
var.time = "time",
var.survival = "survival",
var.ci.inf = NULL,
var.ci.sup = NULL,
var.event = NULL,
var.censor = NULL,
name.strata = NULL,
var.strata = NULL,
plot = TRUE,
legend.position = "top",
title = NULL,
text.size = NULL,
ylim = NULL,
line.size = 2,
confint = FALSE,
alpha.CIfill = 0.2,
alpha.CIline = 0.5,
censoring = FALSE,
alpha.censoring = 1,
colour.censoring = rgb(0.2, 0.2, 0.2),
shape.censoring = 3,
size.censoring = 2,
name.censoring = "censoring",
events = FALSE,
alpha.events = 1,
colour.events = rgb(0.2, 0.2, 0.2),
shape.events = 8,
size.events = 2,
name.events = "event",
...
)
object |
a coxph object or a survfit object or a data.table object. |
... |
arguments to be passed to lower level functions |
data |
the data that have been used to fit the model. |
newdata |
dataset containing the covariates conditional to which the survival is displayed. |
confint , alpha.CIfill , alpha.CIline |
how to display confidence intervals |
format |
the format used to export the data. Can be data.table or data.frame. |
var.time |
the name of the column in the data.table object containing the time variable |
var.survival |
the name of the column in the data.table object containing the survival at each time |
var.ci.inf |
the name of the column in the data.table object containing the lower bound of the confidence interval |
var.ci.sup |
the name of the column in the data.table object containing the upper bound of the confidence interval |
var.event |
the name of the column in the data.table object containing the number of events at each time |
var.censor |
the name of the column in the data.table object containing the number of censored patients at each time |
name.strata |
the title for the color and fill legend in the plot. |
var.strata |
the name of the column in the data.table object containing the strata variable |
plot |
[logical] should the plot be displayed? |
legend.position , title , text.size |
annotations on the plot |
ylim |
y range (survival) |
line.size |
size of the survival curves |
censoring , alpha.censoring , colour.censoring , shape.censoring , size.censoring , name.censoring |
how to display censored events |
events , alpha.events , colour.events , shape.events , size.events , name.events |
how to display non-censored events |
A list:
plot: the ggplot object
data: the data used to create the ggplot object
library(lava)
library(survival)
library(data.table)
set.seed(10)
n <- 100
newdata <- data.frame(X1=1)
time <- 0.25
## simulate non proportional hazard using lava
m <- lvm()
regression(m) <- y ~ 1
regression(m) <- s ~ exp(-2*X1+X2)
distribution(m,~X1+X2) <- binomial.lvm()
distribution(m,~cens) <- coxWeibull.lvm(scale=1)
distribution(m,~y) <- coxWeibull.lvm(scale=1,shape=~s)
eventTime(m) <- eventtime ~ min(y=1,cens=0)
d <- as.data.table(lava::sim(m,n))
setkey(d, eventtime)
## Cox (PH)
## no regressor
m.cox <- coxph(Surv(eventtime, status) ~ 1, data = d, y = TRUE, x = TRUE)
res1 <- ggSurv(m.cox)
## regressors
m.cox <- coxph(Surv(eventtime, status) ~ X1, data = d, y = TRUE, x = TRUE)
res1 <- ggSurv(m.cox)
ggSurv(m.cox, newdata = d[,.SD[1], by = X1])
ggSurv(m.cox, newdata = d[,.SD[1], by = X1], confint = TRUE)
## Cox (stratified)
mStrata.cox <- coxph(Surv(eventtime, status) ~ strata(X1), data = d,
y = TRUE, x = TRUE)
ggSurv(mStrata.cox, censoring = TRUE, event = TRUE)
mStrata.cox <- coxph(Surv(eventtime, status) ~ strata(X1) + strata(X2), data = d,
y = TRUE, x = TRUE)
ggSurv(mStrata.cox, censoring = TRUE, event = TRUE)
## KM
m.KM <- survfit(Surv(eventtime, status) ~ X1, data = d)
res2 <- ggSurv(m.KM)
## combine plot
res1$data[,X1 := as.numeric(factor(strata2, levels = c("X1=0","X1=1")))-1]
res1$data[,strata2 := NULL]
dt.gg <- rbind(cbind(res1$data[original == TRUE & X1==1], model = "cox"),
cbind(res2$data[original == TRUE & X1==1], model = "KM")
)
dt.gg[,c("X1","original") := NULL]
ggSurv(dt.gg, var.time = "time", var.survival = "survival", var.strata = "model")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.