dodge2: Point dodge

View source: R/plot_extra.R

dodge2R Documentation

Point dodge

Description

Dodge and center overlapping points by group. Spreads scattered points similar to jitter but symmetrically. Although the default method can be used, it is recommended to use the formula method for ease of use and to set useful defaults for jit and dist.

Usage

dodge2(x, ...)

## Default S3 method:
dodge2(x, y, jit = NULL, dist = NULL, ...)

## S3 method for class 'formula'
dodge2(formula, data = NULL, ...)

Arguments

x

grouping variables or, equivalently, positions along x-axis

...

additional arguments passed to or from other methods

y

a numeric vector of data, y-values

jit, dist

jittering parameters; jit describes the spread of close points, and dist defines a range to consider points "close"; both may be specified for each group and recycled as needed

formula

a formula, such as y ~ group, where y is a numeric vector of data values to be split into groups according to the grouping variable, group

data

optional matrix or data frame containing the variables in formula; by default, the variables are taken from environment(formula)

See Also

jitter; tplot; dodge; beeswarm::beeswarm

Examples

## these are equivalent ways to call dodge2:
dodge2(mpg ~ gear + vs, mtcars)
with(mtcars, dodge2(list(gear, vs), mpg))
dodge2(mtcars[, c('gear', 'vs')], mtcars$mpg)


## compare to overlapping points and jittering
sp <- split(mtcars$mpg, do.call(interaction, mtcars[, c('gear','vs')]))
plot.new()
op <- par(cex = 2)
plot.window(c(0.5, 6.5), c(10, 35))
for (ii in seq_along(sp))
  points(rep(ii, length(sp[[ii]])), sp[[ii]])
for (ii in seq_along(sp))
  points(jitter(rep(ii, length(sp[[ii]]))), sp[[ii]], col = 4, pch = 1)
points(dodge2(mpg ~ gear + vs, mtcars), col = 2, pch = 4)
legend('topleft', pch = c(1, 1, 4), col = c(1, 4, 2), cex = 0.8,
       legend = c('overlapping', 'random jitter', 'dodging'))
par(op)


## practical use
boxplot(disp ~ vs + gear, data = mtcars)
points(dodge2(disp ~ vs + gear, data = mtcars))


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.