Title: | Compose Half-Half Plots Using Your Favourite Geoms |
---|---|
Description: | A 'ggplot2' extension for easy plotting of half-half geom combinations. Think half boxplot and half jitterplot, or half violinplot and half dotplot. |
Authors: | Frederik Tiedemann [aut, cre] |
Maintainer: | Frederik Tiedemann <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.4 |
Built: | 2025-02-07 03:11:49 UTC |
Source: | https://github.com/erocoar/gghalves |
A half boxplot
geom_half_boxplot( mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2", ..., side = "l", center = FALSE, nudge = 0, outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, errorbar.draw = TRUE, errorbar.length = 0.5, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_half_boxplot( mapping = NULL, data = NULL, stat = "boxplot", position = "dodge2", ..., side = "l", center = FALSE, nudge = 0, outlier.colour = NULL, outlier.color = NULL, outlier.fill = NULL, outlier.shape = 19, outlier.size = 1.5, outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5, varwidth = FALSE, errorbar.draw = TRUE, errorbar.length = 0.5, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
Use to override the default connection between 'geom_boxplot()' and 'stat_boxplot()'. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
side |
The side of the half-geom, "l" for left and "r" for right, defaults to "l". |
center |
Boolean whether to center the half-boxplot instead of aligning it to its respective side. |
nudge |
Add space between the boxplot and the middle of the space allotted to a given factor on the x-axis. |
outlier.colour , outlier.color , outlier.fill , outlier.shape , outlier.size , outlier.stroke , outlier.alpha
|
Default aesthetics for outliers. Set to In the unlikely event you specify both US and UK spellings of colour, the US spelling will take precedence. Sometimes it can be useful to hide the outliers, for example when overlaying
the raw data points on top of the boxplot. Hiding the outliers can be achieved
by setting |
notch |
If |
notchwidth |
For a notched box plot, width of the notch relative to
the body (defaults to |
varwidth |
If |
errorbar.draw |
Draw horizontal whiskers at the top and bottom (the IQR). Defaults to 'TRUE'. |
errorbar.length |
Length of the horizontal whiskers (errorbar). Defaults to half the width of the half-boxplot |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_boxplot() ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_boxplot(side = "r") ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_boxplot(center = TRUE)
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_boxplot() ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_boxplot(side = "r") ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_boxplot(center = TRUE)
In a dot plot, the width of a dot corresponds to the bin width (or maximum width, depending on the binning algorithm), and dots are stacked, with each dot representing one observation.
geom_half_dotplot( mapping = NULL, data = NULL, position = "dodge", ..., binwidth = NULL, binaxis = "y", method = "dotdensity", binpositions = "bygroup", stackdir = "up", stackratio = 1, dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE, width = NULL, drop = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_half_dotplot( mapping = NULL, data = NULL, position = "dodge", ..., binwidth = NULL, binaxis = "y", method = "dotdensity", binpositions = "bygroup", stackdir = "up", stackratio = 1, dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE, width = NULL, drop = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
binwidth |
When |
binaxis |
The axis to bin along, "x" (default) or "y" |
method |
"dotdensity" (default) for dot-density binning, or "histodot" for fixed bin widths (like stat_bin) |
binpositions |
When |
stackdir |
Which direction to stack the dots. "up" (default) places the half-dotplot on the right side. "down" on the left side. |
stackratio |
how close to stack the dots. Default is 1, where dots just touch. Use smaller values for closer, overlapping dots. |
dotsize |
The diameter of the dots relative to |
stackgroups |
should dots be stacked across groups? This has the effect
that |
origin |
When |
right |
When |
width |
When |
drop |
If TRUE, remove all bins with zero counts |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
There are two basic approaches: dot-density and histodot. With dot-density binning, the bin positions are determined by the data and 'binwidth', which is the maximum width of each bin. See Wilkinson (1999) for details on the dot-density binning algorithm. With histodot binning, the bins have fixed positions and fixed widths, much like a histogram.
When binning along the x axis and stacking along the y axis, the numbers on y axis are not meaningful, due to technical limitations of ggplot2. You can hide the y axis, as in one of the examples, or manually scale it to match the number of dots.
center of each bin, if binaxis is "x"
center of each bin, if binaxis is "x"
max width of each bin if method is "dotdensity"; width of each bin if method is "histodot"
number of points in bin
count, scaled to maximum of 1
density of points in bin, scaled to integrate to 1, if method is "histodot"
density, scaled to maximum of 1, if method is "histodot"
Wilkinson, L. (1999) Dot plots. The American Statistician, 53(3), 276-281.
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_dotplot(stackratio = .5, method = "histodot")
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_dotplot(stackratio = .5, method = "histodot")
Points with jitter for half geoms.
geom_half_point( mapping = NULL, data = NULL, stat = "HalfPoint", position = "dodge2", ..., side = "r", transformation = position_jitter(), range_scale = 0.75, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_half_point( mapping = NULL, data = NULL, stat = "HalfPoint", position = "dodge2", ..., side = "r", transformation = position_jitter(), range_scale = 0.75, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
side |
The side on which to draw the half violin plot. "l" for left, "r" for right, defaults to "l". |
transformation |
An evaluated 'position_*()' function yielding a 'Position' object with specified parameters to calculate the transformation of the points. Defaults to 'ggplot2::position_jitter()'. |
range_scale |
If no 'width' argument is specified in 'transformation', 'range_scale' is used to determine the width of the jitter. Defaults to '0.75', which is half of the allotted space for the jitter-points, whereas '1' would use all of the allotted space. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_point() ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_point(side = "l")
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_point() ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_point(side = "l")
Points with jitter for half geoms. Unlike 'geom_half_point', 'geom_half_point_panel' does not dodge different grouping aesthetics. This allows multiple groups in a single cloud of points (see examples).
geom_half_point_panel( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., side = "r", transformation = position_jitter(), range_scale = 0.75, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_half_point_panel( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., side = "r", transformation = position_jitter(), range_scale = 0.75, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer, as a string. |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
side |
The side on which to draw the half violin plot. "l" for left, "r" for right, defaults to "l". |
transformation |
An evaluated 'position_*()' function yielding a 'Position' object with specified parameters to calculate the transformation of the points. Defaults to 'ggplot2::position_jitter()'. |
range_scale |
If no 'width' argument is specified in 'transformation', 'range_scale' is used to determine the width of the jitter. Defaults to '0.75', which is half of the allotted space for the jitter-points, whereas '1' would use all of the allotted space. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
ggplot(iris, aes(y = Sepal.Width)) + geom_half_boxplot() + geom_half_point_panel(aes(x = 0.5, color = Species), range_scale = .5) + theme_classic()
ggplot(iris, aes(y = Sepal.Width)) + geom_half_boxplot() + geom_half_point_panel(aes(x = 0.5, color = Species), range_scale = .5) + theme_classic()
A violin plot is a compact display of a continuous distribution. It is a blend of [geom_boxplot()] and [geom_density()]: a violin plot is a mirrored density plot displayed in the same way as a boxplot.
geom_half_violin( mapping = NULL, data = NULL, stat = "half_ydensity", position = "dodge", ..., side = "l", nudge = 0, draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) stat_half_ydensity( mapping = NULL, data = NULL, geom = "half_violin", position = "dodge", ..., bw = "nrd0", adjust = 1, kernel = "gaussian", trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
geom_half_violin( mapping = NULL, data = NULL, stat = "half_ydensity", position = "dodge", ..., side = "l", nudge = 0, draw_quantiles = NULL, trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) stat_half_ydensity( mapping = NULL, data = NULL, geom = "half_violin", position = "dodge", ..., bw = "nrd0", adjust = 1, kernel = "gaussian", trim = TRUE, scale = "area", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string, or the result of a call to a position adjustment function. |
... |
Other arguments passed on to |
side |
The side on which to draw the half violin plot. "l" for left, "r" for right, defaults to "l". |
nudge |
Add space between the violinplot and the middle of the space allotted to a given factor on the x-axis. |
draw_quantiles |
If |
trim |
If |
scale |
if "area" (default), all violins have the same area (before trimming the tails). If "count", areas are scaled proportionally to the number of observations. If "width", all violins have the same maximum width. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom , stat
|
Use to override the default connection between
|
bw |
The smoothing bandwidth to be used.
If numeric, the standard deviation of the smoothing kernel.
If character, a rule to choose the bandwidth, as listed in
|
adjust |
A multiplicate bandwidth adjustment. This makes it possible
to adjust the bandwidth while still using the a bandwidth estimator.
For example, |
kernel |
Kernel. See list of available kernels in |
The half-violin plot accepts an optional 'split' aesthethic to compare data separated by a binary variable side-by-side.
Hintze, J. L., Nelson, R. D. (1998) Violin Plots: A Box Plot-Density Trace Synergism. The American Statistician 52, 181-184.
[geom_half_violin()] for examples.
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_violin() ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_violin(side = "r") ggplot() + geom_half_violin( data = ToothGrowth, aes(x = as.factor(dose), y = len, split = supp, fill = supp), position = "identity" ) + theme_minimal() ggplot(ToothGrowth, aes(x = supp, y = len, color = supp)) + geom_half_violin(side = c("l", "r")) ggplot(data = ToothGrowth, aes(x = 1, y = len)) + geom_half_point(aes(y = len), side = "l") + geom_half_violin(aes(y = len), side = "r") + coord_flip()
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_violin() ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) + geom_half_violin(side = "r") ggplot() + geom_half_violin( data = ToothGrowth, aes(x = as.factor(dose), y = len, split = supp, fill = supp), position = "identity" ) + theme_minimal() ggplot(ToothGrowth, aes(x = supp, y = len, color = supp)) + geom_half_violin(side = c("l", "r")) ggplot(data = ToothGrowth, aes(x = 1, y = len)) + geom_half_point(aes(y = len), side = "l") + geom_half_violin(aes(y = len), side = "r") + coord_flip()