Title: | Selected Utilities Extending 'ggplot2' |
---|---|
Description: | Selected utilities, in particular 'geoms' and 'stats' functions, extending the 'ggplot2' package. This package imports functions from 'EnvStats' <doi:10.1007/978-1-4614-8456-1> by Millard (2013), 'ggpp' <https://CRAN.R-project.org/package=ggpp> by Aphalo et al. (2023) and 'ggstats' <doi:10.5281/zenodo.10183964> by Larmarange (2023), and then exports them. This package also contains modified code from 'ggquickeda' <https://CRAN.R-project.org/package=ggquickeda> by Mouksassi et al. (2023) for Kaplan-Meier lines and ticks additions to plots. All functions are tested to make sure that they work reliably. |
Authors: | Daniel Sabanés Bové [aut, cre], Samer Mouksassi [aut] (wrote original Kaplan-Meier code), Michael Sachs [aut] (wrote original Kaplan-Meier code), F. Hoffmann-La Roche AG [cph, fnd] |
Maintainer: | Daniel Sabanés Bové <[email protected]> |
License: | Apache License 2.0 |
Version: | 0.3.2.9002 |
Built: | 2024-11-15 05:52:55 UTC |
Source: | https://github.com/insightsengineering/ggplot2.utils |
ggplot2.utils
Packageggplot2.utils
provides simple access to utility functions
extending ggplot2
.
Currently all of the functions are imported from other extension packages:
ggpp
: geom_table()
and associated functions.
EnvStats
: stat_n_text()
and associated functions.
ggstats
: stat_prop()
and associated functions.
Maintainer: Daniel Sabanés Bové [email protected]
Authors:
Samer Mouksassi (wrote original Kaplan-Meier code)
Michael Sachs (wrote original Kaplan-Meier code)
Other contributors:
F. Hoffmann-La Roche AG [copyright holder, funder]
Useful links:
Report bugs at https://github.com/insightsengineering/ggplot2.utils/issues
Adds the Kaplan-Meier survival curve.
geom_km( mapping = NULL, data = NULL, stat = "km", position = "identity", show.legend = NA, inherit.aes = TRUE, na.rm = TRUE, ... )
geom_km( mapping = NULL, data = NULL, stat = "km", position = "identity", show.legend = NA, inherit.aes = TRUE, na.rm = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
na.rm |
If |
... |
Other arguments passed on to
|
geom_km()
understands the following aesthetics (required aesthetics in bold):
x
: the survival/censoring times, automatically mapped by stat_km()
.
y
: the survival probability estimates, automatically mapped by stat_km()
.
alpha
color
linetype
linewidth
Inspired by geom_km
written by Michael Sachs (in ggkm
) and
Samer Mouksassi (in ggquickeda
). Here we directly use ggplot2::geom_step()
instead of the more general ggplot2::geom_path()
.
The default stat
for this geom
is stat_km()
.
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex))) + geom_km()
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex))) + geom_km()
Adds tickmarks at the times when there are censored observations but no events.
geom_km_ticks( mapping = NULL, data = NULL, stat = "km_ticks", position = "identity", show.legend = NA, inherit.aes = TRUE, na.rm = TRUE, ... )
geom_km_ticks( mapping = NULL, data = NULL, stat = "km_ticks", position = "identity", show.legend = NA, inherit.aes = TRUE, na.rm = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
na.rm |
If |
... |
Other arguments passed on to
|
geom_km_ticks()
understands the following aesthetics (required aesthetics in bold):
x
: the survival/censoring times, automatically mapped by stat_km_ticks()
.
y
: the survival probability estimates, automatically mapped by stat_km_ticks()
.
alpha
color
shape
size
stroke
fill
Michael Sachs (in ggkm
), Samer Mouksassi (in ggquickeda
).
The default stat
for this geom
is stat_km_ticks()
.
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex), group = factor(sex))) + geom_km() + geom_km_ticks(col = "black")
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex), group = factor(sex))) + geom_km() + geom_km_ticks(col = "black")
This stat
is for computing the Kaplan-Meier survival estimate for
right-censored data. It requires the aesthetic mapping time
for the
observation times and status
which indicates the event status,
either 0 for alive and 1 for dead, or 1 for alive and 2 for dead.
stat_km( mapping = NULL, data = NULL, geom = "km", position = "identity", show.legend = NA, inherit.aes = TRUE, ... )
stat_km( mapping = NULL, data = NULL, geom = "km", position = "identity", show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to
|
A data.frame
with columns:
time
: time
in data
.
survival
: survival estimate at time
.
Logical status
is not supported.
Michael Sachs (in ggkm
), Samer Mouksassi (in ggquickeda
).
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex))) + stat_km()
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex))) + stat_km()
This stat
is for computing the location of the tick marks for the
Kaplan-Meier survival estimate for right-censored data.
It requires the aesthetic mapping time
for the
observation times and status
which indicates the event status,
either 0 for alive and 1 for dead, or 1 for alive and 2 for dead.
stat_km_ticks( mapping = NULL, data = NULL, geom = "km_ticks", position = "identity", show.legend = NA, inherit.aes = TRUE, ... )
stat_km_ticks( mapping = NULL, data = NULL, geom = "km_ticks", position = "identity", show.legend = NA, inherit.aes = TRUE, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to
|
A data.frame
with columns:
time
: time
in data
.
survival
: survival estimate at time
.
n.risk
: number of patients at risk.
n.censor
: number of patients censored.
n.event
: number of patients with event.
Logical status
is not supported.
Michael Sachs (in ggkm
), Samer Mouksassi (in ggquickeda
).
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex))) + stat_km() + stat_km_ticks()
library(ggplot2) sex <- rbinom(250, 1, .5) df <- data.frame( time = exp(rnorm(250, mean = sex)), status = rbinom(250, 1, .75), sex = sex ) ggplot(df, aes(time = time, status = status, color = factor(sex))) + stat_km() + stat_km_ticks()
See EnvStats::stat_n_text()
for details.
A plot layer including the sample size text.
See ggstats::stat_prop()
for details.
A plot layer containing the custom proportions.
See ggpp::ttheme_gtdefault()
for details.
A list
object that can be used as ttheme
in the
construction of tables with functions from package 'gridExtra'.
See ggpp::ttheme_set()
for details.
A named list with the previous value of the option.
When testing this function, we found that in contrast to the original documentation, the theme is not fixed when the plot object is constructed. Instead, the option setting affects the rendering of ready built plot objects.