Add quasi-random jitter on the x-axis for violin plots when the x-axis variable is categorical. Add random jitter within a rectangular area for square plots when both x- and y-axis variables are categorical.
1 2 3
A factor corresponding to a categorical variable.
A numeric vector of the same length as
A named list of factors of the same length as
Further arguments to be passed to
jitterViolinPoints function calls
offsetX to obtain quasi-random jittered x-axis values.
This reflects the area occupied by a violin plot, though some tuning of arguments in
... may be required to get an exact match.
jitterSquarePoints function will uniformly jitter points on both the x- and y-axes.
The jitter area is a square with area proportional to the frequency of the paired levels in
If either factor only has one level, the jitter area becomes a rectangle that can be interpreted as a bar plot.
grouping is specified, the values corresponding to each point defines a single combination of levels.
Both functions will then perform jittering separately within each unique combination of levels.
This is useful for obtaining appropriate jittering when points are split by group, e.g., during faceting.
jitterSquarePoints the statistics in the returned
summary data.frame will be stratified by unique combinations of levels.
To avoid clashes with existing fields, the names in
grouping should not be
jitterViolinPoints, a numeric vector is returned containing the jittered x-axis coordinates for all points.
jitterSquarePoints, a list is returned with numeric vectors
Y, containing jittered coordinates on the x- and y-axes respectively for all points;
summary, a data.frame of frequencies and side lengths for each unique pairing of X/Y levels.
1 2 3 4 5 6 7 8 9 10
X <- factor(sample(LETTERS[1:4], 100, replace=TRUE)) Y <- rnorm(100) (out1 <- jitterViolinPoints(X=X, Y=Y)) Y2 <- factor(sample(letters[1:3], 100, replace=TRUE)) (out2 <- jitterSquarePoints(X=X, Y=Y2)) grouped <- sample(5, 100, replace=TRUE) (out3 <- jitterViolinPoints(X=X, Y=Y, grouping=list(FacetRow=grouped))) (out4 <- jitterSquarePoints(X=X, Y=Y2, grouping=list(FacetRow=grouped)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.