This function will plot a KM or CIF curve with option to add the number at risk. You can specify if you want confidence bands, the hazard ratio, and pvalues, as well as the units of time used.
ggkmcif(
response,
cov = NULL,
data,
type = NULL,
pval = TRUE,
HR = FALSE,
HR_pval = FALSE,
conf.curves = FALSE,
conf.type = "log",
table = TRUE,
times = NULL,
xlab = "Time",
ylab = NULL,
main = NULL,
stratalabs = NULL,
strataname = nicename(cov),
stratalabs.table = NULL,
strataname.table = strataname,
median.text = FALSE,
median.lines = FALSE,
median.CI = FALSE,
set.time.text = NULL,
set.time.line = FALSE,
set.time = 5,
set.time.CI = FALSE,
censor.marks = TRUE,
censor.size = 3,
censor.stroke = 1.5,
fsize = 10,
nsize = 3,
lsize = 1,
psize = 3.5,
median.size = 3,
median.pos = NULL,
median.lsize = 1,
set.size = 3,
set.pos = NULL,
set.lsize = 1,
ylim = c(0, 1),
col = NULL,
linetype = NULL,
xlim = NULL,
legend.pos = NULL,
pval.pos = NULL,
plot.event = 1,
event = c("col", "linetype"),
flip.CIF = FALSE,
cut = NULL,
eventlabs = NULL,
event.name = NULL,
Numbers_at_risk_text = "Numbers at risk",
HR.digits = 2,
HR.pval.digits = 3,
pval.digits = 3,
median.digits = 3,
set.time.digits = 3,
returns = FALSE,
print.n.missing = TRUE
)
Nothing is returned unless returns = TRUE is used. With returns = TRUE, if table=TRUE (the default) a table style graphic with survival plot and number at risk table is returned. Otherwise a plot with the survival curves is returned.
character vector with names of columns to use for response
String specifying the column name of stratification variable
dataframe containing your data
string indicating he type of univariate model to fit. The function will try and guess what type you want based on your response. If you want to override this you can manually specify the type. Options include "KM", and ,"CIF"
boolean to specify if you want p-values in the plot (Log Rank test for KM and Gray's test for CIF)
boolean to specify if you want hazard ratios included in the plot
boolean to specify if you want HR p-values in the plot
boolean to specify if you want confidence interval bands
One of "none"(the default), "plain", "log" , "log-log" or "logit". Only enough of the string to uniquely identify it is necessary. The first option causes confidence intervals not to be generated. The second causes the standard intervals curve +- k *se(curve), where k is determined from conf.int. The log option calculates intervals based on the cumulative hazard or log(survival). The log-log option bases the intervals on the log hazard or log(-log(survival)), and the logit option on log(survival/(1-survival)).
Logical value. If TRUE, includes the number at risk table
Numeric vector of times for the x-axis
String corresponding to xlabel. By default is "Time"
String corresponding to ylabel. When NULL uses "Survival probability" for KM cuves, and "Probability of an event" for CIF
String corresponding to main title. When NULL uses Kaplan-Meier Plot s, and "Cumulative Incidence Plot for CIF"
string corresponding to the labels of the covariate, when NULL will use the levels of the covariate
String of the covariate name default is nicename(cov)
String corresponding to the levels of the covariate for the number at risk table, when NULL will use the levels of the covariate. Can use a string of "-" when the labels are long
String of the covariate name for the number at risk table default is nicename(cov
boolean to specify if you want the median values added to the legend (or as added text if there are no covariates), for KM only
boolean to specify if you want the median values added as lines to the plot, for KM only
boolean to specify if you want the 95\ with the median text (Only for KM)
string for the text to add survival at a specified time (eg. year OS)
boolean to specify if you want the survival added as lines to the plot at a specified point
Numeric values of the specific time of interest, default is 5 (Multiple values can be entered)
boolean to specify if you want the 95\ interval with the set time text
logical value. If TRUE, includes censor marks (only for KM curves)
size of censor marks, default is 3
stroke of censor marks, default is 1.5
font size
font size for numbers in the numbers at risk table
line size
size of the pvalue
size of the median text (Only when there are no covariates)
vector of length 2 corresponding to the median position (Only when there are no covariates)
line size of the median lines
size of the survival at a set time text (Only when there are no covariates)
vector of length 2 corresponding to the survival at a set point position (Only when there are no covariates)
line size of the survival at set points
vector of length 2 corresponding to limits of y-axis. Default to NULL
vector of colours
vector of line types
vector of length 2 corresponding to limits of x-axis. Default to NULL
Can be either a string corresponding to the legend position ("left","top", "right", "bottom", "none") or a vector of length 2 corresponding to the legend position (uses normalized units (ie the c(0.5,0.5) is the middle of the plot))
vector of length 2 corresponding to the p-value position
Which event(s) to plot (1,2, or c(1,2))
String specifying if the event should be mapped to the colour, or linetype when plotting both events to colour = "col", line type
boolean to flip the CIF curve to start at 1
numeric value indicating where to divide a continuous covariate (default is the median)
String corresponding to the event type names
String corresponding to the label of the event types
String for the label of the number at risk
Number of digits printed of the hazard ratio
Number of digits printed of the hazard ratio pvalue
Number of digits printed of the Gray's/log rank pvalue
Number of digits printed of the median pvalue
Number of digits printed of the probability at a specified time
Logical value returns a list with all ggplot objects in a list
Logical, should the number of missing be shown !Needs to be checked
Note that for proper pdf output of special characters the following code needs to be included in the first chunk of the rmd knitr::opts_chunk$set(dev="cairo_pdf")
data("pembrolizumab")
# Simple plot without confidence intervals
ggkmcif(response = c('os_time','os_status'),
cov='cohort',
data=pembrolizumab)
# Plot with median survival time
ggkmcif(response = c('os_time','os_status'),
cov='sex',
data=pembrolizumab,
median.text = TRUE,median.lines=TRUE,conf.curves=TRUE)
# Plot with specified survival times and log-log CI
ggkmcif(response = c('os_time','os_status'),
cov='sex',
data=pembrolizumab,
median.text = FALSE,set.time.text = 'mo OS',
set.time = c(12,24),conf.type = 'log-log',conf.curves=TRUE)
# KM plot with 95% CI and censor marks
ggkmcif(c('os_time','os_status'),'sex',data = pembrolizumab, type = 'KM',
HR=TRUE, HR_pval = TRUE, conf.curves = TRUE,conf.type='log-log',
set.time.CI = TRUE, censor.marks=TRUE)
Run the code above in your browser using DataLab