formatVarForPlotLabel: Format parameter variable to be displayed in the labels of a...

Description Usage Arguments Value Author(s)

View source: R/plots-utility.R

Description

The following workflow is used:

  1. format the variable as a factor

  2. wrap it across multiple lines if needed

  3. sort (its levels) according to a grouping variable

Usage

1
2
3
4
5
6
7
formatVarForPlotLabel(
  data,
  paramVar = NULL,
  paramGroupVar = NULL,
  revert = FALSE,
  width = 20
)

Arguments

data

data.frame with data

paramVar

string, variable of data with parameter

paramGroupVar

(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.

revert

logical, if TRUE revert the order of the levels of the variable

width

max number of characters in the codeparamVar parameter.

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")


clinUtils documentation built on June 8, 2021, 9:06 a.m.