geom_dist: Geometry for plotting distributions with ggplot2.

View source: R/geom_dist.R

geom_distR Documentation

Geometry for plotting distributions with ggplot2.

Description

This function works like any other geom_ type ggplot2 function. However, this technically is not a custom geometry but instead returns a list of geometries to plot the various components of the distribution(s). In most instances this can be used within any ggplot2 expression. The primary difference is this function does not use any of the ggplot2 data or aes components.

Usage

geom_dist(
  x,
  quantile = 0.95,
  cv,
  color,
  alpha = 0.2,
  point_size = 0,
  center_fun = mean,
  legend_title = "Distribution",
  ...
)

Arguments

x

either a numeric vector, list of numeric vectors, or a data frame. For lists and data frames multiple density plots will be plotted.

quantile

the proportion to shade around the mean. Set to 0 to not shade any of the distribution.

cv

critical values which define the bounds of the shaded area. If specified the quantile parameter is ignored.

color

the color of the distribution. If plotting more than one distribution (i.e. x is a list or data.frame) then the length of color should be equal to the length or number of columns in x.

alpha

the transparency level used for shading the quantiles.

point_size

the size of the point at the center. The default is 0 which will exclude the point.

center_fun

the function used to determine the center of the distribution, typically mean or median.

legend_title

if x is a list of vectors a legend will be added. This is the title of the legend or set to NULL to exclude the legend.

...

other parameters passed to density().

Value

a list of ggplot2 geometries.

Examples

# Plot a single distribution
x <- rnorm(n = 1000)
ggplot2::ggplot() +
    geom_dist(x) +
    ggplot2::geom_point(data = data.frame(x = mean(x), y = 0),
                        ggplot2::aes(x = x, y = y), pch = 22)

# Plot two distributions
x1 <- rnorm(1000, mean = 0, sd = 4)
x2 <- rnbinom(1000, 10, .4)
ggplot2::ggplot() + geom_dist(list(normal = x1, binomial = x2), adjust = 2)

# Plot more than one distribution using a data frame
data(mtcars)
ggplot2::ggplot() + geom_dist(mtcars[,c('mpg', 'wt')], quantile = 0.5)

jbryer/VisualStats documentation built on Feb. 27, 2025, 6:19 p.m.