ggkm | R Documentation |
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.
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
)
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; |
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 |
ylims |
A numeric vector; The minimum and maximum values for the y-axis.
Default is |
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; |
pval_threshold |
Numeric; threshold to showing actual p-value or "P <
...". Defualt is |
pval_accuracy |
Number to round to, NULL for automatic guess. Default is
|
pvalpos |
A numeric vector; Position for the p-value on the plot if
|
text_annotate |
String; Text to show instead of p-value. |
marks |
Logical; |
shape |
Integer; Controls the shape of the censoring mark if |
point_size |
Double; Controls the size of the censoring mark. Default is 1.5. |
linetype |
Logical; if |
linecolor |
Logical; if |
blackandwhite |
Logical; if |
palette |
The color paletter to be used. Default are opinionated choices. |
legend |
Logical; if |
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 |
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 |
surv_plot_height |
the height of the survival plot on the grid. Default
is |
risk_table_height |
the height of the risk table on the grid. Increase
the value when you have many strata. Default is |
A plot
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.