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
formatVarForPlotLabel(
data,
paramVar = NULL,
paramGroupVar = NULL,
revert = FALSE,
width = 20
)
Vector with re-formatted paramVar
, NULL if empty
data.frame with data
string, variable of data
with parameter
(optional) character vector with variable(s) of data
with grouping.
If specified, the parameters will be grouped by this(these) variable(s) in the y-axis.
logical, if TRUE revert the order of the levels of the variable
max number of characters in the codeparamVar parameter.
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")