formatVarForPlotLabel {clinUtils} | R Documentation |
Format parameter variable to be displayed in the labels of a plot
Description
The following workflow is used:
format the variable as a factor
wrap it across multiple lines if needed
sort (its levels) according to a grouping variable
Usage
formatVarForPlotLabel(
data,
paramVar = NULL,
paramGroupVar = NULL,
revert = FALSE,
width = 20
)
Arguments
data |
data.frame with data |
paramVar |
string, variable of |
paramGroupVar |
(optional) character vector with variable(s) of
|
revert |
logical, if TRUE revert the order of the levels of the variable |
width |
max number of characters in the |
Value
Vector with re-formatted paramVar
, NULL if empty
Author(s)
Laure Cougnaud library(ggplot2) data(dataADaMCDISCP01) dataAE <- dataADaMCDISCP01$ADAE
# by default, groups are sorted alphabetically in ggplot2 (from bottom to top for an histogram) ggplot(data = dataAE, aes(y = AEDECOD, fill = AEBODSYS)) + geom_histogram(stat="count")
# by default: labels are set to a new line if more than 20 characters: dataAE$AEDECOD <- formatVarForPlotLabel(data = dataAE, paramVar = "AEDECOD") levels(dataAE$AEDECOD) ggplot(data = dataAE, aes(y = AEDECOD, fill = AEBODSYS)) + geom_histogram(stat="count")
# revert order of the variable dataAE$AEDECOD <- formatVarForPlotLabel(data = dataAE, paramVar = "AEDECOD", revert = TRUE) levels(dataAE$AEDECOD) ggplot(data = dataAE, aes(y = AEDECOD, fill = AEBODSYS)) + geom_histogram(stat="count")
# group based on body system dataAE$AEDECOD <- formatVarForPlotLabel(data = dataAE, paramVar = "AEDECOD", paramGroupVar = "AEBODSYS") ggplot(data = dataAE, aes(y = AEDECOD, fill = AEBODSYS)) + geom_histogram(stat="count")