https://ellessenne.github.io/KMunicate-package/
Create KMunicate-Style Kaplan–Meier Plots
https://ellessenne.github.io/KMunicate-package/
Last synced: 14 days ago
JSON representation
Create KMunicate-Style Kaplan–Meier Plots
- Host: GitHub
- URL: https://ellessenne.github.io/KMunicate-package/
- Owner: ellessenne
- License: other
- Created: 2020-07-10T09:17:31.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-16T12:38:52.000Z (over 1 year ago)
- Last Synced: 2025-10-22T03:57:14.358Z (about 2 months ago)
- Language: R
- Homepage: https://ellessenne.github.io/KMunicate-package
- Size: 32.9 MB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ggplot2 - KMunicate - Style Plots (Data and models)
README
---
output: github_document
editor_options:
chunk_output_type: console
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.align = "center",
out.width = "90%",
dpi = 300,
warning = FALSE,
message = FALSE,
dev = "ragg_png"
)
```
# KMunicate-Style Kaplan–Meier Plots
[](https://app.codecov.io/gh/ellessenne/KMunicate-package?branch=master)
[](https://CRAN.R-project.org/package=KMunicate)
[](https://cran.r-project.org/package=KMunicate)
[](https://cran.r-project.org/package=KMunicate)
[](https://github.com/ellessenne/KMunicate-package/actions/workflows/R-CMD-check.yaml)
The goal of {KMunicate} is to produce Kaplan–Meier plots in the style recommended following the [KMunicate study](http://dx.doi.org/10.1136/bmjopen-2019-030215) (TP Morris *et al*. Proposals on Kaplan–Meier plots in medical research and a survey of stakeholder views: KMunicate. *BMJ Open*, 2019, 9:e030215).
## Installation
You can install {KMunicate} from [CRAN](https://CRAN.R-project.org/package=KMunicate) by typing the following in your R console:
``` r
install.packages("KMunicate")
```
Alternatively, you can install the dev version of {KMunicate} from [GitHub](https://github.com/ellessenne/KMunicate-package/) with:
``` r
# install.packages("devtools")
devtools::install_github("ellessenne/KMunicate-package")
```
## Example
```{r packages}
library(survival)
library(KMunicate)
```
The {KMunicate} package comes with a couple of bundled dataset, `cancer` and `brcancer`.
The main function is named `KMunicate`:
```{r brcancer, fig.height = 7, fig.width = 7 / sqrt(2)}
KM <- survfit(Surv(rectime, censrec) ~ hormon, data = brcancer)
time_scale <- seq(0, max(brcancer$rectime), by = 365)
KMunicate(fit = KM, time_scale = time_scale)
```
```{r cancer, fig.height = 7, fig.width = 7 / sqrt(2)}
KM <- survfit(Surv(studytime, died) ~ drug, data = cancer2)
time_scale <- seq(0, max(cancer2$studytime), by = 7)
KMunicate(fit = KM, time_scale = time_scale)
```
You also might wonder, does this work with a single arm?
Yes, yes it does:
```{r cancer-single, fig.height = 6, fig.width = 6}
KM <- survfit(Surv(studytime, died) ~ 1, data = cancer2)
time_scale <- seq(0, max(cancer2$studytime), by = 7)
KMunicate(fit = KM, time_scale = time_scale)
```
Finally, you can also plot 1 - survival by using the argument `.reverse = TRUE`:
```{r brcancer-reverse, fig.height = 7, fig.width = 7 / sqrt(2)}
KM <- survfit(Surv(rectime, censrec) ~ hormon, data = brcancer)
time_scale <- seq(0, max(brcancer$rectime), by = 365)
KMunicate(fit = KM, time_scale = time_scale, .reverse = TRUE)
```
## Customise Risk Table
By default, `KMunicate()` will build a risk table conform to the KMunicate style, e.g., with cumulative number of events and censored (the column-wise sum is equal to the total number of individuals at risk per arm):
```{r brcancer-KMunicate, fig.height = 7, fig.width = 7 / sqrt(2)}
KM <- survfit(Surv(rectime, censrec) ~ hormon, data = brcancer)
time_scale <- seq(0, max(brcancer$rectime), by = 365)
KMunicate(fit = KM, time_scale = time_scale)
```
Alternatively, it is possible to customise the risk table via the `.risk_table` argument.
For instance, if one wants to have interval-wise number of events and censored, just pass the `survfit` value to the `.risk_table` argument:
```{r brcancer-survfit, fig.height = 7, fig.width = 7 / sqrt(2)}
KMunicate(fit = KM, time_scale = time_scale, .risk_table = "survfit")
```
This is the default output of the `summary.survfit()` function.
Finally, it is also possible to fully omit the risk table by setting `.risk_table = NULL`:
```{r brcancer-NULL, fig.height = 6 / sqrt(2), fig.width = 6}
KMunicate(fit = KM, time_scale = time_scale, .risk_table = NULL)
```
## Custom Fonts
Assuming you have set up your computer to use custom fonts with `ggplot2`, customising your KMunicate-style plot is trivial.
All you have to do is pass the font name as the `.ff` argument:
```{r cancer-single-ff, fig.height = 6, fig.width = 6}
KM <- survfit(Surv(studytime, died) ~ 1, data = cancer2)
time_scale <- seq(0, max(cancer2$studytime), by = 7)
KMunicate(fit = KM, time_scale = time_scale, .ff = "Times New Roman")
```
## Further Customisation
Several options to further customise each plot are provided, see e.g. the introductory vignette for more details.