pretty_boxplot: Pretty boxplots

View source: R/pretty_boxplot.R

pretty_boxplotR Documentation

Pretty boxplots

Description

This function makes prettier boxplots. Boxplots are created using boxplot but the axes are controlled via pretty_axis and an adj argument. The addition of axis labels is also more flexible because, while these can be implemented via xlab, ylab and main as usual, axis labels can also be controlled using mtext via mtext_args.

Usage

pretty_boxplot(
  x,
  y,
  xadj = 0.5,
  ylim = NULL,
  pretty_axis_args = list(side = 1:2),
  xlab,
  ylab,
  mtext_args = list(),
  ...
)

Arguments

x

A factor vector.

y

A numeric vector.

xadj, ylim

Axis limit control short-cuts. xadj controls the x axis limits via the 'control_factor_lim' argument in pretty_axis. These are taken as c(1 - xadj, number of factor levels + xadj). (xlim should not be supplied.) ylim is a vector of two limits for the y axis, implemented via pretty_axis. Both arguments are short-cuts to specifying axis limits via pretty_axis_args (see below).

pretty_axis_args

A named list of arguments passed to pretty_axis to control axes.

xlab

The x axis label. This can be added via mtext_args for more control.

ylab

The y axis label. This can be added via mtext_args for more control.

mtext_args

A named list of arguments passed to mtext to add axis labels. A nested list is used to control each axis separately.

...

Additional arguments passed to boxplot These should not include xlim, frame.plot or axes which are suppressed so that pretty_axis_args can control axes.

Details

Note that, unlike boxplot, formula notation is not implemented.

Value

The function returns a pretty boxplot.

Author(s)

Edward Lavender

Examples

#### Generate example data, e.g. sex, size and maturation status of an animal
set.seed(1)
dx <- sample(c("M", "F"), size = 100, replace = TRUE, prob = c(0.2, 0.8))
dy <- stats::runif(length(dx), 0, 50)
dx2 <- ifelse(dx < 10, "immature", "mature")
d <- data.frame(x = dx, x2 = dx2, y = dy)

#### Example (1): Comparing graphics::boxplot() and prettyGraphics::pretty_boxplot()
pp <- par(mfrow = c(1, 2))
graphics::boxplot(y ~ x, data = d)
pretty_boxplot(d$x, d$y)
par(pp)

##### Example (2): Unlike graphics::boxplot() formula notation is not supported
## Not run: 
  pretty_boxplot( d$y ~ d$x)

## End(Not run)

#### Example (3): Other arguments can be passed via ... to boxplot
pp <- graphics::par(mfrow = c(2, 2))
graphics::boxplot(d$y ~ d$x, width = c(5, 1))
pretty_boxplot(d$x, d$y, data = d, width = c(5, 1))
pretty_boxplot(d$x, d$y, data = d, varwidth = TRUE)
# However,xlim us is set via pretty_axis_args and should not be supplied.
## Not run: 
  pretty_boxplot(d$x, d$y, xlim = c(1, 2))

## End(Not run)

#### Example (4): Axis labels can be added via xlab, ylab and main
# ... or via mtext_args for more control
pretty_boxplot(d$x, d$y, xlab = "Sex", ylab = "Response")
pretty_boxplot(d$x, d$y,
               xlab = "", ylab = "",
               mtext_args = list(list(side = 1, text = "Sex", font = 2, line = 2),
                                 list(side = 2, text = "Response", font = 2, line = 2)))

#### Example (5): Axes are controlled via pretty_axis_args
pp <- par(mfrow = c(1, 2))
pretty_boxplot(d$x, d$y, pretty_axis_args = list(side = 1:2,
                                                 pretty = list(n = 10),
                                                 axis = list(las = TRUE)))
# Add a box around the plot:
pretty_boxplot(d$x, d$y, pretty_axis_args = list(side = 1:4,
                                                 pretty = list(n = 10),
                                                 axis = list(list(),
                                                             list(las = TRUE),
                                                             list(labels = FALSE, lwd.ticks = 0),
                                                             list(labels = FALSE, lwd.ticks = 0)))
)
par(pp)

#### Example (6): Examples with more groups
pp <- par(mfrow = c(2, 2))
invisible(
  lapply(c(5, 9, 12, 15), function(n){
    set.seed(1)
    dx <- factor(sample(1:n, size = 100, replace = TRUE))
    dy <- stats::runif(length(dx), 0, 10)
    d <- data.frame(x = dx, y = dy)
    pretty_boxplot(d$x, d$y, col = "lightgrey")
  })
)
par(pp)

#### Example (7): Example with multiple categories
# pretty_boxplot() does not support formula notation, so multiple categories
# ... have to be joined together prior to plotting
pretty_boxplot(factor(paste0(d$x, ",", d$x2)), d$y)

#### Example (8): A single factor level
n <- 1000
pretty_boxplot(rep("A", n), stats::rnorm(n, 0, 50))


edwardlavender/prettyGraphics documentation built on Jan. 19, 2025, 2:47 p.m.