gboxplot: Grouped Boxplot

View source: R/gboxplot.R

gboxplotR Documentation

Grouped Boxplot

Description

gboxplot produces grouped box plot.

Usage

gboxplot(
  f,
  data,
  col = c("green", "red"),
  alpha = 1,
  at1 = NULL,
  at2 = NULL,
  at1.labels = NULL,
  at2.labels = NULL,
  at1.centered = FALSE,
  mgp1 = c(3, 0.2, 0),
  mgp2 = c(3, 0.25, 0),
  lwd.axis = 0.1,
  legend = FALSE,
  legend.position = "topright",
  legend.label1 = "Sub-group 1",
  legend.label2 = "Sub-group 2",
  cex.legend = 1,
  x.intersp = 1,
  y.intersp = 1,
  inset = c(0, 0),
  ...
)

Arguments

f

a formula such as y ~ g1:g2, where y is a numeric vector of data values to be split into groups. Note that ~ g1 + g2 is equivalent to g1:g2.

data

a data.frame or data.table from which the variables in formula should be taken.

col

box colors.

alpha

color transparency (a number between 0 and 1).

at1

if null (default), even-distributed locations would be generated for the boxes. Otherwise, users should provide a "at list" generated from gbat to adjust the positions where the boxes should be drawn.

at2

the points at which y-tick-marks are to be drawn.

at1.labels

x-tick-labels. Valid only when at1.centered = TRUE. Otherwise please use names to change x-tick-labels.

at2.labels

y-tick-labels.

at1.centered

logical. If TRUE x-axis ticks would be centered.

mgp1

mgp for x axis, same as the mgp in par.

mgp2

mgp for y axis, same as the mgp in par.

lwd.axis

axis line width.

legend

logical. Whether a legend should be added.

legend.position

position of legend.

legend.label1

legend label for sub-group 1.

legend.label2

legend label for sub-group 2.

cex.legend

size of legend.

x.intersp

horizontal spacing between legend item and text.

y.intersp

vertical spacing between legend items.

inset

inset distance from the margins as a fraction of the plot region when legend is placed by keyword.

...

other graphical parameters to be passed.

Value

List with components same as boxplot.

See Also

gbat.

Examples

opar = par(no.readonly = TRUE)
par(cex.axis = 0.9, mai = c(0.5, 0.4, 0.3, 0.2), mfrow = c(2, 2), oma = c(1, 5, 2, 5))

# base r boxplot ----
boxplot(
  len ~ supp:dose, data = ToothGrowth,
  boxwex = 0.5, col = c('red', 'green'),
  xlab = '', ylab = 'tooth length',
  ylim = c(0, 35), yaxs = 'i',
  sep = ':',
  main = 'base r boxplot'
)

# gboxplot with defaul at and legend ----
gboxplot(
  len ~ supp:dose, data = ToothGrowth,
  boxwex = 0.5, col = c('red', 'green'), alpha = 0.5,
  xlab = '', ylab = 'tooth length',
  ylim = c(0, 35), yaxs = 'i',
  sep = ':',
  main = 'gboxplot with defaul at and legend',
  legend = TRUE, legend.position = 'bottomright', legend.label1 = 'OJ', legend.label2 = 'VC', cex.legend = 0.8,
  x.intersp = 0.8, y.intersp = 0.8
)

# gboxplot with adjusted at ----
n.group = 3
n.subgroup = 2
# adjusted box positions
myat = gbat(n.group, n.subgroup, 1.2, 0.35)
gboxplot(
len ~ supp:dose, data = ToothGrowth,
boxwex = 0.2, col = c('red', 'green'), alpha = 0.5,
xlab = '', ylab = 'tooth length',
at1 = myat,
ylim = c(0, 35), yaxs = 'i',
sep = ':',
main = 'gboxplot with adjusted at',
legend = TRUE, legend.position = 'bottomright', legend.label1 = 'OJ', legend.label2 = 'VC', cex.legend = 0.8,
x.intersp = 0.8, y.intersp = 0.8
)

# gboxplot with adjusted at and centered ticks ----
gboxplot(
  len ~ supp:dose, data = ToothGrowth,
  boxwex = 0.2, col = c('red', 'green'), alpha = 0.5,
  xlab = '', ylab = 'tooth length',
  at1 = myat, at1.labels = c(0.5, 1, 2), at1.centered = TRUE,
  ylim = c(0, 35), yaxs = 'i',
  sep = ':',
  main = 'gboxplot with adjusted at and centered ticks',
  legend = TRUE, legend.position = 'bottomright', legend.label1 = 'OJ', legend.label2 = 'VC', cex.legend = 0.8,
  x.intersp = 0.8, y.intersp = 0.8
)
par(opar)


jiachengzhao/smot documentation built on Sept. 20, 2022, 6:36 a.m.