Learn R Programming

datana (version 1.1.1)

histbxp: Function for building a figure having both an histogram and a boxplot for a single random variable

Description

The function creates a figure having both an histogram and a boxplot for a random variable, as a way to help understanding its distribution.

Usage

histbxp(
  y = y,
  freqlab = "Frequency",
  varlab = "Variable",
  eng = TRUE,
  refval = NA,
  print.refval = FALSE,
  col.hist = "gray",
  col.bxp = "gray",
  portrait = TRUE,
  oma = c(3, 0.5, 2, 0),
  mar = c(1, 4, 0.2, 1),
  cex.varlab = 1.2,
  refval.symbol = expression(bar(y)),
  col.refval = "blue",
  varlim = NA,
  freqlim = NA
)

Value

The function returns the above described graph.

Arguments

y

A numeric vector representing the random variable.

freqlab

(optional) A string specifying the frequency label. The default is set to "Frequency".

varlab

(optional) A string specifying the random variable label. The default is set to "Variable".

eng

logical; if "TRUE" (by default), the language of some default text will be in English; if "FALSE" will be in Spanish. The default is to "TRUE".

refval

A numeric value to be used for printing as reference for the random variable. By default is set to the mean of the variable y.

print.refval

A logical statement to define whether a reference value should be printed, if set to TRUE, the mean of the y vector will be plotted. The default is FALSE.

col.hist

A string specifying the histogram color. The default is "gray".

col.bxp

A string specifying the boxplot color. The default is "gray".

portrait

A logical statement, if set to TRUE, the boxplot will be located under the histogram (2 rows, 1 column). If is set to FALSE, the boxplot will be located next to the histogram (1 row, 2 columns). The default is TRUE.

oma

As in the plot environment. The default is c(3, .5, 2, 0).

mar

As in the plot environment. The default is c(1, 4.0, 0.2, 1).

cex.varlab

A numeric value for the cex option of plotting to the assigned varlab element. The default value is set to 1.2 .

refval.symbol

A string of type expression with name of the refval being printed, if print.refval is set to TRUE. The default is expression(bar(y)).

col.refval

A string specifying the refval.symbol color, if print.refval is set to TRUE. The default is "blue"

varlim

(optional) A numeric vector having the minimum and maximum, respectively for the random variable.

freqlim

(optional) A numeric vector having the minimum and maximum, respectively for the frequency axis.

Author

Christian Salas-Eljatib

Details

The variable must be numeric.

References

  • Salas-Eljatib C. 2021. Análisis de datos con el programa estadístico R: una introducción aplicada. Ediciones Universidad Mayor. Santiago, Chile. 170 p. https://eljatib.com

  • Salas C, Stage AR, and Robinson AP. 2008. Modeling effects of overstory density and competing vegetation on tree height growth. Forest Science 54(1):107-122. tools:::Rd_expr_doi("10.1093/forestscience/54.1.107")

Examples

Run this code
df <- datana::fishgrowth
histbxp(y=df$length)

### distribution of 'length'
## with mean refval
histbxp(y=df$length, print.refval = TRUE)

## with given refval
histbxp(y=df$length, print.refval = TRUE, refval = 250)

## changing labels
histbxp(y=df$length, print.refval = TRUE, refval = 250,
        freqlab = "FREQ", varlab = "LENGTH")

## changing colors
histbxp(y=df$length, print.refval = TRUE, refval = 250,
        freqlab = "FREQ", varlab = "LENGTH",
        col.hist = "blue",
        col.bxp = "green",
        col.refval = "red")


### distribution of 'scale'
## with mean refval
histbxp(y=df$scale, print.refval = TRUE)

## landscape mode
histbxp(y=df$scale, print.refval = TRUE, portrait = FALSE)

## with limits
histbxp(y=df$scale, print.refval = TRUE, portrait = FALSE,
        freqlim = c(0,100),
        varlim = c(0, max(df$scale)))

Run the code above in your browser using DataLab