View source: R/pretty_boxplot.R
| pretty_boxplot | R Documentation |
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.
pretty_boxplot(
x,
y,
xadj = 0.5,
ylim = NULL,
pretty_axis_args = list(side = 1:2),
xlab,
ylab,
mtext_args = list(),
...
)
x |
A factor vector. |
y |
A numeric vector. |
xadj, ylim |
Axis limit control short-cuts. |
pretty_axis_args |
A named list of arguments passed to |
xlab |
The x axis label. This can be added via |
ylab |
The y axis label. This can be added via |
mtext_args |
A named list of arguments passed to |
... |
Additional arguments passed to |
Note that, unlike boxplot, formula notation is not implemented.
The function returns a pretty boxplot.
Edward Lavender
#### 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.