ggkm: Kaplan-Meier survival plot

View source: R/ggkm.R

ggkmR Documentation

Kaplan-Meier survival plot

Description

I mostly user the package survminer when making quick and easy survival plots. But when I want to make a plot for publication or for higher quality, I will use this function ggkm. Most of the code was cobbled together from many different versions of this function that people have shared online. Just google "ggkm" and you'll see them. My version allows me to make the tweaks and adjustments that I want when making these plots.

Usage

ggkm(
  sfit,
  main = "Kaplan-Meier Plot",
  subtitle = NA,
  risk_table = TRUE,
  risk_title = "Number at risk",
  timeby = 100,
  xlims = c(0, max(sfit$time)),
  ylims = c(0, 1.01),
  xlabs = "Time",
  ylabs = "Survival Probability (%)",
  ystratalabs = NULL,
  ystrataname = NULL,
  yaccuracy = 1,
  yscale = 100,
  ysuffix = "",
  pval = TRUE,
  pval_threshold = 0.001,
  pval_accuracy = 0.001,
  pvalpos = c(max(sfit$time)/6, 0.2),
  text_annotate = NA,
  marks = FALSE,
  shape = 3,
  point_size = 1.5,
  linetype = FALSE,
  linecolor = TRUE,
  blackandwhite = FALSE,
  palette = c("#0073C2", "#EFC000", "#868686", "#CD534C", "#7AA6DC", "#003C67",
    "#8F7700", "#3B3B3B", "#A73030", "#4A6990"),
  legend = TRUE,
  legpos = c(0.9, 0.8),
  subs = NULL,
  font_family = "Arial",
  adj_table_title = -0.1,
  adj_y_axis_label = -12.5,
  surv_plot_height = 1 - risk_table_height,
  risk_table_height = length(levels(summary(sfit)$strata)) * 0.12
)

Arguments

sfit

A survfit object

main

String; Main plot title. Default is "Kaplan-Meier Plot".

subtitle

String; Subtitle for the plot. Default is NA.

risk_table

Logical; TRUE will show the risk table. Default is FALSE

risk_title

String; title for the risk table. Default is "Number at risk".

timeby

Integer; amount to break the x-axis by.

xlims

A numeric vector; The minimum and maximum values for the x-axis. Default is 0 and max time in the survfit obeject.

ylims

A numeric vector; The minimum and maximum values for the y-axis. Default is 0 to 1.01 to give a little space at the top.

xlabs

String; Label for the x-axis. Default is "Time".

ylabs

String; Label for the y-axis. Default is "Survival Probability (%)"

ystratalabs

String vector; Labels for the strata

ystrataname

String; The title for the strata.

yaccuracy

A number to round to. Use (e.g.) 0.01 to show 2 decimal places of precision. If NULL, the default, uses a heuristic that should ensure breaks have the minimum number of digits needed to show the difference between adjacent values.

yscale

A scaling factor: x will be multiplied by scale before formatting. This is useful if the underlying data is very small or very large.

ysuffix

Additional text to display after the number.

pval

Logical; TRUE is default and shows a p-value onn the plot.

pval_threshold

Numeric; threshold to showing actual p-value or "P < ...". Defualt is 0.001.

pval_accuracy

Number to round to, NULL for automatic guess. Default is 0.001.

pvalpos

A numeric vector; Position for the p-value on the plot if pval is TRUE. Default is in the lower left corner of the plot.

text_annotate

String; Text to show instead of p-value.

marks

Logical; TRUE will show marks for censoring. Default is FALSE

shape

Integer; Controls the shape of the censoring mark if TRUE. Default is 3, a cross shape. Other good option is 4 = X or 124 = |.

point_size

Double; Controls the size of the censoring mark. Default is 1.5.

linetype

Logical; if TRUE then lines will be different types: solid, dashed, dotted, etc. Default is FALSE.

linecolor

Logical; if TRUE then lines are different colors. Default is TRUE.

blackandwhite

Logical; if TRUE, plot is made in black and white using R's gray.colors

palette

The color paletter to be used. Default are opinionated choices.

legend

Logical; if TRUE then the legend is shown. Default is TRUE.

legpos

Numeric vector; Position of the legend. Default is the upper right corner of the plot.

subs

Default is NULL

font_family

String name of the font to be used; Default is "Arial".

adj_table_title

Numeric; adjusts the postion of the title for the Risk Table when used. Combining plots will move this label around. This argument will artifically move the label out of place so that it ends up in the right place. Default is -0.20. __More negative moves left__. This takes trial and error.

adj_y_axis_label

Numeric; adjusts the postion of the title for the Risk Table when used. Combining plots will move this label around. This argument will artifically move the label out of place so that it ends up in the right place. Default is -12.5. __More negative moves right__. This takes trial and error.

surv_plot_height

the height of the survival plot on the grid. Default is 1 - length(levels(summary(fit)$strata)) * .12. Ignored when risk.table = FALSE.

risk_table_height

the height of the risk table on the grid. Increase the value when you have many strata. Default is length(levels(summary(fit)$strata)) * .12. Ignored when risk.table = FALSE.

Value

A plot

Examples

## Not run: 
library(survival)
library(extrafont)
#### Load fonts --------------------------------
# extrafont::fonts()  # Vector of font family names
# extrafont::fonttable()  # Show entire table
# extrafont::font_import()  # imports fonts installed on the system
extrafont::loadfonts(device = "win", quiet = TRUE)
data(colon)
fit <- survfit(Surv(time, status) ~ 1, data = colon)
ggkm(fit, timeby = 500, marks = TRUE)
ggkm(fit, timeby = 500)

fit <- survfit(Surv(time, status) ~ rx, data = colon)
ggkm(fit,
     timeby = 500,
     marks = TRUE,
     ystratalabs = c("Observed",
                     "Level",
                     "Level+"),
     adj_table_title = -0.12)

sfit <- survfit(Surv(time, status) ~ rx, data = colon)
foo <- ggkm(sfit = sfit,
            risk_table = TRUE,
            xlabs = "Time (in months)",
            ylabs = "Survival Probability (%)",
            xlims = c(0, max(sfit$time)),
            ylims = c(0, 1.01),
            ystratalabs = NULL,
            ystrataname = NULL,
            timeby = 500,
            main = "Kaplan-Meier Plot",
            pval = FALSE,
            marks = TRUE,
            linetype = FALSE,
            linecolor = TRUE,
            shape = 3,  # 3 = cross, 4 = X
            legend = TRUE,
            subs = NULL,
            legpos = c(1, 1),
            text_annotate = "Here is some text",
            pvalpos = c(500, 0.25),
            font_family = "Times New Roman")

foo

## End(Not run)


emilelatour/laviz documentation built on Nov. 11, 2024, 9:42 p.m.