View source: R/panel_beeswarm.R
panel.beeswarm | R Documentation |
Panel function for beeswarm plots. This panel function works essentially like a stripplot, but instead of randomly scattering a variable produces a regular, grid-like pattern of points. Currently only the X variable is transformed. Continuously distributed data points can be optionally discretized, both for X and for Y variable.
panel.beeswarm(
x,
y,
groups = NULL,
subscripts = NULL,
bin_x = FALSE,
bin_y = FALSE,
breaks_x = 10,
breaks_y = 10,
spread = 0.1,
return_coords = FALSE,
...
)
x, y |
(numeric, factor) X and Y variables passed to |
groups |
Passed to |
subscripts |
Passed to |
bin_x |
(Logical) If X variable is to be binned or not (default FALSE). |
bin_y |
(Logical) If Y variable is to be binned or not (default FALSE). |
breaks_x |
(numeric) Number of breaks for x variable if |
breaks_y |
(numeric) Number of breaks for y variable if |
spread |
(numeric) Scalar indicating how much values should be spread. |
return_coords |
(logical) if TRUE returns a list with the calculated x- and y-coordinates for each data point |
... |
Further arguments passed to |
modified after a function from Walmes Zeviani, walmes@ufpr.br,
panel.beeswarm
from package wzRfun
. Idea based on the package
beeswarm
.
library(lattice)
# simple example
df <- data.frame(
Y = sample(1:10, 60, replace = TRUE),
X = factor(rep(1:3, each = 20))
)
xyplot(Y ~ X, df, groups = X, panel = panel.beeswarm)
# but with continuous Y variable, it doesn't work as expected
df$Y <- rnorm(60)
xyplot(Y ~ X, df, groups = X, panel = panel.beeswarm)
# for this purpose we can bin the Y variable into groups
xyplot(Y ~ X, df, groups = X,
panel = function(x, y, ...) {
panel.beeswarm(x, y, bin_y = TRUE, breaks_y = 10, ...)
})
# the breaks for Y bins are computed for each panel independently.
# we can also supply fixed bins via the 'breaks_y' argument
# to obtain the same binning for each panel
xyplot(Y ~ factor(rep(1, length(Y))) | X, df, groups = X,
panel = function(x, y, ...) {
panel.beeswarm(x, y, bin_y = TRUE,
breaks_y = seq(-4, 4, length.out = 20), ...)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.