Skip to content

Generate boxplots of a response variable by one or two factor variables.

Usage

boxplotGrouped(
  .data,
  y,
  group.var,
  notch = FALSE,
  y.lab = NULL,
  x.lab = NULL,
  beeswarm = FALSE,
  main = NULL,
  pt.shape = 21,
  pt.size = 2.5
)

Arguments

.data

Either a data.frame/tbl_df object where each column is a numeric vector containing values for each box, or a named list object which can be converted to one.

y

Character. The response variable for the y axis.

group.var

Character. String of length 1 or 2 representing the names of the grouping variable(s). Variables must be either factor or character class vectors.

notch

Logical. Should notches be drawn in the boxplots? If FALSE (default), a standard box plot will be drawn. If TRUE, notches will be added to the median line of each box plot. See notch argument of geom_boxplot().

y.lab

Character. Optional string for the y-axis. Otherwise one is automatically generated (default).

x.lab

Character. Optional string for the x-axis. Otherwise one is automatically generated (default).

beeswarm

Logical. Add points as overlay on top of boxplots?

main

Character. Main title for the plot. See ggtitle() for ggplot2 style graphics.

pt.shape

Numeric or Character. Recognized pch shapes for the points. Recall that only pch = 21 - 25 are "fill-able". Other point characters will not take on the color from pt.color. See geom_point().

pt.size

Numeric. A size for the points. See geom_point().

Value

A series of boxplots grouped by group.var(s).

See also

geom_boxplot(), geom_jitter()

Other boxplots: boxplotBeeswarm(), boxplotSubarray()

Author

Stu Field

Examples

size <- c("small", "medium", "large")
time <- c("baseline", "6 months", "12 months", "24 months")
df <- data.frame(
  Sample      = sample(size, 40, replace = TRUE),
  TimePoint   = rep(time, each = 10),
  seq.1234.56 = stats::rnorm(40, mean = 25, sd = 3.5)
)

# factor levels determine group order
df$Sample    <- factor(df$Sample, levels = size)
df$TimePoint <- factor(df$TimePoint, levels = time)

# single factor
boxplotGrouped(df, y = "seq.1234.56", group.var = "Sample")


# double factor
boxplotGrouped(df, y = "seq.1234.56", group.var = c("Sample", "TimePoint"))


# with "beeswarm" points
boxplotGrouped(df, y = "seq.1234.56", group.var = "TimePoint", beeswarm = TRUE)

boxplotGrouped(df, y = "seq.1234.56", group.var = c("Sample", "TimePoint"),
               beeswarm = TRUE)