ggkmcif {reportRmd} | R Documentation |
Plot KM and CIF curves with ggplot
Description
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.
Usage
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
)
Arguments
response |
character vector with names of columns to use for response |
cov |
String specifying the column name of stratification variable |
data |
dataframe containing your data |
type |
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" |
pval |
boolean to specify if you want p-values in the plot (Log Rank test for KM and Gray's test for CIF) |
HR |
boolean to specify if you want hazard ratios included in the plot |
HR_pval |
boolean to specify if you want HR p-values in the plot |
conf.curves |
boolean to specify if you want confidence interval bands |
conf.type |
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)). |
table |
Logical value. If TRUE, includes the number at risk table |
times |
Numeric vector of times for the x-axis |
xlab |
String corresponding to xlabel. By default is "Time" |
ylab |
String corresponding to ylabel. When NULL uses "Survival probability" for KM cuves, and "Probability of an event" for CIF |
main |
String corresponding to main title. When NULL uses Kaplan-Meier Plot s, and "Cumulative Incidence Plot for CIF" |
stratalabs |
string corresponding to the labels of the covariate, when NULL will use the levels of the covariate |
strataname |
String of the covariate name default is nicename(cov) |
stratalabs.table |
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 |
strataname.table |
String of the covariate name for the number at risk table default is nicename(cov |
median.text |
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 |
median.lines |
boolean to specify if you want the median values added as lines to the plot, for KM only |
median.CI |
boolean to specify if you want the 95\ with the median text (Only for KM) |
set.time.text |
string for the text to add survival at a specified time (eg. year OS) |
set.time.line |
boolean to specify if you want the survival added as lines to the plot at a specified point |
set.time |
Numeric values of the specific time of interest, default is 5 (Multiple values can be entered) |
set.time.CI |
boolean to specify if you want the 95\ interval with the set time text |
censor.marks |
logical value. If TRUE, includes censor marks (only for KM curves) |
censor.size |
size of censor marks, default is 3 |
censor.stroke |
stroke of censor marks, default is 1.5 |
fsize |
font size |
nsize |
font size for numbers in the numbers at risk table |
lsize |
line size |
psize |
size of the pvalue |
median.size |
size of the median text (Only when there are no covariates) |
median.pos |
vector of length 2 corresponding to the median position (Only when there are no covariates) |
median.lsize |
line size of the median lines |
set.size |
size of the survival at a set time text (Only when there are no covariates) |
set.pos |
vector of length 2 corresponding to the survival at a set point position (Only when there are no covariates) |
set.lsize |
line size of the survival at set points |
ylim |
vector of length 2 corresponding to limits of y-axis. Default to NULL |
col |
vector of colours |
linetype |
vector of line types |
xlim |
vector of length 2 corresponding to limits of x-axis. Default to NULL |
legend.pos |
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)) |
pval.pos |
vector of length 2 corresponding to the p-value position |
plot.event |
Which event(s) to plot (1,2, or c(1,2)) |
event |
String specifying if the event should be mapped to the colour, or linetype when plotting both events to colour = "col", line type |
flip.CIF |
boolean to flip the CIF curve to start at 1 |
cut |
numeric value indicating where to divide a continuous covariate (default is the median) |
eventlabs |
String corresponding to the event type names |
event.name |
String corresponding to the label of the event types |
Numbers_at_risk_text |
String for the label of the number at risk |
HR.digits |
Number of digits printed of the hazard ratio |
HR.pval.digits |
Number of digits printed of the hazard ratio pvalue |
pval.digits |
Number of digits printed of the Gray's/log rank pvalue |
median.digits |
Number of digits printed of the median pvalue |
set.time.digits |
Number of digits printed of the probability at a specified time |
returns |
Logical value returns a list with all ggplot objects in a list |
print.n.missing |
Logical, should the number of missing be shown !Needs to be checked |
Details
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")
Value
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.
Examples
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)