| Title: | Visualizations of Distributions and Uncertainty |
| Version: | 3.3.3 |
| Date: | 2025-04-20 |
| Maintainer: | Matthew Kay <mjskay@northwestern.edu> |
| Description: | Provides primitives for visualizing distributions using 'ggplot2' that are particularly tuned for visualizing uncertainty in either a frequentist or Bayesian mode. Both analytical distributions (such as frequentist confidence distributions or Bayesian priors) and distributions represented as samples (such as bootstrap distributions or Bayesian posterior samples) are easily visualized. Visualization primitives include but are not limited to: points with multiple uncertainty intervals, eye plots (Spiegelhalter D., 1999)https://ideas.repec.org/a/bla/jorssa/v162y1999i1p45-58.html, density plots, gradient plots, dot plots (Wilkinson L., 1999) <doi:10.1080/00031305.1999.10474474>, quantile dot plots (Kay M., Kola T., Hullman J., Munson S., 2016) <doi:10.1145/2858036.2858558>, complementary cumulative distribution function barplots (Fernandes M., Walls L., Munson S., Hullman J., Kay M., 2018) <doi:10.1145/3173574.3173718>, and fit curves with multiple uncertainty ribbons. |
| Depends: | R (≥ 4.0.0) |
| Imports: | grid, ggplot2 (≥ 3.5.0), scales, rlang (≥ 0.3.0), cli,tibble, vctrs, withr, glue, gtable, distributional (≥ 0.3.2),numDeriv, quadprog, Rcpp |
| Suggests: | tidyselect, dplyr (≥ 1.0.0), fda, posterior (≥ 1.4.0),beeswarm (≥ 0.4.0), rmarkdown, knitr, testthat (≥ 3.0.0),vdiffr (≥ 1.0.0), svglite (≥ 2.1.0), fontquiver, sysfonts,showtext, mvtnorm, covr, broom (≥ 0.5.6), patchwork, tidyr (≥1.0.0), ragg (≥ 1.3.0), pkgdown |
| License: | GPL (≥ 3) |
| Language: | en-US |
| BugReports: | https://github.com/mjskay/ggdist/issues |
| URL: | https://mjskay.github.io/ggdist/,https://github.com/mjskay/ggdist/ |
| VignetteBuilder: | knitr |
| RoxygenNote: | 7.3.2 |
| LazyData: | true |
| Encoding: | UTF-8 |
| Collate: | "ggdist-package.R" "util.R" "compat.R" "rd.R" "RcppExports.R""abstract_geom.R" "abstract_stat.R""abstract_stat_slabinterval.R" "auto_partial.R""binning_methods.R" "bounder.R" "curve_interval.R""cut_cdf_qi.R" "data.R" "density.R" "distributions.R""draw_key_slabinterval.R" "geom.R" "geom_slabinterval.R""geom_dotsinterval.R" "geom_blur_dots.R" "geom_interval.R""geom_lineribbon.R" "geom_pointinterval.R" "geom_slab.R""geom_spike.R" "geom_swarm.R" "guide_rampbar.R""interval_widths.R" "lkjcorr_marginal.R" "parse_dist.R""partial_colour_ramp.R" "point_interval.R""position_dodgejust.R" "pr.R" "rd_density.R""rd_dotsinterval.R" "rd_slabinterval.R" "rd_spike.R""rd_lineribbon.R" "scale_colour_ramp.R" "scale_thickness.R""scale_side_mirrored.R" "scale_.R" "smooth.R" "stat.R""stat_slabinterval.R" "stat_dotsinterval.R" "stat_mcse_dots.R""stat_pointinterval.R" "stat_interval.R" "stat_lineribbon.R""stat_spike.R" "student_t.R" "subguide.R" "subscale.R""testthat.R" "theme_ggdist.R" "thickness.R""tidy_format_translators.R" "weighted_ecdf.R" "weighted_hist.R""weighted_quantile.R" "deprecated.R" |
| Config/testthat/edition: | 3 |
| LinkingTo: | Rcpp |
| NeedsCompilation: | yes |
| Packaged: | 2025-04-22 23:25:44 UTC; matth |
| Author: | Matthew Kay [aut, cre], Brenton M. Wiernik [ctb] |
| Repository: | CRAN |
| Date/Publication: | 2025-04-23 00:20:02 UTC |
Visualizations of Distributions and Uncertainty
Description
ggdist is an R package that aims to make it easy to integratepopular Bayesian modeling methods into a tidy data + ggplot workflow.
Details
ggdist is an R package that provides a flexible set ofggplot2 geoms and stats designedespecially for visualizing distributions and uncertainty. It is designed for bothfrequentist and Bayesian uncertainty visualization, taking the view that uncertaintyvisualization can be unified through the perspective of distribution visualization:for frequentist models, one visualizes confidence distributions or bootstrap distributions(seevignette("freq-uncertainty-vis")); for Bayesian models, one visualizes probabilitydistributions (seevignette("tidybayes", package = "tidybayes")).
Thegeom_slabinterval() /stat_slabinterval() family (seevignette("slabinterval")) makes iteasy to visualize point summaries and intervals, eye plots, half-eye plots, ridge plots,CCDF bar plots, gradient plots, histograms, and more.
Thegeom_dotsinterval() /stat_dotsinterval() family (seevignette("dotsinterval")) makesit easy to visualize dot+interval plots, Wilkinson dotplots, beeswarm plots, and quantile dotplots.
Thegeom_lineribbon() /stat_lineribbon() family (seevignette("lineribbon"))makes it easy to visualize fit lines with an arbitrary number of uncertainty bands.
Author(s)
Maintainer: Matthew Kaymjskay@northwestern.edu
Other contributors:
Brenton M. Wiernikbrenton@wiernik.org [contributor]
See Also
Useful links:
Report bugs athttps://github.com/mjskay/ggdist/issues
Base ggproto classes for ggdist
Description
Base ggproto classes for ggdist
See Also
Probability expressions in ggdist aesthetics
Description
Experimental probability-like expressions that can be used in place ofsomeafter_stat() expressions in aesthetic assignments inggdist stats.
Usage
Pr_(x)p_(x)Arguments
x | <barelanguage> Expressions. SeeProbability expressions, below. |
Details
Pr_() andp_() are anexperimental mini-language for specifying aesthetic valuesbased on probabilities and probability densities derived from distributionssupplied toggdist stats (e.g., instat_slabinterval(),stat_dotsinterval(), etc.). They generate expressions that useafter_stat()and the computed variables of the stat (such ascdf andpdf; see e.g.theComputed Variables section ofstat_slabinterval()) to computethe desired probabilities or densities.
For example, one way to map the density of a distribution onto thealphaaesthetic of a slab is to useafter_stat(pdf):
ggplot() + stat_slab(aes(xdist = distributional::dist_normal(), alpha = after_stat(pdf)))
ggdist probability expressions offer an alternative, equivalent syntax:
ggplot() + stat_slab(aes(xdist = distributional::dist_normal(), alpha = !!p_(x)))
Wherep_(x) is the probability density function. The use of!! isnecessary to splice the generated expression into theaes() call; formore information, seequasiquotation.
Probability expressions
Probability expressions consist of a call toPr_() orp_() containinga small number of valid combinations of operators and variable names.
Valid variables in probability expressions include:
x,y, orvalue: values along thexoryaxis.valueis theorientation-neutral form.xdist,ydist, ordist: distributions mapped along thexoryaxis.distis the orientation-neutral form.XandYcan also beused as synonyms forxdistandydist.interval: the smallest interval containing the currentx/yvalue.
Pr_() generates expressions for probabilities, e.g. cumulative distributionfunctions (CDFs). Valid operators insidePr_() are:
<,<=,>,>=: generates values of the cumulative distributionfunction (CDF) or complementary CDF by comparing one of {x,y,value}to one of {xdist,ydist,dist,X,Y}. For example,Pr_(xdist <= x)gives the CDF andPr_(xdist > x)gives the CCDF.%in%: currently can only be used withintervalon the right-hand side:gives the probability of {x,y,value} (left-hand side) being in thesmallest interval the stat generated that contains the value; e.g.Pr_(x %in% interval).
p_() generates expressions for probability density functions or probability massfunctions (depending on if the underlying distribution is continuous ordiscrete). It currently does not allow any operators in the expression, andmust be passed one ofx,y, orvalue.
See Also
TheComputed Variables section ofstat_slabinterval() (especiallycdf andpdf) and theafter_stat() function.
Examples
library(ggplot2)library(distributional)df = data.frame( d = c(dist_normal(2.7, 1), dist_lognormal(1, 1/3)), name = c("normal", "lognormal"))# map density onto alpha of the fillggplot(df, aes(y = name, xdist = d)) + stat_slabinterval(aes(alpha = !!p_(x)))# map CCDF onto thickness (like stat_ccdfinterval())ggplot(df, aes(y = name, xdist = d)) + stat_slabinterval(aes(thickness = !!Pr_(xdist > x)))# map containing interval onto fillggplot(df, aes(y = name, xdist = d)) + stat_slabinterval(aes(fill = !!Pr_(x %in% interval)))# the color scale in the previous example is not great, so turn the# probability into an ordered factor and adjust the fill scale.# Though, see also the `level` computed variable in `stat_slabinterval()`,# which is probably easier to use to create this style of chart.ggplot(df, aes(y = name, xdist = d)) + stat_slabinterval(aes(fill = ordered(!!Pr_(x %in% interval)))) + scale_fill_brewer(direction = -1)Thinned subset of posterior sample from a Bayesian analysis of perception of correlation.
Description
Data from Kay and Heer (2016), primarily used for testing and examples.
Details
For more details, see Kay and Heer (2016) or the Github repository describing the analysis:https://github.com/mjskay/ranking-correlation. The original experiment (but not this analysis of it)is described in Harrisonet al. (2014).
data("RankCorr") is a substantially thinned version of the original posterior sample and has omitted severalparameters in order for it to be a more manageable size.
data("RankCorr_u_tau") is used for testing and examples and is roughly the equivalent of the following:
data("RankCorr")RankCorr_u_tau = tidybayes::spread_draws(RankCorr, u_tau[i]))References
Kay, Matthew, and Jeffrey Heer. (2016)."Beyond Weber's law: A second look at ranking visualizations of correlation."IEEE transactions on visualization and computer graphics 22(1): 469-478.doi:10.1109/TVCG.2015.2467671
Harrison, Lane, Fumeng Yang, Steven Franconeri, and Remco Chang. (2014)."Ranking visualizations of correlation using Weber's law."IEEE transactions on visualization and computer graphics 20(12): 1943-1952.doi:10.1109/TVCG.2014.2346979
Break (bin) alignment methods
Description
Methods for aligning breaks (bins) in histograms, as used in thealignargument todensity_histogram().
Supportsautomatic partial function application withwaived arguments.
Usage
align_none(breaks)align_boundary(breaks, at = 0)align_center(breaks, at = 0)Arguments
breaks | <numeric> A sorted vector of breaks (bin edges). |
at | <scalarnumeric> The alignment point.
|
Details
These functions take a sorted vector of equally-spacedbreaks givingbin edges and return a numeric offset which, if subtracted frombreaks,will align them as desired:
align_none()performs no alignment (it always returns0).align_boundary()ensures that a bin edge lines up withat.align_center()ensures that a bin center lines up withat.
Foralign_boundary() (respectivelyalign_center()), if no bin edge (or center) in therange ofbreaks would line up withat, it ensures thatat is an integermultiple of the bin width away from a bin edge (or center).
Value
A scalar numeric returning an offset to be subtracted frombreaks.
See Also
Examples
library(ggplot2)set.seed(1234)x = rnorm(200, 1, 2)# If we manually specify a bin width using breaks_fixed(), the default# alignment (align_none()) will not align bin edges to any "pretty" numbers.# Here is a comparison of the three alignment methods on such a histogram:ggplot(data.frame(x), aes(x)) + stat_slab( aes(y = "align_none()\nor 'none'"), density = "histogram", breaks = breaks_fixed(width = 1), outline_bars = TRUE, # no need to specify align; align_none() is the default color = "black", ) + stat_slab( aes(y = "align_center(at = 0)\nor 'center'"), density = "histogram", breaks = breaks_fixed(width = 1), align = align_center(at = 0), # or align = "center" outline_bars = TRUE, color = "black", ) + stat_slab( aes(y = "align_boundary(at = 0)\nor 'boundary'"), density = "histogram", breaks = breaks_fixed(width = 1), align = align_boundary(at = 0), # or align = "boundary" outline_bars = TRUE, color = "black", ) + geom_point(aes(y = 0.7), alpha = 0.5) + labs( subtitle = "ggdist::stat_slab(density = 'histogram', ...)", y = "align =", x = NULL ) + geom_vline(xintercept = 0, linetype = "22", color = "red")Automatic partial function application in ggdist
Description
Severalggdist functions supportautomatic partial application: when called,if all of their required arguments have not been provided, the function returns amodified version of itself that uses the arguments passed to it so far as defaults.Technically speaking, these functions are essentially "Curried" with respect totheir required arguments, but I think "automatic partial application" getsthe idea across more clearly.
Functions supporting automatic partial application include:
The
point_interval()family, such asmedian_qi(),mean_qi(),mode_hdi(), etc.The
smooth_family, such assmooth_bounded(),smooth_unbounded(),smooth_discrete(), andsmooth_bar().The
density_family, such asdensity_bounded(),density_unbounded()anddensity_histogram().Thealign family.
Thebreaks family.
Thebandwidth family.
Theblur family.
Partial application makes it easier to supply custom parameters to thesefunctions when using them inside other functions, such as geoms and stats.For example, smoothers forgeom_dots() can be supplied in one of threeways:
as a suffix:
geom_dots(smooth = "bounded")as a function:
geom_dots(smooth = smooth_bounded)as a partially-applied function with options:
geom_dots(smooth = smooth_bounded(kernel = "cosine"))
Many other common arguments forggdist functions work similarly; e.g.density,align,breaks,bandwidth, andpoint_interval arguments.
These function families (exceptpoint_interval()) also support passingwaivers to their optional arguments: ifwaiver() is passed to anyof these arguments, their default value (or the mostrecently-partially-applied non-waiver value) is used instead.
Use theauto_partial() function to create new functions that supportautomatic partial application.
Usage
auto_partial(f, name = NULL, waivable = TRUE)Arguments
f | <function> Function to automatically partially-apply. |
name | <string> Name of the function, to be usedwhen printing. |
waivable | <scalarlogical> If |
Value
A modified version off that will automatically be partiallyapplied if all of its required arguments are not given.
Examples
set.seed(1234)x = rnorm(100)# the first required argument, `x`, of the density_ family is the vector# to calculate a kernel density estimate from. If it is not provided, the# function is partially applied and returned as-isdensity_unbounded()# we could create a new function that uses half the default bandwidthdensity_half_bw = density_unbounded(adjust = 0.5)density_half_bw# we can overwrite partially-applied argumentsdensity_quarter_bw_trimmed = density_half_bw(adjust = 0.25, trim = TRUE)density_quarter_bw_trimmed# when we eventually call the function and provide the required argument# `x`, it is applied using the arguments we have "saved up" so fardensity_quarter_bw_trimmed(x)# create a custom automatically partially applied functionf = auto_partial(function(x, y, z = 3) (x + y) * z)f()f(1)g = f(y = 2)(z = 4)gg(1)# pass waiver() to optional arguments to use existing valuesf(z = waiver())(1, 2) # uses default z = 3f(z = 4)(z = waiver())(1, 2) # uses z = 4Bandwidth estimators
Description
Bandwidth estimators for densities, used in thebandwidth argumentto density functions (e.g.density_bounded(),density_unbounded()).
Supportsautomatic partial function application withwaived arguments.
Usage
bandwidth_nrd0(x, ...)bandwidth_nrd(x, ...)bandwidth_ucv(x, ...)bandwidth_bcv(x, ...)bandwidth_SJ(x, ...)bandwidth_dpi(x, ...)Arguments
x | <numeric> Vector containing a sample. |
... | Arguments passed on to
|
Details
These are loose wrappers around the correspondingbw.-prefixed functionsinstats. See, for example,bw.SJ().
bandwidth_dpi(), which is the default bandwidth estimator inggdist,is the Sheather-Jones direct plug-in estimator, i.e.bw.SJ(..., method = "dpi").
With the exception ofbandwidth_nrd0(), these estimators may fail in somecases, often when a sample contains many duplicates. If they do they willautomatically fall back tobandwidth_nrd0() with a warning. However, thesefailures are typically symptomatic of situations where you should not want touse a kernel density estimator in the first place (e.g. data with duplicatesand/or discrete data). In these cases consider using a dotplot (geom_dots())or histogram (density_histogram()) instead.
Value
A single number giving the bandwidth
See Also
density_bounded(),density_unbounded().
Bin data values using a dotplot algorithm
Description
Bins the provided data values using one of several dotplot algorithms.
Usage
bin_dots( x, y, binwidth, heightratio = 1, stackratio = 1, layout = c("bin", "weave", "hex", "swarm", "bar"), side = c("topright", "top", "right", "bottomleft", "bottom", "left", "topleft", "bottomright", "both"), orientation = c("horizontal", "vertical", "y", "x"), overlaps = "nudge")Arguments
x | <numeric>x values. |
y | <numeric>y values (same length as |
binwidth | <scalarnumeric> Bin width. |
heightratio | <scalarnumeric> Ratio of bin width to dot height |
stackratio | <scalarnumeric> Ratio of dot height to vertical distancebetween dot centers |
layout | <string> The layout method used for the dots. One of:
|
side | Which side to place the slab on. |
orientation | <string> Whether the dots are laid out horizontallyor vertically. Follows the naming scheme of
For compatibility with the base ggplot naming scheme for |
overlaps | <string> How to handle overlapping dots or bins in the
|
Value
Adata.frame with three columns:
x: the x position of each doty: the y position of each dotbin: a unique number associated with each bin(supplied but not used whenlayout = "swarm")
See Also
find_dotplot_binwidth() for an algorithm that finds good bin widthsto use with this function;geom_dotsinterval() for geometries that usethese algorithms to create dotplots.
Examples
library(dplyr)library(ggplot2)x = qnorm(ppoints(20))bin_df = bin_dots(x = x, y = 0, binwidth = 0.5, heightratio = 1)bin_df# we can manually plot the binning above, though this is only recommended# if you are using find_dotplot_binwidth() and bin_dots() to build your own# grob. For practical use it is much easier to use geom_dots(), which will# automatically select good bin widths for you (and which uses# find_dotplot_binwidth() and bin_dots() internally)bin_df %>% ggplot(aes(x = x, y = y)) + geom_point(size = 4) + coord_fixed()Blur functions for blurry dot plots
Description
Methods for constructing blurs, as used in theblur argument togeom_blur_dots() orstat_mcse_dots().
Supportsautomatic partial function application withwaived arguments.
Usage
blur_gaussian(x, r, sd)blur_interval(x, r, sd, .width = 0.95)Arguments
x | <numeric> Vector of positive distances from the center of the dot(assumed to be 0) to evaluate blur function at. |
r | <scalarnumeric> Radius of the dot that is being blurred. |
sd | <scalarnumeric> Standard deviation of the dot that is being blurred. |
.width | <scalarnumeric> For |
Details
These functions are passedx,r, andsd whengeom_blur_dots()draws in order to create a radial gradient representing each dot in thedotplot. They return values between0 and1 giving the opacity of thedot at each value ofx.
blur_gaussian() creates a dot with radiusr that has a Gaussian blur withstandard deviationsd applied to it. It does this by calculating\alpha(x; r, \sigma), the opacity at distancex from the centerof a dot with radiusr that has had a Gaussian blur with standarddeviation\sigma =sd applied to it:
\alpha(x; r, \sigma) = \Phi \left(\frac{x + r}{\sigma} \right) - \Phi \left(\frac{x - r}{\sigma} \right)
blur_interval() creates an interval-type representation around thedot at 50% opacity, where the interval is a Gaussian quantile interval withmass equal to.width and standard deviationsd.
Value
A vector with the same length asx giving the opacity of the radialgradient representing the dot at eachx value.
See Also
geom_blur_dots() andstat_mcse_dots() for geometries making use ofblur functions.
Examples
# see examples in geom_blur_dots()Estimate bounds of a distribution using the CDF of its order statistics
Description
Estimate the bounds of the distribution a sample came from using the CDF ofthe order statistics of the sample. Use with thebounder argument todensity_bounded().
Supportsautomatic partial function application withwaived arguments.
Usage
bounder_cdf(x, p = 0.01)Arguments
x | <numeric> Sample to estimate the bounds of. |
p | <scalarnumeric> in |
Details
bounder_cdf() uses the distribution of the order statistics ofX to estimate where the first and last order statistics (i.e. themin and max) of this distribution would be, assuming the samplex is thedistribution. Then, it adjusts the boundary outwards frommin(x) (ormax(x))by the distance betweenmin(x) (ormax(x)) and the nearest estimatedorder statistic.
TakingX =x, the distributions of the first and last order statistics are:
\begin{array}{rcl}F_{X_{(1)}}(x) &=& 1 - \left[1 - F_X(x)\right]^n\\F_{X_{(n)}}(x) &=& F_X(x)^n\end{array}
Re-arranging, we can get the inverse CDFs (quantile functions) of eachorder statistic in terms of the quantile function ofX (which wecan estimate from the data), giving us an estimate for the minimumand maximum order statistic:
\begin{array}{rcrcl}\hat{x_1} &=& F_{X_{(1)}}^{-1}(p) &=& F_X^{-1}\left[1 - (1 - p)^{1/n}\right]\\\hat{x_n} &=& F_{X_{(n)}}^{-1}(p) &=& F_X^{-1}\left[p^{1/n}\right]\end{array}
Then the estimated bounds are:
\left[2\min(x) - \hat{x_1}, 2\max(x) - \hat{x_n} \right]
These bounds depend onp, the percentile of the distribution of the orderstatistic used to form the estimate. Whilep = 0.5 (the median) might bea reasonable choice (and gives results similar tobounder_cooke()), this tendsto be a bit too aggressive in "detecting" bounded distributions, especially insmall sample sizes. Thus, we use a default ofp = 0.01, which tends tobe very conservative in small samples (in that it usually gives resultsroughly equivalent to an unbounded distribution), but which still performswell on bounded distributions when sample sizes are larger (in the thousands).
Value
A length-2 numeric vector giving an estimate of the minimum and maximum boundsof the distribution thatx came from.
See Also
Thebounder argument todensity_bounded().
Other bounds estimators:bounder_cooke(),bounder_range()
Estimate bounds of a distribution using Cooke's method
Description
Estimate the bounds of the distribution a sample came from using Cooke's method.Use with thebounder argument todensity_bounded().
Supportsautomatic partial function application withwaived arguments.
Usage
bounder_cooke(x)Arguments
x | <numeric> Sample to estimate the bounds of. |
Details
Estimate the bounds of a distribution using the method from Cooke (1979);i.e. method 2.3 from Loh (1984). These bounds are:
\left[\begin{array}{l}2X_{(1)} - \sum_{i = 1}^n \left[\left(1 - \frac{i - 1}{n}\right)^n - \left(1 - \frac{i}{n}\right)^n \right] X_{(i)}\\2X_{(n)} - \sum_{i = 1}^n \left[\left(1 - \frac{n - i}{n}\right)^n - \left(1 - \frac{n + 1 - i}{n} \right)^n\right] X_{(i)}\end{array}\right]
WhereX_{(i)} is theith order statistic ofx (i.e. itsith-smallest value).
Value
A length-2 numeric vector giving an estimate of the minimum and maximum boundsof the distribution thatx came from.
References
Cooke, P. (1979). Statistical inference for bounds of random variables.Biometrika 66(2), 367–374.doi:10.1093/biomet/66.2.367.
Loh, W. Y. (1984). Estimating an endpoint of a distribution with resamplingmethods.The Annals of Statistics 12(4), 1543–1550.doi:10.1214/aos/1176346811
See Also
Thebounder argument todensity_bounded().
Other bounds estimators:bounder_cdf(),bounder_range()
Estimate bounds of a distribution using the range of the sample
Description
Estimate the bounds of the distribution a sample came from using the range of the sample.Use with thebounder argument todensity_bounded().
Supportsautomatic partial function application withwaived arguments.
Usage
bounder_range(x)Arguments
x | <numeric> Sample to estimate the bounds of. |
Details
Estimate the bounds of a distribution usingrange(x).
Value
A length-2 numeric vector giving an estimate of the minimum and maximum boundsof the distribution thatx came from.
See Also
Thebounder argument todensity_bounded().
Other bounds estimators:bounder_cdf(),bounder_cooke()
Break (bin) selection algorithms for histograms
Description
Methods for determining breaks (bins) in histograms, as used in thebreaksargument todensity_histogram().
Supportsautomatic partial function application withwaived arguments.
Usage
breaks_fixed(x, weights = NULL, width = 1)breaks_Sturges(x, weights = NULL)breaks_Scott(x, weights = NULL)breaks_FD(x, weights = NULL, digits = 5)breaks_quantiles(x, weights = NULL, max_n = "Scott", min_width = 0.5)Arguments
x | <numeric> Sample values. |
weights | <numeric |NULL> Optional weights to apply to |
width | <scalarnumeric> For |
digits | <scalarnumeric> For |
max_n | <scalarnumeric |function |string>For |
min_width | <scalarnumeric> For |
Details
These functions take a sample and its weights and return a value suitable forthebreaks argument todensity_histogram() that will determine the histogrambreaks.
breaks_fixed()allows you to manually specify a fixed bin width.breaks_Sturges(),breaks_Scott(), andbreaks_FD()implement weightedversions of their corresponding base functions. They return a scalarnumeric giving the number of bins. Seenclass.Sturges(),nclass.scott(),andnclass.FD().breaks_quantiles()constructs irregularly-sized bins usingmax_n + 1(possibly weighted) quantiles ofx. The final number of bins isat mostmax_n, as small bins (ones whose bin width is less than halfthe range of the data divided bymax_ntimesmin_width) will be mergedinto adjacent bins.
Value
Either a single number (giving the number of bins) or a vectorgiving the edges between bins.
See Also
Examples
library(ggplot2)set.seed(1234)x = rnorm(2000, 1, 2)# Let's compare the different break-selection algorithms on this data:ggplot(data.frame(x), aes(x)) + stat_slab( aes(y = "breaks_fixed(width = 0.5)"), density = "histogram", breaks = breaks_fixed(width = 0.5), outline_bars = TRUE, color = "black", ) + stat_slab( aes(y = "breaks_Sturges()\nor 'Sturges'"), density = "histogram", breaks = "Sturges", outline_bars = TRUE, color = "black", ) + stat_slab( aes(y = "breaks_Scott()\nor 'Scott'"), density = "histogram", breaks = "Scott", outline_bars = TRUE, color = "black", ) + stat_slab( aes(y = "breaks_FD()\nor 'FD'"), density = "histogram", breaks = "FD", outline_bars = TRUE, color = "black", ) + stat_slab( aes(y = "breaks_quantiles()\nor 'quantiles'"), density = "histogram", breaks = "quantiles", outline_bars = TRUE, color = "black", ) + geom_point(aes(y = 0.7), alpha = 0.5) + labs( subtitle = "ggdist::stat_slab(density = 'histogram', ...)", y = "breaks =", x = NULL )Curvewise point and interval summaries for tidy data frames of draws from distributions
Description
Translates draws from distributions in a grouped data frame into a set of point andinterval summaries using a curve boxplot-inspired approach.
Usage
curve_interval( .data, ..., .along = NULL, .width = 0.5, na.rm = FALSE, .interval = c("mhd", "mbd", "bd", "bd-mbd"))## S3 method for class 'matrix'curve_interval( .data, ..., .along = NULL, .width = 0.5, na.rm = FALSE, .interval = c("mhd", "mbd", "bd", "bd-mbd"))## S3 method for class 'rvar'curve_interval( .data, ..., .along = NULL, .width = 0.5, na.rm = FALSE, .interval = c("mhd", "mbd", "bd", "bd-mbd"))## S3 method for class 'data.frame'curve_interval( .data, ..., .along = NULL, .width = 0.5, na.rm = FALSE, .interval = c("mhd", "mbd", "bd", "bd-mbd"), .simple_names = TRUE, .exclude = c(".chain", ".iteration", ".draw", ".row"))Arguments
.data | <data.frame |rvar |matrix> One of:
|
... | <barelanguage> Bare column names or expressions that, when evaluated in the context of |
.along | <tidyselect> Which columns are the input values to the functiondescribing the curve (e.g., the "x" values). Intervals are calculated jointly withrespect to these variables, conditional on all other grouping variables in the data frame. The default( |
.width | <numeric> Vector of probabilities to use that determine the widths of the resultingintervals. If multiple probabilities are provided, multiple rows per group are generated, each witha different probability interval (and value of the corresponding |
na.rm | <scalarlogical> Should |
.interval | <string> The method used to calculate the intervals. Currently, allmethods rank the curves using some measure ofdata depth, then create envelopes containing the
|
.simple_names | <scalarlogical> When |
.exclude | <character> Vector of names of columns to be excluded from summarizationif no column names are specified to be summarized. Default ignores several meta-data columnnames used inggdist andtidybayes. |
Details
Intervals are calculated by ranking the curves using some measure ofdata depth, thenusing binary search to find a cutoffk such that an envelope containing thek% "deepest"curves also contains.width% of the curves, for each value of.width (note thatkand.width are not necessarily the same). This is in contrast to most functional boxplotor curve boxplot approaches, which tend to simply take the.width% deepest curves, andare generally quite conservative (i.e. they may contain more than.width% of the curves).
See Mirzargaret al. (2014) or Juulet al. (2020) for an accessible introductionto data depth and curve boxplots / functional boxplots.
Value
A data frame containing point summaries and intervals, with at least one column correspondingto the point summary, one to the lower end of the interval, one to the upper end of the interval, thewidth of the interval (.width), the type of point summary (.point), and the type of interval (.interval).
Author(s)
Matthew Kay
References
Fraiman, Ricardo and Graciela Muniz. (2001)."Trimmed means for functional data".Test 10: 419–440.doi:10.1007/BF02595706.
Sun, Ying and Marc G. Genton. (2011)."Functional Boxplots".Journal of Computational and Graphical Statistics, 20(2): 316-334.doi:10.1198/jcgs.2011.09224
Mirzargar, Mahsa, Ross T Whitaker, and Robert M Kirby. (2014)."Curve Boxplot: Generalization of Boxplot for Ensembles of Curves".IEEE Transactions on Visualization and Computer Graphics. 20(12): 2654-2663.doi:10.1109/TVCG.2014.2346455
Juul Jonas, Kaare Græsbøll, Lasse Engbo Christiansen, and Sune Lehmann. (2020)."Fixed-time descriptive statistics underestimate extremes of epidemic curve ensembles".arXiv e-print.arXiv:2007.05035
See Also
point_interval() for pointwise intervals. Seevignette("lineribbon") for more examplesand discussion of the differences between pointwise and curvewise intervals.
Examples
library(dplyr)library(ggplot2)# generate a set of curvesk = 11 # number of curvesn = 201df = tibble( .draw = rep(1:k, n), mean = rep(seq(-5,5, length.out = k), n), x = rep(seq(-15,15,length.out = n), each = k), y = dnorm(x, mean, 3) )# see pointwise intervals...df %>% group_by(x) %>% median_qi(y, .width = c(.5)) %>% ggplot(aes(x = x, y = y)) + geom_lineribbon(aes(ymin = .lower, ymax = .upper)) + geom_line(aes(group = .draw), alpha=0.15, data = df) + scale_fill_brewer() + ggtitle("50% pointwise intervals with point_interval()") + theme_ggdist()# ... compare them to curvewise intervalsdf %>% group_by(x) %>% curve_interval(y, .width = c(.5)) %>% ggplot(aes(x = x, y = y)) + geom_lineribbon(aes(ymin = .lower, ymax = .upper)) + geom_line(aes(group = .draw), alpha=0.15, data = df) + scale_fill_brewer() + ggtitle("50% curvewise intervals with curve_interval()") + theme_ggdist()Categorize values from a CDF into quantile intervals
Description
Given a vector of probabilities from a cumulative distribution function (CDF)and a list of desired quantile intervals, return a vector categorizing eachelement of the input vector according to which quantile interval it falls into.NOTE: While this function can be used for (and was originally designed for)drawing slabs with intervals overlaid on the density, this is can now bedone more easily by mapping the.width orlevel computed variable toslab fill or color. SeeExamples.
Usage
cut_cdf_qi(p, .width = c(0.66, 0.95, 1), labels = NULL)Arguments
p | <numeric> Vector of values from a cumulative distribution function,such as values returned by |
.width | <numeric> Vector of probabilities to use that determine the widths of the resulting intervals. |
labels | <character |function |NULL> One of:
|
Value
Anordered factor of the same length asp giving the quantile interval towhich each value ofp belongs.
See Also
Seestat_slabinterval() andits shortcut stats, which generatecdf aesthetics that can be used withcut_cdf_qi() to draw slabs colored by their intervals.
Examples
library(ggplot2)library(dplyr)library(scales)library(distributional)theme_set(theme_ggdist())# NOTE: cut_cdf_qi() used to be the recommended way to do intervals overlaid# on densities, like this...tibble(x = dist_normal(0, 1)) %>% ggplot(aes(xdist = x)) + stat_slab( aes(fill = after_stat(cut_cdf_qi(cdf))) ) + scale_fill_brewer(direction = -1)# ... however this is now more easily and flexibly accomplished by directly# mapping .width or level onto fill:tibble(x = dist_normal(0, 1)) %>% ggplot(aes(xdist = x)) + stat_slab( aes(fill = after_stat(level)), .width = c(.66, .95, 1) ) + scale_fill_brewer()# See vignette("slabinterval") for more examples. The remaining examples# below using cut_cdf_qi() are kept for posterity.# With a halfeye (or other geom with slab and interval), NA values will# show up in the fill scale from the CDF function applied to the internal# interval geometry data and can be ignored, hence na.translate = FALSEtibble(x = dist_normal(0, 1)) %>% ggplot(aes(xdist = x)) + stat_halfeye(aes( fill = after_stat(cut_cdf_qi(cdf, .width = c(.5, .8, .95, 1))) )) + scale_fill_brewer(direction = -1, na.translate = FALSE)# we could also use the labels parameter to apply nicer formatting# and provide a better name for the legend, and omit the 100% interval# if desiredtibble(x = dist_normal(0, 1)) %>% ggplot(aes(xdist = x)) + stat_halfeye(aes( fill = after_stat(cut_cdf_qi( cdf, .width = c(.5, .8, .95), labels = percent_format(accuracy = 1) )) )) + labs(fill = "Interval") + scale_fill_brewer(direction = -1, na.translate = FALSE)Bounded density estimator using the reflection method
Description
Bounded density estimator using the reflection method.
Supportsautomatic partial function application withwaived arguments.
Usage
density_bounded( x, weights = NULL, n = 501, bandwidth = "dpi", adjust = 1, kernel = "gaussian", trim = TRUE, bounds = c(NA, NA), bounder = "cdf", adapt = 1, na.rm = FALSE, ..., range_only = FALSE)Arguments
x | <numeric> Sample to compute a density estimate for. |
weights | |
n | <scalarnumeric> The number of grid points to evaluate the density estimator at. |
bandwidth | <scalarnumeric |function |string>Bandwidth of the density estimator. One of:
|
adjust | <scalarnumeric> Value to multiply the bandwidth of the density estimator by. Default |
kernel | <string> The smoothing kernel to be used. This must partiallymatch one of |
trim | <scalarlogical> Should the density estimate be trimmed to the range of the data? Default |
bounds | <length-2numeric> Min and max bounds. If a bound is |
bounder | <function |string> Method to use to find missing(
|
adapt | <positiveinteger> (very experimental) The name and interpretation of this argumentare subject to change without notice. If |
na.rm | <scalarlogical> Should missing ( |
... | Additional arguments (ignored). |
range_only | <scalarlogical> If |
Value
An object of class"density", mimicking the output format ofstats::density(), with the following components:
x: The grid of points at which the density was estimated.y: The estimated density values.bw: The bandwidth.n: The sample size of thexinput argument.call: The call used to produce the result, as a quoted expression.data.name: The deparsed name of thexinput argument.has.na: AlwaysFALSE(for compatibility).cdf: Values of the (possibly weighted) empirical cumulative distributionfunction atx. Seeweighted_ecdf().
This allows existing methods for density objects, likeprint() andplot(), to work if desired.This output format (and in particular, thex andy components) is alsothe format expected by thedensity argument of thestat_slabinterval()and thesmooth_ family of functions.
References
Cooke, P. (1979). Statistical inference for bounds of random variables.Biometrika 66(2), 367–374.doi:10.1093/biomet/66.2.367.
Loh, W. Y. (1984). Estimating an endpoint of a distribution with resamplingmethods.The Annals of Statistics 12(4), 1543–1550.doi:10.1214/aos/1176346811
See Also
Other density estimators:density_histogram(),density_unbounded()
Examples
library(distributional)library(dplyr)library(ggplot2)# For compatibility with existing code, the return type of density_bounded()# is the same as stats::density(), ...set.seed(123)x = rbeta(5000, 1, 3)d = density_bounded(x)d# ... thus, while designed for use with the `density` argument of# stat_slabinterval(), output from density_bounded() can also be used with# base::plot():plot(d)# here we'll use the same data as above, but pick either density_bounded()# or density_unbounded() (which is equivalent to stats::density()). Notice# how the bounded density (green) is biased near the boundary of the support,# while the unbounded density is not.data.frame(x) %>% ggplot() + stat_slab( aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)), alpha = 0.25 ) + stat_slab(aes(x), density = "bounded", fill = NA, color = "#d95f02", alpha = 0.5) + stat_slab(aes(x), density = "unbounded", fill = NA, color = "#1b9e77", alpha = 0.5) + scale_thickness_shared() + theme_ggdist()# We can also supply arguments to the density estimators by using their# full function names instead of the string suffix; e.g. we can supply# the exact bounds of c(0,1) rather than using the bounds of the data.data.frame(x) %>% ggplot() + stat_slab( aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)), alpha = 0.25 ) + stat_slab( aes(x), fill = NA, color = "#d95f02", alpha = 0.5, density = density_bounded(bounds = c(0,1)) ) + scale_thickness_shared() + theme_ggdist()Histogram density estimator
Description
Histogram density estimator.
Supportsautomatic partial function application withwaived arguments.
Usage
density_histogram( x, weights = NULL, breaks = "Scott", align = "none", outline_bars = FALSE, right_closed = TRUE, outermost_closed = TRUE, na.rm = FALSE, ..., range_only = FALSE)Arguments
x | <numeric> Sample to compute a density estimate for. |
weights | |
breaks | <numeric |function |string> Determines the breakpoints defining bins. Default
For example, |
align | <scalarnumeric |function |string> Determines how to align the breakpoints defining bins. Default
For example, |
outline_bars | <scalarlogical> Should outlines in between the bars (i.e. density values of0) be included? |
right_closed | <scalarlogical> Should the right edge of each bin be closed? Fora bin with endpoints
Equivalent to the |
outermost_closed | <scalarlogical> Should values on the edges of the outermost (firstor last) bins always be included in those bins? If Equivalent to the |
na.rm | <scalarlogical> Should missing ( |
... | Additional arguments (ignored). |
range_only | <scalarlogical> If |
Value
An object of class"density", mimicking the output format ofstats::density(), with the following components:
x: The grid of points at which the density was estimated.y: The estimated density values.bw: The bandwidth.n: The sample size of thexinput argument.call: The call used to produce the result, as a quoted expression.data.name: The deparsed name of thexinput argument.has.na: AlwaysFALSE(for compatibility).cdf: Values of the (possibly weighted) empirical cumulative distributionfunction atx. Seeweighted_ecdf().
This allows existing methods for density objects, likeprint() andplot(), to work if desired.This output format (and in particular, thex andy components) is alsothe format expected by thedensity argument of thestat_slabinterval()and thesmooth_ family of functions.
See Also
Other density estimators:density_bounded(),density_unbounded()
Examples
library(distributional)library(dplyr)library(ggplot2)# For compatibility with existing code, the return type of density_unbounded()# is the same as stats::density(), ...set.seed(123)x = rbeta(5000, 1, 3)d = density_histogram(x)d# ... thus, while designed for use with the `density` argument of# stat_slabinterval(), output from density_histogram() can also be used with# base::plot():plot(d)# here we'll use the same data as above with stat_slab():data.frame(x) %>% ggplot() + stat_slab( aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)), alpha = 0.25 ) + stat_slab(aes(x), density = "histogram", fill = NA, color = "#d95f02", alpha = 0.5) + scale_thickness_shared() + theme_ggdist()Unbounded density estimator
Description
Unbounded density estimator usingstats::density().
Supportsautomatic partial function application withwaived arguments.
Usage
density_unbounded( x, weights = NULL, n = 501, bandwidth = "dpi", adjust = 1, kernel = "gaussian", trim = TRUE, adapt = 1, na.rm = FALSE, ..., range_only = FALSE)Arguments
x | <numeric> Sample to compute a density estimate for. |
weights | |
n | <scalarnumeric> The number of grid points to evaluate the density estimator at. |
bandwidth | <scalarnumeric |function |string>Bandwidth of the density estimator. One of:
|
adjust | <scalarnumeric> Value to multiply the bandwidth of the density estimator by. Default |
kernel | <string> The smoothing kernel to be used. This must partiallymatch one of |
trim | <scalarlogical> Should the density estimate be trimmed to the range of the data? Default |
adapt | <positiveinteger> (very experimental) The name and interpretation of this argumentare subject to change without notice. If |
na.rm | <scalarlogical> Should missing ( |
... | Additional arguments (ignored). |
range_only | <scalarlogical> If |
Value
An object of class"density", mimicking the output format ofstats::density(), with the following components:
x: The grid of points at which the density was estimated.y: The estimated density values.bw: The bandwidth.n: The sample size of thexinput argument.call: The call used to produce the result, as a quoted expression.data.name: The deparsed name of thexinput argument.has.na: AlwaysFALSE(for compatibility).cdf: Values of the (possibly weighted) empirical cumulative distributionfunction atx. Seeweighted_ecdf().
This allows existing methods for density objects, likeprint() andplot(), to work if desired.This output format (and in particular, thex andy components) is alsothe format expected by thedensity argument of thestat_slabinterval()and thesmooth_ family of functions.
See Also
Other density estimators:density_bounded(),density_histogram()
Examples
library(distributional)library(dplyr)library(ggplot2)# For compatibility with existing code, the return type of density_unbounded()# is the same as stats::density(), ...set.seed(123)x = rbeta(5000, 1, 3)d = density_unbounded(x)d# ... thus, while designed for use with the `density` argument of# stat_slabinterval(), output from density_unbounded() can also be used with# base::plot():plot(d)# here we'll use the same data as above, but pick either density_bounded()# or density_unbounded() (which is equivalent to stats::density()). Notice# how the bounded density (green) is biased near the boundary of the support,# while the unbounded density is not.data.frame(x) %>% ggplot() + stat_slab( aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)), alpha = 0.25 ) + stat_slab(aes(x), density = "bounded", fill = NA, color = "#d95f02", alpha = 0.5) + stat_slab(aes(x), density = "unbounded", fill = NA, color = "#1b9e77", alpha = 0.5) + scale_thickness_shared() + theme_ggdist()Dynamically select a good bin width for a dotplot
Description
Searches for a nice-looking bin width to use to draw a dotplot such thatthe height of the dotplot fits within a given space (maxheight).
Usage
find_dotplot_binwidth( x, maxheight, heightratio = 1, stackratio = 1, layout = c("bin", "weave", "hex", "swarm", "bar"))Arguments
x | <numeric> Data values. |
maxheight | <scalarnumeric> Maximum height of the dotplot. |
heightratio | <scalarnumeric> Ratio of bin width to dot height. |
stackratio | <scalarnumeric> Ratio of dot height to vertical distancebetween dot centers |
layout | <string> The layout method used for the dots. One of:
|
Details
This dynamic bin selection algorithm uses a binary search over the number ofbins to find a bin width such that if the input data (x) is binnedusing a Wilkinson-style dotplot algorithm the height of the tallest binwill be less thanmaxheight.
This algorithm is used bygeom_dotsinterval() (and its variants) to automaticallyselect bin widths. Unless you are manually implementing you own dotplotgroborgeom, you probably do not need to use this function directly
Value
A suitable bin width such that a dotplot created with this bin widthandheightratio should have its tallest bin be less than or equal tomaxheight.
See Also
bin_dots() for an algorithm can bin dots using bin widths selectedby this function;geom_dotsinterval() for geometries that usethese algorithms to create dotplots.
Examples
library(dplyr)library(ggplot2)x = qnorm(ppoints(20))binwidth = find_dotplot_binwidth(x, maxheight = 4, heightratio = 1)binwidthbin_df = bin_dots(x = x, y = 0, binwidth = binwidth, heightratio = 1)bin_df# we can manually plot the binning above, though this is only recommended# if you are using find_dotplot_binwidth() and bin_dots() to build your own# grob. For practical use it is much easier to use geom_dots(), which will# automatically select good bin widths for you (and which uses# find_dotplot_binwidth() and bin_dots() internally)bin_df %>% ggplot(aes(x = x, y = y)) + geom_point(size = 4) + coord_fixed()Blurry dot plot (geom)
Description
Variant ofgeom_dots() for creating blurry dotplots. Accepts ansdaesthetic that gives the standard deviation of the blur applied to the dots.Requires a graphics engine supporting radial gradients. Unlikegeom_dots(),this geom only supports circular and squareshapes.
Usage
geom_blur_dots( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., blur = "gaussian", binwidth = NA, dotsize = 1.07, stackratio = 1, layout = "bin", overlaps = "nudge", smooth = "none", overflow = "warn", verbose = FALSE, orientation = NA, subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
blur | <function |string> Blur function to apply to dots.One of:
|
binwidth | <numeric |unit> The bin width to use for laying out the dots.One of:
If the value is numeric, it is assumed to be in units of data. The bin width(or its bounds) can also be specified using |
dotsize | <scalarnumeric> The width of the dots relative to the |
stackratio | <scalarnumeric> The distance between the center of the dots in the samestack relative to the dot height. The default, |
layout | <string> The layout method used for the dots. One of:
|
overlaps | <string> How to handle overlapping dots or bins in the
|
smooth | <function |string> Smoother to apply to dot positions.One of:
Smoothing is most effective when the smoother is matched to the support ofthe distribution; e.g. using |
overflow | <string> How to handle overflow of dots beyond the extent of the geomwhen a minimum
If you find the default layout has dots that are too small, and you are okaywith dots overlapping, consider setting |
verbose | <scalarlogical> If |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
Value
Aggplot2::Geom representing a blurry dot geometry which canbe added to aggplot() object.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Dots-specific (aka Slab-specific) aesthetics
sd: The standard deviation (in data units) of the blur associated with each dot.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seegeom_dotsinterval() for the geometry this shortcut is based on.
Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval geoms:geom_dots(),geom_dotsinterval(),geom_swarm(),geom_weave()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(1234)x = rnorm(1000)# manually calculate quantiles and their MCSE# this could also be done more succinctly with stat_mcse_dots()p = ppoints(100)df = data.frame( q = quantile(x, p), se = posterior::mcse_quantile(x, p))df %>% ggplot(aes(x = q, sd = se)) + geom_blur_dots()df %>% ggplot(aes(x = q, sd = se)) + # or blur = blur_interval(.width = .95) to set the interval width geom_blur_dots(blur = "interval")Dot plot (shortcut geom)
Description
Shortcut version ofgeom_dotsinterval() for creating dot plots.Geoms based ongeom_dotsinterval() create dotplots that automaticallyensure the plot fits within the available space.
Roughly equivalent to:
geom_dotsinterval( show_point = FALSE, show_interval = FALSE)
Usage
geom_dots( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., binwidth = NA, dotsize = 1.07, stackratio = 1, layout = "bin", overlaps = "nudge", smooth = "none", overflow = "warn", verbose = FALSE, orientation = NA, subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
binwidth | <numeric |unit> The bin width to use for laying out the dots.One of:
If the value is numeric, it is assumed to be in units of data. The bin width(or its bounds) can also be specified using |
dotsize | <scalarnumeric> The width of the dots relative to the |
stackratio | <scalarnumeric> The distance between the center of the dots in the samestack relative to the dot height. The default, |
layout | <string> The layout method used for the dots. One of:
|
overlaps | <string> How to handle overlapping dots or bins in the
|
smooth | <function |string> Smoother to apply to dot positions.One of:
Smoothing is most effective when the smoother is matched to the support ofthe distribution; e.g. using |
overflow | <string> How to handle overflow of dots beyond the extent of the geomwhen a minimum
If you find the default layout has dots that are too small, and you are okaywith dots overlapping, consider setting |
verbose | <scalarlogical> If |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
Value
Aggplot2::Geom representing a dot geometry which canbe added to aggplot() object.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Dots-specific (aka Slab-specific) aesthetics
family: The font family used to draw the dots.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seestat_dots() for the stat version, intended foruse on sample data or analytical distributions.
Seegeom_dotsinterval() for the geometry this shortcut is based on.
Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval geoms:geom_blur_dots(),geom_dotsinterval(),geom_swarm(),geom_weave()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(12345)df = tibble( g = rep(c("a", "b"), 200), value = rnorm(400, c(0, 3), c(0.75, 1)))# orientation is detected automatically based on# which axis is discretedf %>% ggplot(aes(x = value, y = g)) + geom_dots()df %>% ggplot(aes(y = value, x = g)) + geom_dots()Automatic dotplot + point + interval meta-geom
Description
This meta-geom supports drawing combinations of dotplots, points, and intervals.Geoms and stats based ongeom_dotsinterval() create dotplots that automatically determine a bin width thatensures the plot fits within the available space. They also ensure dots do not overlap, and allowthe generation of quantile dotplots using thequantiles argument tostat_dotsinterval()/stat_dots().Generally follows the naming scheme andarguments of thegeom_slabinterval() andstat_slabinterval() family ofgeoms and stats.
Usage
geom_dotsinterval( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., binwidth = NA, dotsize = 1.07, stackratio = 1, layout = "bin", overlaps = "nudge", smooth = "none", overflow = "warn", verbose = FALSE, orientation = NA, interval_size_domain = c(1, 6), interval_size_range = c(0.6, 1.4), fatten_point = 1.8, arrow = NULL, show_slab = TRUE, show_point = TRUE, show_interval = TRUE, subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
binwidth | <numeric |unit> The bin width to use for laying out the dots.One of:
If the value is numeric, it is assumed to be in units of data. The bin width(or its bounds) can also be specified using |
dotsize | <scalarnumeric> The width of the dots relative to the |
stackratio | <scalarnumeric> The distance between the center of the dots in the samestack relative to the dot height. The default, |
layout | <string> The layout method used for the dots. One of:
|
overlaps | <string> How to handle overlapping dots or bins in the
|
smooth | <function |string> Smoother to apply to dot positions.One of:
Smoothing is most effective when the smoother is matched to the support ofthe distribution; e.g. using |
overflow | <string> How to handle overflow of dots beyond the extent of the geomwhen a minimum
If you find the default layout has dots that are too small, and you are okaywith dots overlapping, consider setting |
verbose | <scalarlogical> If |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
interval_size_domain | <length-2numeric> Minimum and maximum of the values of the |
interval_size_range | <length-2numeric> This geom scales the raw size aesthetic values whendrawing interval and point sizes, as they tend to be too thick when usingthe default settings of |
fatten_point | <scalarnumeric> A multiplicative factor used to adjust the size of the point relative to thesize of the thickest interval line. If you wish to specify point sizes directly, you can also usethe |
arrow | <arrow |NULL> Type of arrow heads to use on the interval, or |
show_slab | <scalarlogical> Should the slab portion of the geom be drawn? |
show_point | <scalarlogical> Should the point portion of the geom be drawn? |
show_interval | <scalarlogical> Should the interval portion of the geom be drawn? |
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Geom orggplot2::Stat representing a dotplot or combined dotplot+interval geometry which canbe added to aggplot() object.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Dots-specific (aka Slab-specific) aesthetics
family: The font family used to draw the dots.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
Author(s)
Matthew Kay
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
See thestat_slabinterval() family for otherstats built on top ofgeom_slabinterval().Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval geoms:geom_blur_dots(),geom_dots(),geom_swarm(),geom_weave()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(12345)df = tibble( g = rep(c("a", "b"), 200), value = rnorm(400, c(0, 3), c(0.75, 1)))# orientation is detected automatically based on# which axis is discretedf %>% ggplot(aes(x = value, y = g)) + geom_dotsinterval()df %>% ggplot(aes(y = value, x = g)) + geom_dotsinterval()# stat_dots can summarize quantiles, creating quantile dotplotsdata(RankCorr_u_tau, package = "ggdist")RankCorr_u_tau %>% ggplot(aes(x = u_tau, y = factor(i))) + stat_dots(quantiles = 100)# color and fill aesthetics can be mapped within the geom# dotsinterval adds an intervalRankCorr_u_tau %>% ggplot(aes(x = u_tau, y = factor(i), fill = after_stat(x > 6))) + stat_dotsinterval(quantiles = 100)Multiple-interval plot (shortcut geom)
Description
Shortcut version ofgeom_slabinterval() for creating multiple-interval plots.
Roughly equivalent to:
geom_slabinterval( aes( datatype = "interval", side = "both" ), interval_size_range = c(1, 6), show_slab = FALSE, show_point = FALSE)
Usage
geom_interval( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., orientation = NA, interval_size_range = c(1, 6), interval_size_domain = c(1, 6), arrow = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
interval_size_range | <length-2numeric> This geom scales the raw size aesthetic values whendrawing interval and point sizes, as they tend to be too thick when usingthe default settings of |
interval_size_domain | <length-2numeric> Minimum and maximum of the values of the |
arrow | <arrow |NULL> Type of arrow heads to use on the interval, or |
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
This geom wrapsgeom_slabinterval() with defaults designed to producemultiple-interval plots. Default aesthetic mappings are applied if the.width columnis present in the input data (e.g., as generated by thepoint_interval() family of functions),making this geom often more convenient than vanillaggplot2 geometries when used withfunctions likemedian_qi(),mean_qi(),mode_hdi(), etc.
Specifically, if.width is present in the input,geom_interval() actsas if its default aesthetics areaes(colour = forcats::fct_rev(ordered(.width)))
Value
Aggplot2::Geom representing a multiple-interval geometry which canbe added to aggplot() object.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Deprecated aesthetics
interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seestat_interval() for the stat version, intended foruse on sample data or analytical distributions.Seegeom_slabinterval() for the geometry this shortcut is based on.
Other slabinterval geoms:geom_pointinterval(),geom_slab(),geom_spike()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())data(RankCorr_u_tau, package = "ggdist")# orientation is detected automatically based on# use of xmin/xmax or ymin/ymaxRankCorr_u_tau %>% group_by(i) %>% median_qi(.width = c(.5, .8, .95, .99)) %>% ggplot(aes(y = i, x = u_tau, xmin = .lower, xmax = .upper)) + geom_interval() + scale_color_brewer()RankCorr_u_tau %>% group_by(i) %>% median_qi(.width = c(.5, .8, .95, .99)) %>% ggplot(aes(x = i, y = u_tau, ymin = .lower, ymax = .upper)) + geom_interval() + scale_color_brewer()Line + multiple-ribbon plots (ggplot geom)
Description
A combination ofgeom_line() andgeom_ribbon()with default aesthetics designed for use with output frompoint_interval().
Usage
geom_lineribbon( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., step = FALSE, orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | A position adjustment to use on the data for this layer. Thiscan be used in various ways, including to prevent overplotting andimproving the display. The
|
... | Other arguments passed to |
step | <scalarlogical |string> Should the line/ribbon be drawnas a step function? One of:
|
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
geom_lineribbon() is a combination of ageom_line() andgeom_ribbon() designed for use with output frompoint_interval().This geom sets some default aesthetics equal to the.width column generated by thepoint_interval() family of functions, making them often more convenient than a vanillageom_ribbon() +geom_line().
Specifically,geom_lineribbon() acts as if its default aesthetics areaes(fill = forcats::fct_rev(ordered(.width))).
Value
Aggplot2::Geom representing a combined line + multiple-ribbon geometry which canbe added to aggplot() object.
Aesthetics
The line+ribbonstats andgeoms have a wide variety of aesthetics that controlthe appearance of their two sub-geometries: theline and theribbon.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Ribbon-specific aesthetics
xmin: Left edge of the ribbon sub-geometry (iforientation = "horizontal").xmax: Right edge of the ribbon sub-geometry (iforientation = "horizontal").ymin: Lower edge of the ribbon sub-geometry (iforientation = "vertical").ymax: Upper edge of the ribbon sub-geometry (iforientation = "vertical").order: The order in which ribbons are drawn. Ribbons with the smallest mean value oforderare drawn first (i.e., will be drawn below ribbons with larger mean values oforder). Iforderis not supplied togeom_lineribbon(),-abs(xmax - xmin)or-abs(ymax - ymax)(depending onorientation) is used, having the effect of drawing the widest (on average)ribbons on the bottom.stat_lineribbon()usesorder = after_stat(level)by default,causing the ribbons generated from the largest.widthto be drawn on the bottom.
Color aesthetics
colour: (orcolor) The color of theline sub-geometry.fill: The fill color of theribbon sub-geometry.alpha: The opacity of theline andribbon sub-geometries.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width ofline. Inggplot2 < 3.4, was calledsize.linetype: Type ofline (e.g.,"solid","dashed", etc)
Other aesthetics (these work as in standardgeoms)
group
See examples of some of these aesthetics in action invignette("lineribbon").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
Author(s)
Matthew Kay
See Also
Seestat_lineribbon() for a version that does summarizing of samples into points and intervalswithin ggplot. Seegeom_pointinterval() for a similar geom intendedfor point summaries and intervals. Seegeom_line() andgeom_ribbon() and for the geoms this is based on.
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(12345)tibble( x = rep(1:10, 100), y = rnorm(1000, x)) %>% group_by(x) %>% median_qi(.width = c(.5, .8, .95)) %>% ggplot(aes(x = x, y = y, ymin = .lower, ymax = .upper)) + # automatically uses aes(fill = forcats::fct_rev(ordered(.width))) geom_lineribbon() + scale_fill_brewer()Point + multiple-interval plot (shortcut geom)
Description
Shortcut version ofgeom_slabinterval() for creating point + multiple-interval plots.
Roughly equivalent to:
geom_slabinterval( aes( datatype = "interval", side = "both" ), show_slab = FALSE, show.legend = c(size = FALSE))
Usage
geom_pointinterval( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., orientation = NA, interval_size_domain = c(1, 6), interval_size_range = c(0.6, 1.4), fatten_point = 1.8, arrow = NULL, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
interval_size_domain | <length-2numeric> Minimum and maximum of the values of the |
interval_size_range | <length-2numeric> This geom scales the raw size aesthetic values whendrawing interval and point sizes, as they tend to be too thick when usingthe default settings of |
fatten_point | <scalarnumeric> A multiplicative factor used to adjust the size of the point relative to thesize of the thickest interval line. If you wish to specify point sizes directly, you can also usethe |
arrow | <arrow |NULL> Type of arrow heads to use on the interval, or |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends?Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
This geom wrapsgeom_slabinterval() with defaults designed to producepoint + multiple-interval plots. Default aesthetic mappings are applied if the.width columnis present in the input data (e.g., as generated by thepoint_interval() family of functions),making this geom often more convenient than vanillaggplot2 geometries when used withfunctions likemedian_qi(),mean_qi(),mode_hdi(), etc.
Specifically, if.width is present in the input,geom_pointinterval() actsas if its default aesthetics areaes(size = -.width)
Value
Aggplot2::Geom representing a point + multiple-interval geometry which canbe added to aggplot() object.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seestat_pointinterval() for the stat version, intended foruse on sample data or analytical distributions.Seegeom_slabinterval() for the geometry this shortcut is based on.
Other slabinterval geoms:geom_interval(),geom_slab(),geom_spike()
Examples
library(dplyr)library(ggplot2)data(RankCorr_u_tau, package = "ggdist")# orientation is detected automatically based on# use of xmin/xmax or ymin/ymaxRankCorr_u_tau %>% group_by(i) %>% median_qi(.width = c(.8, .95)) %>% ggplot(aes(y = i, x = u_tau, xmin = .lower, xmax = .upper)) + geom_pointinterval()RankCorr_u_tau %>% group_by(i) %>% median_qi(.width = c(.8, .95)) %>% ggplot(aes(x = i, y = u_tau, ymin = .lower, ymax = .upper)) + geom_pointinterval()Slab (ridge) plot (shortcut geom)
Description
Shortcut version ofgeom_slabinterval() for creating slab (ridge) plots.
Roughly equivalent to:
geom_slabinterval( show_point = FALSE, show_interval = FALSE)
Usage
geom_slab( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., orientation = NA, subscale = "thickness", normalize = "all", fill_type = "segments", subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subscale | <function |string> Sub-scale used to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
normalize | <string> Groups within which to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
fill_type | <string> What type of fill to use when the fill color or alpha varies within a slab.One of:
|
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Value
Aggplot2::Geom representing a slab (ridge) geometry which canbe added to aggplot() object.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Deprecated aesthetics
slab_size: Useslab_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seestat_slab() for the stat version, intended foruse on sample data or analytical distributions.Seegeom_slabinterval() for the geometry this shortcut is based on.
Other slabinterval geoms:geom_interval(),geom_pointinterval(),geom_spike()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())# we will manually demonstrate plotting a density with geom_slab(),# though generally speaking this is easier to do using stat_slab(), which# will determine sensible limits automatically and correctly adjust# densities when using scale transformationsdf = expand.grid( mean = 1:3, input = seq(-2, 6, length.out = 100) ) %>% mutate( group = letters[4 - mean], density = dnorm(input, mean, 1) )# orientation is detected automatically based on# use of x or ydf %>% ggplot(aes(y = group, x = input, thickness = density)) + geom_slab()df %>% ggplot(aes(x = group, y = input, thickness = density)) + geom_slab()# RIDGE PLOTS# "ridge" plots can be created by increasing the slab height and# setting the slab colordf %>% ggplot(aes(y = group, x = input, thickness = density)) + geom_slab(height = 2, color = "black")Slab + point + interval meta-geom
Description
This meta-geom supports drawing combinations of functions (as slabs, aka ridge plots or joy plots), points, andintervals. It acts as a meta-geom for many otherggdist geoms that are wrappers around this geom, includingeye plots, half-eye plots, CCDF barplots, and point+multiple interval plots, and supports both horizontal andvertical orientations, dodging (via theposition argument), and relative justification of slabs with theircorresponding intervals.
Usage
geom_slabinterval( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., orientation = NA, subscale = "thickness", normalize = "all", fill_type = "segments", interval_size_domain = c(1, 6), interval_size_range = c(0.6, 1.4), fatten_point = 1.8, arrow = NULL, show_slab = TRUE, show_point = TRUE, show_interval = TRUE, subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subscale | <function |string> Sub-scale used to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
normalize | <string> Groups within which to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
fill_type | <string> What type of fill to use when the fill color or alpha varies within a slab.One of:
|
interval_size_domain | <length-2numeric> Minimum and maximum of the values of the |
interval_size_range | <length-2numeric> This geom scales the raw size aesthetic values whendrawing interval and point sizes, as they tend to be too thick when usingthe default settings of |
fatten_point | <scalarnumeric> A multiplicative factor used to adjust the size of the point relative to thesize of the thickest interval line. If you wish to specify point sizes directly, you can also usethe |
arrow | <arrow |NULL> Type of arrow heads to use on the interval, or |
show_slab | <scalarlogical> Should the slab portion of the geom be drawn? |
show_point | <scalarlogical> Should the point portion of the geom be drawn? |
show_interval | <scalarlogical> Should the interval portion of the geom be drawn? |
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
geom_slabinterval() is a flexible meta-geom that you can use directly or through a variety of "shortcut"geoms that represent useful combinations of the various parameters of this geom. In many cases you will want touse the shortcut geoms instead as they create more useful mnemonic primitives, such as eye plots,half-eye plots, point+interval plots, or CCDF barplots.
Theslab portion of the geom is much like a ridge or "joy" plot: it represents the value of a functionscaled to fit between values on thex ory axis (depending on the value oforientation). Values ofthe functions are specified using thethickness aesthetic and are scaled to fit intoscaletimes the distance between points on the relevant axis. E.g., iforientation is"horizontal",scale is0.9, andy is a discrete variable, then thethickness aesthetic specifies thevalue of some function ofx that is drawn for everyy value and scaled to fit into0.9 timesthe distance between points on they axis.
For theinterval portion of the geom,x andy aesthetics specify the location of thepoint, andymin/ymax orxmin/xmax (depending on the value oforientation)specify the endpoints of the interval. A scaling factor for interval line width and point size is appliedthrough theinterval_size_domain,interval_size_range, andfatten_point parameters.These scaling factors are designed to give multiple uncertainty intervals reasonablescaling at the default settings forscale_size_continuous().
As a combination geom, this geom expects adatatype aesthetic specifying which part of the geom a givenrow in the input data corresponds to:"slab" or"interval". However, specifying this aestheticmanually is typically only necessary if you use this geom directly; the numerous wrapper geoms willusually set this aesthetic for you as needed, and their use is recommended unless you have a very customuse case.
Wrapper geoms include:
In addition, thestat_slabinterval() family of stats uses geoms from thegeom_slabinterval() family, and is often easier to use than using these geomsdirectly. Typically, thegeom_* versions are meant for use with already-summarized data (such as intervals) and thestat_* versions are summarize the data themselves (usually draws from a distribution) to produce the geom.
Value
Aggplot2::Geom representing a slab or combined slab+interval geometry which canbe added to aggplot() object.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
Author(s)
Matthew Kay
See Also
Seegeom_lineribbon() for a combination geom designed for fit curves plus probability bands.Seegeom_dotsinterval() for a combination geom designed for plotting dotplots with intervals.Seestat_slabinterval() for families of statsbuilt on top of this geom for common use cases (likestat_halfeye()).Seevignette("slabinterval") for a variety of examples of use.
Examples
# geom_slabinterval() is typically not that useful on its own.# See vignette("slabinterval") for a variety of examples of the use of its# shortcut geoms and stats, which are more useful than using# geom_slabinterval() directly.Spike plot (ggplot2 geom)
Description
Geometry for drawing "spikes" (optionally with points on them) on top ofgeom_slabinterval() geometries: this geometry understands the scaling andpositioning of thethickness aesthetic fromgeom_slabinterval(), whichallows you to position spikes and points along a slab.
Usage
geom_spike( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., subguide = "spike", orientation = NA, subscale = "thickness", normalize = "all", arrow = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
subguide | <function |string> Sub-guide used to annotate the
|
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subscale | <function |string> Sub-scale used to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
normalize | <string> Groups within which to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
arrow | <arrow |NULL> Type of arrow heads to use on the spike, or |
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
This geometry consists of a "spike" (vertical/horizontal line segment) and a"point" (at the end of the line segment). It uses thethickness aestheticto determine where the endpoint of the line is, which allows it to be usedwithgeom_slabinterval() geometries for labeling specific values of thethickness function.
Value
Aggplot2::Geom representing a spike geometry which canbe added to aggplot() object.rd_slabinterval_aesthetics(geom_name),
Aesthetics
The spikegeom has a wide variety of aesthetics that controlthe appearance of its two sub-geometries: thespike and thepoint.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Spike-specific (aka Slab-specific) aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.
Color aesthetics
colour: (orcolor) The color of thespike andpoint sub-geometries.fill: The fill color of thepoint sub-geometry.alpha: The opacity of thespike andpoint sub-geometries.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw thespike sub-geometry.size: Size of thepoint sub-geometry.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw thespike.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seestat_spike() for the stat version, intended foruse on sample data or analytical distributions.
Other slabinterval geoms:geom_interval(),geom_pointinterval(),geom_slab()
Examples
library(ggplot2)library(distributional)library(dplyr)# geom_spike is easiest to use with distributional or# posterior::rvar objectsdf = tibble( d = dist_normal(1:2, 1:2), g = c("a", "b"))# annotate the density at the mean of a distributiondf %>% mutate( mean = mean(d), density(d, list(density_at_mean = mean))) %>% ggplot(aes(y = g)) + stat_slab(aes(xdist = d)) + geom_spike(aes(x = mean, thickness = density_at_mean)) + # need shared thickness scale so that stat_slab and geom_spike line up scale_thickness_shared()# annotate the endpoints of intervals of a distribution# here we'll use an arrow instead of a point by setting size = 0arrow_spec = arrow(angle = 45, type = "closed", length = unit(4, "pt"))df %>% mutate( median_qi(d, .width = 0.9), density(d, list(density_lower = .lower, density_upper = .upper))) %>% ggplot(aes(y = g)) + stat_halfeye(aes(xdist = d), .width = 0.9, color = "gray35") + geom_spike( aes(x = .lower, thickness = density_lower), size = 0, arrow = arrow_spec, color = "blue", linewidth = 0.75 ) + geom_spike( aes(x = .upper, thickness = density_upper), size = 0, arrow = arrow_spec, color = "red", linewidth = 0.75 ) + scale_thickness_shared()Beeswarm plot (shortcut geom)
Description
Shortcut version ofgeom_dotsinterval() for creating beeswarm plots.Geoms based ongeom_dotsinterval() create dotplots that automaticallyensure the plot fits within the available space.
Roughly equivalent to:
geom_dots( aes(side = "both"), overflow = "compress", binwidth = unit(1.5, "mm"), layout = "swarm")
Usage
geom_swarm( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., overflow = "compress", binwidth = unit(1.5, "mm"), layout = "swarm", dotsize = 1.07, stackratio = 1, overlaps = "nudge", smooth = "none", verbose = FALSE, orientation = NA, subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
overflow | <string> How to handle overflow of dots beyond the extent of the geomwhen a minimum
If you find the default layout has dots that are too small, and you are okaywith dots overlapping, consider setting |
binwidth | <numeric |unit> The bin width to use for laying out the dots.One of:
If the value is numeric, it is assumed to be in units of data. The bin width(or its bounds) can also be specified using |
layout | <string> The layout method used for the dots. One of:
|
dotsize | <scalarnumeric> The width of the dots relative to the |
stackratio | <scalarnumeric> The distance between the center of the dots in the samestack relative to the dot height. The default, |
overlaps | <string> How to handle overlapping dots or bins in the
|
smooth | <function |string> Smoother to apply to dot positions.One of:
Smoothing is most effective when the smoother is matched to the support ofthe distribution; e.g. using |
verbose | <scalarlogical> If |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
Value
Aggplot2::Geom representing a beeswarm geometry which canbe added to aggplot() object.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Dots-specific (aka Slab-specific) aesthetics
family: The font family used to draw the dots.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seegeom_dotsinterval() for the geometry this shortcut is based on.
Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval geoms:geom_blur_dots(),geom_dots(),geom_dotsinterval(),geom_weave()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(12345)df = tibble( g = rep(c("a", "b"), 200), value = rnorm(400, c(0, 3), c(0.75, 1)))# orientation is detected automatically based on# which axis is discretedf %>% ggplot(aes(x = value, y = g)) + geom_swarm()df %>% ggplot(aes(y = value, x = g)) + geom_swarm()Dot-weave plot (shortcut geom)
Description
Shortcut version ofgeom_dotsinterval() for creating dot-weave plots.Geoms based ongeom_dotsinterval() create dotplots that automaticallyensure the plot fits within the available space.
Roughly equivalent to:
geom_dots( aes(side = "both"), layout = "weave", overflow = "compress", binwidth = unit(1.5, "mm"))
Usage
geom_weave( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., layout = "weave", overflow = "compress", binwidth = unit(1.5, "mm"), dotsize = 1.07, stackratio = 1, overlaps = "nudge", smooth = "none", verbose = FALSE, orientation = NA, subguide = "slab", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
stat | The statistical transformation to use on the data for this layer.When using a
|
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to |
layout | <string> The layout method used for the dots. One of:
|
overflow | <string> How to handle overflow of dots beyond the extent of the geomwhen a minimum
If you find the default layout has dots that are too small, and you are okaywith dots overlapping, consider setting |
binwidth | <numeric |unit> The bin width to use for laying out the dots.One of:
If the value is numeric, it is assumed to be in units of data. The bin width(or its bounds) can also be specified using |
dotsize | <scalarnumeric> The width of the dots relative to the |
stackratio | <scalarnumeric> The distance between the center of the dots in the samestack relative to the dot height. The default, |
overlaps | <string> How to handle overlapping dots or bins in the
|
smooth | <function |string> Smoother to apply to dot positions.One of:
Smoothing is most effective when the smoother is matched to the support ofthe distribution; e.g. using |
verbose | <scalarlogical> If |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
subguide | <function |string> Sub-guide used to annotate the
|
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
Value
Aggplot2::Geom representing a dot-weave geometry which canbe added to aggplot() object.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Positional aesthetics
x: x position of the geometryy: y position of the geometry
Dots-specific (aka Slab-specific) aesthetics
family: The font family used to draw the dots.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seegeom_dotsinterval() for the geometry this shortcut is based on.
Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval geoms:geom_blur_dots(),geom_dots(),geom_dotsinterval(),geom_swarm()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(12345)df = tibble( g = rep(c("a", "b"), 200), value = rnorm(400, c(0, 3), c(0.75, 1)))# orientation is detected automatically based on# which axis is discretedf %>% ggplot(aes(x = value, y = g)) + geom_weave()df %>% ggplot(aes(y = value, x = g)) + geom_weave()Deprecated functions and arguments in ggdist
Description
Deprecated functions and arguments and their alternatives are listed below.
Deprecated stats and geoms
Thestat_sample_... andstat_dist_... families of stats were merged in ggdist 3.1.This means:
stat_dist_...is deprecated. For any code usingstat_dist_XXX(), you should nowbe able to usestat_XXX()instead without additional modifications in almost all cases.stat_sample_slabinterval()is deprecated. You should be able to usestat_slabinterval()instead without additional modifications in almost all cases.
The oldstat_dist_... names are currently kept as aliases, but may be removed in the future.
Deprecated arguments
Deprecated parameters forstat_slabinterval() and family:
The
.probargument, which is a long-deprecated alias for.width, wasremoved in ggdist 3.1.The
limits_functionargument: this was a parameter for determining thefunction to compute limits of the slab instat_slabinterval()and itsderived stats. This function is really an internal function only needed bysubclasses of the base class, yet added a lot of noise to the documentation,so it was replaced withAbstractStatSlabInterval$compute_limits().The
limits_argsargument: extra stat parameters are now passed through tothe...arguments toAbstractStatSlabInterval$compute_limits(); usethese instead.The
slab_functionargument: this was a parameter for determining thefunction to compute slabs instat_slabinterval()and itsderived stats. This function is really an internal function only needed bysubclasses of the base class, yet added a lot of noise to the documentation,so it was replaced withAbstractStatSlabInterval$compute_slab().The
slab_argsargument: extra stat parameters are now passed through tothe...arguments toAbstractStatSlabInterval$compute_slab(); usethese instead.The
slab_typeargument: instead of setting the slab type, either adjustthedensityargument (e.g. usedensity = "histogram"to replaceslab_type = "histogram") or use thepdforcdfcomputed variablesmapped onto an appropriate aesthetic (e.g. useaes(thickness = after_stat(cdf))to create a CDF).The
interval_functionandfun.dataarguments: these were parameters for determining thefunction to compute intervals instat_slabinterval()and itsderived stats. This function is really an internal function only needed bysubclasses of the base class, yet added a lot of noise to the documentation,so it was replaced withAbstractStatSlabInterval$compute_interval().The
interval_argsandfun.argsarguments: to pass extra arguments toapoint_intervalreplace the value of thepoint_intervalargument witha simple wrapper; e.g.stat_halfeye(point_interval = \(...) point_interval(..., extra_arg = XXX))
Deprecated parameters forgeom_slabinterval() and family:
The
size_domainandsize_rangearguments, which are long-deprecated aliasesforinterval_size_domainandinterval_size_range, were removed in ggdist 3.1.
Author(s)
Matthew Kay
Continuous guide for colour ramp scales (ggplot2 guide)
Description
A colour ramp bar guide that shows continuous colour ramp scales mapped ontovalues as a smooth gradient. Designed for use withscale_fill_ramp_continuous()andscale_colour_ramp_continuous(). Based onguide_colourbar().
Usage
guide_rampbar( ..., to = "gray65", available_aes = c("fill_ramp", "colour_ramp"))Arguments
... | Arguments passed on to
|
to | <string> The color to ramp to in the guide. Corresponds to |
available_aes | <character> Vector listing the aesthetics for which a |
Details
This guide creates smooth gradient color bars for use withscale_fill_ramp_continuous()andscale_colour_ramp_continuous(). The color to ramp from is determined by thefromargument of thescale_* function, and the color to ramp to is determined by theto argumenttoguide_rampbar().
Guides can be specified in eachscale_* function or inguides().guide = "rampbar" inscale_* is syntactic sugar forguide = guide_rampbar();e.g.scale_colour_ramp_continuous(guide = "rampbar"). For how to specifythe guide for each scale in more detail, seeguides().
Value
A guide object.
Author(s)
Matthew Kay
See Also
Other colour ramp functions:partial_colour_ramp(),ramp_colours(),scale_colour_ramp
Examples
library(dplyr)library(ggplot2)library(distributional)# The default guide for ramp scales is guide_legend(), which creates a# discrete style scale:tibble(d = dist_uniform(0, 1)) %>% ggplot(aes(y = 0, xdist = d)) + stat_slab(aes(fill_ramp = after_stat(x)), fill = "blue") + scale_fill_ramp_continuous(from = "red")# We can use guide_rampbar() to instead create a continuous guide, but# it does not know what color to ramp to (defaults to "gray65"):tibble(d = dist_uniform(0, 1)) %>% ggplot(aes(y = 0, xdist = d)) + stat_slab(aes(fill_ramp = after_stat(x)), fill = "blue") + scale_fill_ramp_continuous(from = "red", guide = guide_rampbar())# We can tell the guide what color to ramp to using the `to` argument:tibble(d = dist_uniform(0, 1)) %>% ggplot(aes(y = 0, xdist = d)) + stat_slab(aes(fill_ramp = after_stat(x)), fill = "blue") + scale_fill_ramp_continuous(from = "red", guide = guide_rampbar(to = "blue"))Nicely-spaced sets of interval widths
Description
Create nicely-spaced sets of nested interval widths for use with (e.g.)the.width parameter ofpoint_interval(),stat_slabinterval(), orstat_lineribbon():
interval_widths(n)creates a sequence ofninterval widthsp_1 \ldots p_n, where0 < p_i \le \textrm{max} < 1, correspondingto the masses of nested intervals that are evenly-spaced on a referencedistribution (by default a Normal distribution). This generalizes the ideabehind the default ~66% and 95% intervals instat_slabinterval()and50%, 80%, and 95% intervals instat_lineribbon(): when applied to a Normaldistribution, those intervals are roughly evenly-spaced and allow one tosee deviations from the reference distribution (such as excess kurtosis) whenthe resulting intervals arenot evenly spaced.pretty_widths(n)is a variant ofinterval_widths()with defaults formaxandprecisionthat make the resulting intervals more human-readable,for labeling purposes.
Intervals should be evenly-spaced on any symmetric reference distributionwhen applied to data from distributions with the same shape. Ifdistis not symmetric, intervals may only be approximately evenly-spaced above themedian.
Usage
interval_widths(n, dist = dist_normal(), max = 1 - 0.1/n, precision = NULL)pretty_widths( n, dist = dist_normal(), max = if (n <= 4) 0.95 else 1 - 0.1/n, precision = if (n <= 4) 0.05 else 0.01)Arguments
n | <numeric> in |
dist | <distribution>: Referencedistribution. |
max | <numeric> in |
precision | <numeric |NULL>: If not |
Details
Given the cumulative distribution functionF_\textrm{dist}(q)and the quantile functionF^{-1}_\textrm{dist}(p) ofdist, thefollowing is a sequence ofn + 1 evenly-spaced quantiles ofdistthat could represent upper limits of nested intervals, whereq_i = q_0 + i\frac{q_n - q_0}{n}:
\begin{array}{rcl}q_0, \ldots, q_n &=& F^{-1}_\textrm{dist}(0.5), \ldots, F^{-1}_\textrm{dist}(0.5 + \frac{\textrm{max}}{2})\end{array}
interval_widths(n) returns then interval widths corresponding to theupper interval limitsq_1, \ldots, q_n:
2\cdot\left[F_\textrm{dist}(q_1) - 0.5\right], \ldots, 2\cdot\left[F_\textrm{dist}(q_n) - 0.5\right]
Value
A length-n numeric vector of interval widths (masses) between0 and1 (exclusive) in increasing order.
See Also
The.width argument topoint_interval(),stat_slabinterval(),stat_lineribbon(), etc.
Examples
library(ggplot2)library(distributional)interval_widths(1) # 0.9# this is roughly +/- 1 SD and +/- 2 SDinterval_widths(2) # 0.672..., 0.95interval_widths(3) # 0.521..., 0.844..., 0.966...# "pretty" widths may be useful for legends with a small number of widthspretty_widths(1) # 0.95pretty_widths(2) # 0.65, 0.95pretty_widths(3) # 0.50, 0.80, 0.95# larger numbers of intervals can be useful for plotsggplot(data.frame(x = 1:20/20)) + aes(x, ydist = dist_normal((x * 5)^2, 1 + x * 5)) + stat_lineribbon(.width = pretty_widths(10))# large numbers of intervals can be used to create gradients -- particularly# useful if you shade ribbons according to density (not interval width)# (this is currently experimental)withr::with_options(list(ggdist.experimental.slab_data_in_intervals = TRUE), print( ggplot(data.frame(x = 1:20/20)) + aes(x, ydist = dist_normal((x * 5)^2, 1 + x * 5)) + stat_lineribbon( aes(fill_ramp = after_stat(ave(pdf_min, level))), .width = interval_widths(40), fill = "gray50" ) + theme_ggdist()))Marginal distribution of a single correlation from an LKJ distribution
Description
Marginal distribution for the correlation in a single cell from a correlationmatrix distributed according to an LKJ distribution.
Usage
dlkjcorr_marginal(x, K, eta, log = FALSE)plkjcorr_marginal(q, K, eta, lower.tail = TRUE, log.p = FALSE)qlkjcorr_marginal(p, K, eta, lower.tail = TRUE, log.p = FALSE)rlkjcorr_marginal(n, K, eta)Arguments
x,q | vector of quantiles. |
K | <numeric> Dimension of the correlation matrix. Must be greater than or equal to 2. |
eta | <numeric> Parameter controlling the shape of the distribution |
log,log.p | logical; if TRUE, probabilities p are given as log(p). |
lower.tail | logical; if TRUE (default), probabilities are |
p | vector of probabilities. |
n | number of observations. If |
Details
The LKJ distribution is a distribution over correlation matrices with a single parameter,\eta.For a given\eta and aK \times K correlation matrixR:
R \sim \textrm{LKJ}(\eta)
Each off-diagonal entry ofR,r_{ij}: i \ne j, has thefollowing marginal distribution (Lewandowski, Kurowicka, and Joe 2009):
\frac{r_{ij} + 1}{2} \sim \textrm{Beta}\left(\eta - 1 + \frac{K}{2}, \eta - 1 + \frac{K}{2}\right)
In other words,r_{ij} is marginally distributed according to the above Betadistribution scaled into(-1,1).
Value
dlkjcorr_marginalgives the densityplkjcorr_marginalgives the cumulative distribution function (CDF)qlkjcorr_marginalgives the quantile function (inverse CDF)rlkjcorr_marginalgenerates random draws.
The length of the result is determined byn forrlkjcorr_marginal, and is the maximum of the lengths ofthe numerical arguments for the other functions.
The numerical arguments other thann are recycled to the length of the result. Only the first elementsof the logical arguments are used.
References
Lewandowski, D., Kurowicka, D., & Joe, H. (2009). Generating random correlation matrices based on vinesand extended onion method.Journal of Multivariate Analysis, 100(9), 1989–2001.doi:10.1016/j.jmva.2009.04.008.
See Also
parse_dist() andmarginalize_lkjcorr() for parsing specs that use theLKJ correlation distribution and thestat_slabinterval() family of stats for visualizing them.
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())expand.grid( eta = 1:6, K = 2:6) %>% ggplot(aes(y = ordered(eta), dist = "lkjcorr_marginal", arg1 = K, arg2 = eta)) + stat_slab() + facet_grid(~ paste0(K, "x", K)) + scale_y_discrete(limits = rev) + labs( title = paste0( "Marginal correlation for LKJ(eta) prior on different matrix sizes:\n", "dlkjcorr_marginal(K, eta)" ), subtitle = "Correlation matrix size (KxK)", y = "eta", x = "Marginal correlation" ) + theme(axis.title = element_text(hjust = 0))Turn spec for LKJ distribution into spec for marginal LKJ distribution
Description
Turns specs for an LKJ correlation matrix distribution as returned byparse_dist() into specs for the marginal distribution ofa single cell in an LKJ-distributed correlation matrix (i.e.,lkjcorr_marginal()).Useful for visualizing prior correlations from LKJ distributions.
Usage
marginalize_lkjcorr( data, K, predicate = NULL, dist = ".dist", args = ".args", dist_obj = ".dist_obj")Arguments
data | <data.frame> A data frame containing a column with distribution names ( |
K | <numeric> Dimension of the correlation matrix. Must be greater than or equal to 2. |
predicate | <barelanguage |NULL> Expression for selecting the rows of If |
dist | <string> The name of the column containing distribution names. See |
args | <string> The name of the column containing distribution arguments. See |
dist_obj | <string> The name of the output column to contain adistributionalobject representing the distribution. See |
Details
The LKJ(eta) prior on a correlation matrix induces a marginal prior on each correlationin the matrix that depends on both the value ofetaandK, the dimensionof theK \times K correlation matrix. Thus to visualize the marginal prioron the correlations, it is necessary to specify the value ofK, which dependson what your model specification looks like.
Given a data frame representing parsed distribution specifications (suchas returned byparse_dist()), this function updates any rows with.dist == "lkjcorr"so that the first argument to the distribution (stored in.args) is equal to the specified dimensionof the correlation matrix (K), changes the distribution name in.dist to"lkjcorr_marginal",and assigns adistributional object representing this distribution to.dist_obj.This allows the distribution to be easily visualized using thestat_slabinterval()family of ggplot2 stats.
Value
A data frame of the same size and column names as the input, with thedist, andargs,anddist_obj columns modified on rows wheredist == "lkjcorr" such that they represent amarginal LKJ correlation distribution with namelkjcorr_marginal andargs havingK equal to the input value ofK.
See Also
parse_dist(),lkjcorr_marginal()
Examples
library(dplyr)library(ggplot2)# Say we have an LKJ(3) prior on a 2x2 correlation matrix. We can visualize# its marginal distribution as follows...data.frame(prior = "lkjcorr(3)") %>% parse_dist(prior) %>% marginalize_lkjcorr(K = 2) %>% ggplot(aes(y = prior, xdist = .dist_obj)) + stat_halfeye() + xlim(-1, 1) + xlab("Marginal correlation for LKJ(3) prior on 2x2 correlation matrix")# Say our prior list has multiple LKJ priors on correlation matrices# of different sizes, we can supply a predicate expression to select# only those rows we want to modifydata.frame(coef = c("a", "b"), prior = "lkjcorr(3)") %>% parse_dist(prior) %>% marginalize_lkjcorr(K = 2, coef == "a") %>% marginalize_lkjcorr(K = 4, coef == "b")Parse distribution specifications into columns of a data frame
Description
Parses simple string distribution specifications, like"normal(0, 1)", into two columns ofa data frame, suitable for use with thedist andargs aesthetics ofstat_slabinterval()and its shortcut stats (likestat_halfeye()). This format is outputbybrms::get_prior, making it particularly useful for visualizing priors frombrms models.
Usage
parse_dist( object, ..., dist = ".dist", args = ".args", dist_obj = ".dist_obj", package = NULL, to_r_names = TRUE)## Default S3 method:parse_dist(object, ...)## S3 method for class 'data.frame'parse_dist( object, dist_col, ..., dist = ".dist", args = ".args", dist_obj = ".dist_obj", package = NULL, lb = "lb", ub = "ub", to_r_names = TRUE)## S3 method for class 'character'parse_dist( object, ..., dist = ".dist", args = ".args", dist_obj = ".dist_obj", package = NULL, to_r_names = TRUE)## S3 method for class 'factor'parse_dist( object, ..., dist = ".dist", args = ".args", dist_obj = ".dist_obj", package = NULL, to_r_names = TRUE)## S3 method for class 'brmsprior'parse_dist( object, dist_col = prior, ..., dist = ".dist", args = ".args", dist_obj = ".dist_obj", package = NULL, to_r_names = TRUE)r_dist_name(dist_name)Arguments
object | <character |data.frame> One of:
|
... | Arguments passed to other implementations of |
dist | <string> The name of the output column to contain the distribution name. |
args | <string> The name of the output column to contain the arguments to the distribution. |
dist_obj | <string> The name of the output column to contain adistributionalobject representing the distribution. |
package | <string |environment |NULL> The package or environment to search fordistribution functions in. Passed to
|
to_r_names | <scalarlogical> If |
dist_col | <barelanguage> Column or column expression of |
lb | <string> The name of an input column (for |
ub | <string> The name of an input column (for |
dist_name | <character> For |
Details
parse_dist() can be applied to character vectors or to a data frame + bare column name of thecolumn to parse, and returns a data frame with".dist" and".args" columns added.parse_dist() usesr_dist_name() to translate distribution names into names recognizedby R.
r_dist_name() takes a character vector of names and translates common names into Rdistribution names. Names are first made into valid R names usingmake.names(),then translated (ignoring character case,".", and"_"). Thus,"lognormal","LogNormal","log_normal","log-Normal", and any number of other variantsall get translated into"lnorm".
Value
parse_distreturns a data frame containing at least two columns named after thedistandargsparameters. If the input is a data frame, the output is a data frame of the same length with thosetwo columns added. If the input is a character vector or factor, the output is a two-column data framewith the same number of rows as the length of the input.r_dist_namereturns a character vector the same length as the input containing translations of theinput names into distribution names R can recognize.
See Also
Seestat_slabinterval() and its shortcut stats, which can easily make use ofthe output of this function using thedist andargs aesthetics.
Examples
library(dplyr)# parse dist can operate on strings directly...parse_dist(c("normal(0,1)", "student_t(3,0,1)"))# ... or on columns of a data frame, where it adds the# parsed specs back on as columnsdata.frame(prior = c("normal(0,1)", "student_t(3,0,1)")) %>% parse_dist(prior)# parse_dist is particularly useful with the output of brms::prior(),# which follows the same format as abovePartial colour ramp (datatype)
Description
A representation of a partial ramp between two colours: the origin colour(from) and the distance from the origin colour to the target colour(amount, a value between0 and1). The target colour of the rampcan be filled in later usingramp_colours(), producing a colour.
Usage
partial_colour_ramp(amount = double(), from = "white")Arguments
amount | <numeric> Vector of values between |
from | <character> Vector giving colours to ramp from. |
Details
This datatype is used byscale_colour_ramp to create ramped colours inggdist geoms. It is avctrs::rcrd datatype with two fields:"amount", the amount to ramp, and"from", the colour to ramp from.
Colour ramps can be applied (i.e. translated into colours) usingramp_colours(), which can be used withpartial_colour_ramp()to implement geoms that make use ofcolour_ramp orfill_ramp scales.
Value
Avctrs::rcrd of class"ggdist_partial_colour_ramp" with fields"amount" and"from".
Author(s)
Matthew Kay
See Also
Other colour ramp functions:guide_rampbar(),ramp_colours(),scale_colour_ramp
Examples
pcr = partial_colour_ramp(c(0, 0.25, 0.75, 1), "red")pcrramp_colours("blue", pcr)Point and interval summaries for tidy data frames of draws from distributions
Description
Translates draws from distributions in a (possibly grouped) data frame into point andinterval summaries (or set of point and interval summaries, if there aremultiple groups in a grouped data frame).
Supportsautomatic partial function application.
Usage
point_interval( .data, ..., .width = 0.95, .point = median, .interval = qi, .simple_names = TRUE, na.rm = FALSE, .exclude = c(".chain", ".iteration", ".draw", ".row"), .prob)## Default S3 method:point_interval( .data, ..., .width = 0.95, .point = median, .interval = qi, .simple_names = TRUE, na.rm = FALSE, .exclude = c(".chain", ".iteration", ".draw", ".row"), .prob)## S3 method for class 'tbl_df'point_interval(.data, ...)## S3 method for class 'numeric'point_interval( .data, ..., .width = 0.95, .point = median, .interval = qi, .simple_names = FALSE, na.rm = FALSE, .exclude = c(".chain", ".iteration", ".draw", ".row"), .prob)## S3 method for class 'rvar'point_interval( .data, ..., .width = 0.95, .point = median, .interval = qi, .simple_names = TRUE, na.rm = FALSE)## S3 method for class 'distribution'point_interval( .data, ..., .width = 0.95, .point = median, .interval = qi, .simple_names = TRUE, na.rm = FALSE)qi(x, .width = 0.95, .prob, na.rm = FALSE)ll(x, .width = 0.95, na.rm = FALSE)ul(x, .width = 0.95, na.rm = FALSE)hdi( x, .width = 0.95, na.rm = FALSE, ..., density = density_bounded(trim = TRUE), n = 4096, .prob)Mode(x, na.rm = FALSE, ...)## Default S3 method:Mode( x, na.rm = FALSE, ..., density = density_bounded(trim = TRUE), n = 2001, weights = NULL)## S3 method for class 'rvar'Mode(x, na.rm = FALSE, ...)## S3 method for class 'distribution'Mode(x, na.rm = FALSE, ...)hdci(x, .width = 0.95, na.rm = FALSE)mean_qi(.data, ..., .width = 0.95)median_qi(.data, ..., .width = 0.95)mode_qi(.data, ..., .width = 0.95)mean_ll(.data, ..., .width = 0.95)median_ll(.data, ..., .width = 0.95)mode_ll(.data, ..., .width = 0.95)mean_ul(.data, ..., .width = 0.95)median_ul(.data, ..., .width = 0.95)mode_ul(.data, ..., .width = 0.95)mean_hdi(.data, ..., .width = 0.95)median_hdi(.data, ..., .width = 0.95)mode_hdi(.data, ..., .width = 0.95)mean_hdci(.data, ..., .width = 0.95)median_hdci(.data, ..., .width = 0.95)mode_hdci(.data, ..., .width = 0.95)Arguments
.data | <data.frame |grouped_df> Data frame (or groupeddata frame as returned by |
... | <barelanguage> Column names or expressions that, when evaluated in the context of |
.width | <numeric> vector of probabilities to use that determine the widths ofthe resulting intervals. If multiple probabilities are provided, multiple rows pergroup are generated, each with a different probability interval (and value of thecorresponding |
.point | <function> Point summary function, which takes a vector and returns a singlevalue, e.g. |
.interval | <function> Interval function, which takes a vector and a probability( |
.simple_names | <scalarlogical> When |
na.rm | <scalarlogical> Should |
.exclude | <character> Vector of names of columns to be excluded from summarizationif no column names are specified to be summarized in |
.prob | Deprecated. Use |
x | <numeric> Vector to summarize (for interval functions: |
density | <function |string> For |
n | <scalarnumeric> For |
weights | <numeric |NULL> For |
Details
If.data is a data frame, then... is a list of bare names ofcolumns (or expressions derived from columns) of.data, on whichthe point and interval summaries are derived. Column expressions are processedusing the tidy evaluation framework (seerlang::eval_tidy()).
For a column namedx, the resulting data frame will have a columnnamedx containing its point summary. If there is a singlecolumn to be summarized and.simple_names isTRUE, the output willalso contain columns.lower (the lower end of the interval),.upper (the upper end of the interval).Otherwise, for every summarized columnx, the output will containx.lower (the lower end of the interval) andx.upper (the upperend of the interval). Finally, the output will have a.width columncontaining the' probability for the interval on each output row.
If.data includes groups (see e.g.dplyr::group_by()),the points and intervals are calculated within the groups.
If.data is a vector,... is ignored and the result is adata frame with one row per value of.width and three columns:y (the point summary),ymin (the lower end of the interval),ymax (the upper end of the interval), and.width, the probabilitycorresponding to the interval. This behavior allowspoint_intervaland its derived functions (likemedian_qi,mean_qi,mode_hdi, etc)to be easily used to plot intervals in ggplot stats using methods likestat_eye(),stat_halfeye(), orstat_summary().
median_qi,mode_hdi, etc are short forms forpoint_interval(..., .point = median, .interval = qi), etc.
qi yields the quantile interval (also known as the percentile interval orequi-tailed interval) as a 1x2 matrix.
hdi yields the highest-density interval(s) (also known as the highest posteriordensity interval).Note: If the distribution is multimodal,hdi may return multipleintervals for each probability level (these will be spread over rows). You may wish to usehdci (below) instead if you want a single highest-density interval, with the caveat that whenthe distribution is multimodalhdci is not a highest-density interval.
hdci yields the highest-densitycontinuous interval, also known as the shortestprobability interval.Note: If the distribution is multimodal, this may not actuallybe the highest-density interval (there may be a higher-densitydiscontinuous interval, which can be found usinghdi).
ll andul yield lower limits and upper limits, respectively (where the oppositelimit is set to eitherInf or-Inf).
Value
A data frame containing point summaries and intervals, with at least one column correspondingto the point summary, one to the lower end of the interval, one to the upper end of the interval, thewidth of the interval (.width), the type of point summary (.point), and the type of interval (.interval).
Author(s)
Matthew Kay
Examples
library(dplyr)library(ggplot2)set.seed(123)rnorm(1000) %>% median_qi()data.frame(x = rnorm(1000)) %>% median_qi(x, .width = c(.50, .80, .95))data.frame( x = rnorm(1000), y = rnorm(1000, mean = 2, sd = 2) ) %>% median_qi(x, y)data.frame( x = rnorm(1000), group = "a" ) %>% rbind(data.frame( x = rnorm(1000, mean = 2, sd = 2), group = "b") ) %>% group_by(group) %>% median_qi(.width = c(.50, .80, .95))multimodal_draws = data.frame( x = c(rnorm(5000, 0, 1), rnorm(2500, 4, 1)) )multimodal_draws %>% mode_hdi(.width = c(.66, .95))multimodal_draws %>% ggplot(aes(x = x, y = 0)) + stat_halfeye(point_interval = mode_hdi, .width = c(.66, .95))Dodge overlapping objects side-to-side, preserving justification
Description
A justification-preserving variant ofggplot2::position_dodge() which preserves thevertical position of a geom while adjusting the horizontal position (or viceversa when in a horizontal orientation). Unlikeggplot2::position_dodge(),position_dodgejust() attempts to preserve the "justification" ofxpositions relative to the bounds containing them (xmin/xmax) (orypositions relative toymin/ymax when in a horizontal orientation). Thismakes it useful for dodging annotations to geoms and stats from thegeom_slabinterval() family, which also preserve the justification of theirintervals relative to their slabs when dodging.
Usage
position_dodgejust( width = NULL, preserve = c("total", "single"), justification = NULL)Arguments
width | Dodging width, when different to the width of the individualelements. This is useful when you want to align narrow geoms with widergeoms. See the examples. |
preserve | Should dodging preserve the |
justification | <scalarnumeric> Justification of the point position ( |
Examples
library(dplyr)library(ggplot2)library(distributional)dist_df = tribble( ~group, ~subgroup, ~mean, ~sd, 1, "h", 5, 1, 2, "h", 7, 1.5, 3, "h", 8, 1, 3, "i", 9, 1, 3, "j", 7, 1)# An example with normal "dodge" positioning# Notice how dodge points are placed in the center of their bounding boxes,# which can cause slabs to be positioned outside their bounds.dist_df %>% ggplot(aes( x = factor(group), ydist = dist_normal(mean, sd), fill = subgroup )) + stat_halfeye( position = "dodge" ) + geom_rect( aes(xmin = group, xmax = group + 1, ymin = 2, ymax = 13, color = subgroup), position = "dodge", data = . %>% filter(group == 3), alpha = 0.1 ) + geom_point( aes(x = group, y = 7.5, color = subgroup), position = position_dodge(width = 1), data = . %>% filter(group == 3), shape = 1, size = 4, stroke = 1.5 ) + scale_fill_brewer(palette = "Set2") + scale_color_brewer(palette = "Dark2")# This same example with "dodgejust" positioning. For the points we# supply a justification parameter to position_dodgejust which mimics the# justification parameter of stat_halfeye, ensuring that they are# placed appropriately. On slabinterval family geoms, position_dodgejust()# will automatically detect the appropriate justification.dist_df %>% ggplot(aes( x = factor(group), ydist = dist_normal(mean, sd), fill = subgroup )) + stat_halfeye( position = "dodgejust" ) + geom_rect( aes(xmin = group, xmax = group + 1, ymin = 2, ymax = 13, color = subgroup), position = "dodgejust", data = . %>% filter(group == 3), alpha = 0.1 ) + geom_point( aes(x = group, y = 7.5, color = subgroup), position = position_dodgejust(width = 1, justification = 0), data = . %>% filter(group == 3), shape = 1, size = 4, stroke = 1.5 ) + scale_fill_brewer(palette = "Set2") + scale_color_brewer(palette = "Dark2")Apply partial colour ramps
Description
Given vectors of colours andpartial_colour_ramps, ramps the coloursaccording to the parameters of the partial colour ramps, returninga vector of the same length as the inputs giving the transformed(ramped) colours.
Usage
ramp_colours(colour, ramp)Arguments
colour | <character> Vector of colours to ramp to. |
ramp | <partial_colour_ramp> Vector of colour ramps (same length as |
Details
Takes vectors of colours andpartial_colour_ramps and producescolours by interpolating between eachfrom colour and the targetcolourthe specifiedamount (whereamount andfrom are the correspondingfields of theramp).
For example, to add support for thefill_ramp aesthetic to a geometry,this line could be used inside thedraw_group() ordraw_panel() methodof a geom:
data$fill = ramp_colours(data$fill, data$fill_ramp)
Value
A character vector of colours.
Author(s)
Matthew Kay
See Also
Other colour ramp functions:guide_rampbar(),partial_colour_ramp(),scale_colour_ramp
Examples
pcr = partial_colour_ramp(c(0, 0.25, 0.75, 1), "red")pcrramp_colours("blue", pcr)Secondary color scale that ramps from another color (ggplot2 scale)
Description
This scale creates a secondary scale that modifies thefill orcolor scale ofgeoms that support it (geom_lineribbon() andgeom_slabinterval()) to "ramp"from a secondary color (by default white) to the primary fill color (determinedby the standardcolor orfill aesthetics). It uses thepartial_colour_ramp() data type.
Usage
scale_colour_ramp_continuous( from = "white", ..., limits = function(l) c(min(0, l[[1]]), l[[2]]), range = c(0, 1), guide = "legend", aesthetics = "colour_ramp")scale_color_ramp_continuous( from = "white", ..., limits = function(l) c(min(0, l[[1]]), l[[2]]), range = c(0, 1), guide = "legend", aesthetics = "colour_ramp")scale_colour_ramp_discrete( from = "white", ..., range = c(0.2, 1), aesthetics = "colour_ramp")scale_color_ramp_discrete( from = "white", ..., range = c(0.2, 1), aesthetics = "colour_ramp")scale_fill_ramp_continuous(..., aesthetics = "fill_ramp")scale_fill_ramp_discrete(..., aesthetics = "fill_ramp")Arguments
from | <string> The color to ramp from. Corresponds to |
... | Arguments passed to underlying scale or guide functions. E.g. |
limits | One of:
|
range | <length-2numeric> Minimum and maximumvalues after the scale transformation. These values should be between |
guide | <Guide |string> A function used to create a guide or its name. For |
aesthetics | <character> Names of aesthetics to set scales for. |
Details
These scales transform data intopartial_colour_ramps. Eachpartial_colour_rampis a pair of two values: afrom colour and a numericamount between0and1 representing a distance betweenfrom and the target color (where0indicates thefrom color and1 the target color).
The target color is determined by the corresponding aesthetic: for example,thecolour_ramp aesthetic creates ramps betweenfrom and whatever thevalue of thecolour aesthetic is; thefill_ramp aesthetic creates rampsbetweenfrom and whatever the value of thefill aesthetic is. When thecolour_ramp aesthetic is set,ggdist geometries will modify theircolour by applying the colour ramp betweenfrom andcolour (andsimilarly forfill_ramp andfill).
Colour ramps can be applied (i.e. translated into colours) usingramp_colours(), which can be used withpartial_colour_ramp()to implement geoms that make use ofcolour_ramp orfill_ramp scales.
Value
Aggplot2::Scale representing a scale for thecolour_ramp and/orfill_rampaesthetics forggdist geoms. Can be added to aggplot() object.
Author(s)
Matthew Kay
See Also
Other ggdist scales:scale_side_mirrored(),scale_thickness,sub-geometry-scales
Other colour ramp functions:guide_rampbar(),partial_colour_ramp(),ramp_colours()
Examples
library(dplyr)library(ggplot2)library(distributional)tibble(d = dist_uniform(0, 1)) %>% ggplot(aes(y = 0, xdist = d)) + stat_slab(aes(fill_ramp = after_stat(x)))tibble(d = dist_uniform(0, 1)) %>% ggplot(aes(y = 0, xdist = d)) + stat_slab(aes(fill_ramp = after_stat(x)), fill = "blue") + scale_fill_ramp_continuous(from = "red")# you can invert the order of `range` to change the order of the blendtibble(d = dist_normal(0, 1)) %>% ggplot(aes(y = 0, xdist = d)) + stat_slab(aes(fill_ramp = after_stat(cut_cdf_qi(cdf))), fill = "blue") + scale_fill_ramp_discrete(from = "red", range = c(1, 0))Side scale for mirrored slabs (ggplot2 scale)
Description
This scale creates mirrored slabs for theside aesthetic of thegeom_slabinterval()andgeom_dotsinterval() family of geoms and stats. It works on discrete variablesof two or three levels.
Usage
scale_side_mirrored(start = "topright", ..., aesthetics = "side")Arguments
start | <string> The side to start from. Can be any valid value of the |
... | Arguments passed on to
|
aesthetics | <character> Names of aesthetics to set scales for. |
Value
Aggplot2::Scale representing a scale for thesideaesthetic forggdist geoms. Can be added to aggplot() object.
Author(s)
Matthew Kay
See Also
Other ggdist scales:scale_colour_ramp,scale_thickness,sub-geometry-scales
Examples
library(dplyr)library(ggplot2)set.seed(1234)data.frame( x = rnorm(400, c(1,4)), g = c("a","b")) %>% ggplot(aes(x, fill = g, side = g)) + geom_weave(linewidth = 0, scale = 0.5) + scale_side_mirrored()Slab thickness scale (ggplot2 scale)
Description
Thisggplot2 scale linearly scales allthickness values of geomsthat support thethickness aesthetic (such asgeom_slabinterval()). Itcan be used to align thethickness scales across multiple geoms (by default,thickness is normalized on a per-geom level instead of as a global scale).For a comprehensive discussion and examples of slab scaling and normalization,see thethickness scale article.
Usage
scale_thickness_shared( name = waiver(), breaks = waiver(), labels = waiver(), limits = function(l) c(min(0, l[[1]]), l[[2]]), renormalize = FALSE, oob = scales::oob_keep, guide = "none", expand = c(0, 0), ...)scale_thickness_identity(..., guide = "none")Arguments
name | The name of the scale. Used as the axis or legend title. If |
breaks | One of:
|
labels | One of:
|
limits | One of:
|
renormalize | <scalarlogical> When mapping values to the |
oob | One of:
|
guide | A function used to create a guide or its name. See |
expand | <numeric> Vector of limit expansion constants of length2 or 4, following the same format used by the |
... | Arguments passed on to
|
Details
By default, normalization/scaling of slab thicknesses is controlled by geometries,not by aggplot2 scale function. This allows various functionality nototherwise possible, such as (1) allowing different geometries to have differentthickness scales and (2) allowing the user to control at what level of aggregation(panels, groups, the entire plot, etc) thickness scaling is done via thenormalizeparameter togeom_slabinterval().
However, this default approach has one drawback: two different geoms will alwayshave their own scaling ofthickness.scale_thickness_shared() offers analternative approach: when added to a chart, all geoms will use the samethickness scale, and geom-level normalization (via theirnormalize parameters)is ignored. This is achieved by "marking" thickness values as alreadynormalized by wrapping them in thethickness() data type (this can bedisabled by settingrenormalize = TRUE).
Note: while a slightly more typical name forscale_thickness_shared() mightbescale_thickness_continuous(), the latter name would cause this scaleto be applied to allthickness aesthetics by default according to the rulesggplot2 uses to find default scales. Thus, to retain the usual behaviorofstat_slabinterval() (per-geom normalization ofthickness), this scaleis calledscale_thickness_shared().
Value
Aggplot2::Scale representing a scale for thethicknessaesthetic forggdist geoms. Can be added to aggplot() object.
Author(s)
Matthew Kay
See Also
Thethickness datatype.
Thethickness aesthetic ofgeom_slabinterval().
subscale_thickness(), for setting athickness sub-scale withina singlegeom_slabinterval().
Other ggdist scales:scale_colour_ramp,scale_side_mirrored(),sub-geometry-scales
Examples
library(distributional)library(ggplot2)library(dplyr)prior_post = data.frame( prior = dist_normal(0, 1), posterior = dist_normal(0.1, 0.5))# By default, separate geoms have their own thickness scales, which means# distributions plotted using two separate geoms will not have their slab# functions drawn on the same scale (thus here, the two distributions have# different areas under their density curves):prior_post %>% ggplot() + stat_halfeye(aes(xdist = posterior)) + stat_slab(aes(xdist = prior), fill = NA, color = "red")# For this kind of prior/posterior chart, it makes more sense to have the# densities on the same scale; thus, the areas under both would be the same.# We can do that using scale_thickness_shared():prior_post %>% ggplot() + stat_halfeye(aes(xdist = posterior)) + stat_slab(aes(xdist = prior), fill = NA, color = "#e41a1c") + scale_thickness_shared()Smooth dot positions in a dotplot using a kernel density estimator ("density dotplots")
Description
Smoothsx values using a density estimator, returning newx of the samelength. Can be used with a dotplot (e.g.geom_dots(smooth = ...)) to create"density dotplots".
Supportsautomatic partial function application withwaived arguments.
Usage
smooth_bounded( x, density = "bounded", bounds = c(NA, NA), bounder = "cooke", trim = FALSE, ...)smooth_unbounded(x, density = "unbounded", trim = FALSE, ...)Arguments
x | <numeric> Values to smooth. |
density | <function |string> Density estimator to use for smoothing. One of:
|
bounds | <length-2numeric> Min and max bounds. If a bound is |
bounder | <function |string> Method to use to find missing(
|
trim | <scalarlogical> Passed to |
... | Arguments passed to the density estimator specified by |
Details
Applies a kernel density estimator (KDE) tox, then uses weighted quantilesof the KDE to generate a new set ofx values with smoothed values. Plottedusing a dotplot (e.g.geom_dots(smooth = "bounded") orgeom_dots(smooth = smooth_bounded(...)), these values create a variation ona "density dotplot" (Zvinca 2018).
Such plots are recommendedonly in verylarge sample sizes where precise positions of individual values are notparticularly meaningful. In small samples, normal dotplots should generallybe used.
Two variants are supplied by default:
smooth_bounded(), which usesdensity_bounded().Passes theboundsarguments to the estimator.smooth_unbounded(), which usesdensity_unbounded().
It is generally recommended to pick the smooth based on the known bounds ofyour data, e.g. by usingsmooth_bounded() with thebounds parameter ifthere are finite bounds, orsmooth_unbounded() if both bounds are infinite.
Value
A numeric vector oflength(x), where each entry is a smoothed version ofthe corresponding entry inx.
Ifx is missing, returns a partial application of itself. Seeautomatic-partial-functions.
References
Zvinca, Daniel. "In the pursuit of diversity in data visualization. Jittering data to access details."https://www.linkedin.com/pulse/pursuit-diversity-data-visualization-jittering-access-daniel-zvinca/.
See Also
Other dotplot smooths:smooth_discrete(),smooth_none()
Examples
library(ggplot2)set.seed(1234)x = rnorm(1000)# basic dotplot is noisyggplot(data.frame(x), aes(x)) + geom_dots()# density dotplot is smoother, but does move points (most noticeable# in areas of low density)ggplot(data.frame(x), aes(x)) + geom_dots(smooth = "unbounded")# you can adjust the kernel and bandwidth...ggplot(data.frame(x), aes(x)) + geom_dots(smooth = smooth_unbounded(kernel = "triangular", adjust = 0.5))# for bounded data, you should use the bounded smootherx_beta = rbeta(1000, 0.5, 0.5)ggplot(data.frame(x_beta), aes(x_beta)) + geom_dots(smooth = smooth_bounded(bounds = c(0, 1)))Smooth dot positions in a dotplot of discrete values ("bar dotplots")
Description
Note: Better-looking bar dotplots are typically easier to achieve usinglayout = "bar" with thegeom_dotsinterval() family instead ofsmooth = "bar" orsmooth = "discrete".
Smoothsx values wherex is presumed to be discrete, returning a newxof the same length. Bothsmooth_discrete() andsmooth_bar() use theresolution() of the data to apply smoothing around unique values in thedataset;smooth_discrete() uses a kernel density estimator andsmooth_bar()places values in an evenly-spaced grid. Can be used with a dotplot(e.g.geom_dots(smooth = ...)) to create "bar dotplots".
Supportsautomatic partial function application withwaived arguments.
Usage
smooth_discrete( x, kernel = c("rectangular", "gaussian", "epanechnikov", "triangular", "biweight", "cosine", "optcosine"), width = 0.7, ...)smooth_bar(x, width = 0.7, ...)Arguments
x | <numeric> Values to smooth. |
kernel | <string> The smoothing kernel to be used. This must partiallymatch one of |
width | <scalarnumeric> approximate width of the bars as a fractionof data |
... | additional parameters; |
Details
smooth_discrete() applies a kernel density estimator (default: rectangular)tox. It automatically sets the bandwidth to be such that the kernel'swidth (for each kernel type) is approximatelywidth times theresolution()of the data. This means it essentially creates smoothed bins around eachunique value. It calls down tosmooth_unbounded().
smooth_bar() generates an evenly-spaced grid of values spanning+/- width/2around each unique value inx.
Value
A numeric vector oflength(x), where each entry is a smoothed version ofthe corresponding entry inx.
Ifx is missing, returns a partial application of itself. Seeautomatic-partial-functions.
See Also
Other dotplot smooths:smooth_density,smooth_none()
Examples
library(ggplot2)set.seed(1234)x = rpois(1000, 2)# automatic binwidth in basic dotplot on large counts in discrete# distributions is very smallggplot(data.frame(x), aes(x)) + geom_dots()# NOTE: It is now recommended to use layout = "bar" instead of# smooth = "discrete" or smooth = "bar"; the latter are retained because# they can sometimes be useful in combination with other layouts for# more specialized (but finicky) applications.ggplot(data.frame(x), aes(x)) + geom_dots(layout = "bar")# smooth_discrete() constructs wider bins of dotsggplot(data.frame(x), aes(x)) + geom_dots(smooth = "discrete")# smooth_bar() is an alternative approach to rectangular layoutsggplot(data.frame(x), aes(x)) + geom_dots(smooth = "bar")# adjust the shape by changing the kernel or the width. epanechnikov# works well with side = "both"ggplot(data.frame(x), aes(x)) + geom_dots(smooth = smooth_discrete(kernel = "epanechnikov", width = 0.8), side = "both")Apply no smooth to a dotplot
Description
Default smooth for dotplots: no smooth. Simply returns the input values.
Supportsautomatic partial function application withwaived arguments.
Usage
smooth_none(x, ...)Arguments
x | <numeric> Values to smooth. |
... | ignored |
Details
This is the default value for thesmooth argument ofgeom_dotsinterval().
Value
x
Ifx is missing, returns a partial application of itself. Seeautomatic-partial-functions.
See Also
Other dotplot smooths:smooth_density,smooth_discrete()
CCDF bar plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_slabinterval() forcreating CCDF bar plots.
Roughly equivalent to:
stat_slabinterval( aes( thickness = after_stat(thickness(1 - cdf, 0, 1)), justification = after_stat(0.5), side = after_stat("topleft") ), normalize = "none", expand = TRUE)Usage
stat_ccdfinterval( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., normalize = "none", expand = TRUE, p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
normalize | <string> Groups within which to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a CCDF bar geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_ccdfinterval() + expand_limits(x = 0)# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_ccdfinterval() + expand_limits(x = 0)CDF bar plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_slabinterval() forcreating CDF bar plots.
Roughly equivalent to:
stat_slabinterval( aes( thickness = after_stat(thickness(cdf, 0, 1)), justification = after_stat(0.5), side = after_stat("topleft") ), normalize = "none", expand = TRUE)Usage
stat_cdfinterval( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., normalize = "none", expand = TRUE, p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
normalize | <string> Groups within which to scale values of the
For a comprehensive discussion and examples of slab scaling and normalization, see the |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a CDF bar geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_cdfinterval()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_cdfinterval()Dot plot (shortcut stat)
Description
A combination ofstat_slabinterval() andgeom_dotsinterval() with sensible defaultsfor making dot plots. Whilegeom_dotsinterval() is intended for use on dataframes that have already been summarized using apoint_interval() function,stat_dots() is intended for use directly on data frames of draws or ofanalytical distributions, and will perform the summarization using apoint_interval()function. Geoms based ongeom_dotsinterval() create dotplots that automatically determine a bin width thatensures the plot fits within the available space. They can also ensure dots do not overlap.
Roughly equivalent to:
stat_dotsinterval( aes(size = NULL), geom = "dots", show_point = FALSE, show_interval = FALSE, show.legend = NA)
Usage
stat_dots( mapping = NULL, data = NULL, geom = "dots", position = "identity", ..., quantiles = NA, orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
quantiles | <scalarlogical> Number of quantiles to plot in the dotplot. Use |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a dot geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_dots())the following aesthetics are supported by the underlying geom:
Dots-specific (aka Slab-specific) aesthetics
family: The font family used to draw the dots.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seegeom_dots() for the geom underlying this stat.Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval stats:stat_dotsinterval(),stat_mcse_dots()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(12345)tibble( x = rep(1:10, 100), y = rnorm(1000, x)) %>% ggplot(aes(x = x, y = y)) + stat_dots()# ON ANALYTICAL DISTRIBUTIONS# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticstibble( x = 1:10, sd = seq(1, 3, length.out = 10)) %>% ggplot(aes(x = x, ydist = dist_normal(x, sd))) + stat_dots(quantiles = 50)Dots + point + interval plot (shortcut stat)
Description
A combination ofstat_slabinterval() andgeom_dotsinterval() with sensible defaultsfor making dots + point + interval plots. Whilegeom_dotsinterval() is intended for use on dataframes that have already been summarized using apoint_interval() function,stat_dotsinterval() is intended for use directly on data frames of draws or ofanalytical distributions, and will perform the summarization using apoint_interval()function. Geoms based ongeom_dotsinterval() create dotplots that automatically determine a bin width thatensures the plot fits within the available space. They can also ensure dots do not overlap.
Usage
stat_dotsinterval( mapping = NULL, data = NULL, geom = "dotsinterval", position = "identity", ..., quantiles = NA, point_interval = "median_qi", .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
quantiles | <scalarlogical> Number of quantiles to plot in the dotplot. Use |
point_interval | <function |string> A function from the |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a dots + point + interval geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_dotsinterval())the following aesthetics are supported by the underlying geom:
Dots-specific (aka Slab-specific) aesthetics
family: The font family used to draw the dots.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seegeom_dotsinterval() for the geom underlying this stat.Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval stats:stat_dots(),stat_mcse_dots()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(12345)tibble( x = rep(1:10, 100), y = rnorm(1000, x)) %>% ggplot(aes(x = x, y = y)) + stat_dotsinterval()# ON ANALYTICAL DISTRIBUTIONS# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticstibble( x = 1:10, sd = seq(1, 3, length.out = 10)) %>% ggplot(aes(x = x, ydist = dist_normal(x, sd))) + stat_dotsinterval(quantiles = 50)Eye (violin + interval) plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_slabinterval() forcreating eye (violin + interval) plots.
Roughly equivalent to:
stat_slabinterval( aes(side = after_stat("both")))Usage
stat_eye( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a eye (violin + interval) geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_eye()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_eye()Gradient + interval plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_slabinterval() forcreating gradient + interval plots.
Roughly equivalent to:
stat_slabinterval( aes( justification = after_stat(0.5), thickness = after_stat(thickness(1)), slab_alpha = after_stat(f) ), fill_type = "auto", show.legend = c(size = FALSE, slab_alpha = FALSE))
If your graphics device supports it, it is recommended to use this statwithfill_type = "gradient" (see the description of that parameter). On R >= 4.2,support forfill_type = "gradient" should be auto-detected based on thegraphics device you are using.
Usage
stat_gradientinterval( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., fill_type = "auto", p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE, slab_alpha = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
fill_type | <string> What type of fill to use when the fill color or alpha varies within a slab.One of:
|
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a gradient + interval geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_gradientinterval()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_gradientinterval()Half-eye (density + interval) plot (shortcut stat)
Description
Equivalent tostat_slabinterval(), whose default settings create half-eye (density + interval) plots.
Usage
stat_halfeye( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a half-eye (density + interval) geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_halfeye()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_halfeye()Histogram + interval plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_slabinterval() forcreating histogram + interval plots.
Roughly equivalent to:
stat_slabinterval( density = "histogram")
Usage
stat_histinterval( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., density = "histogram", p_limits = c(NA, NA), adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
density | <function |string> Density estimator for sample data. One of:
|
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a histogram + interval geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_interval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_histinterval()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_histinterval()Multiple-interval plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_interval() forcreating multiple-interval plots.
Roughly equivalent to:
stat_slabinterval( aes( colour = after_stat(level), size = NULL ), geom = "interval", show_point = FALSE, .width = c(0.5, 0.8, 0.95), show_slab = FALSE, show.legend = NA)
Usage
stat_interval( mapping = NULL, data = NULL, geom = "interval", position = "identity", ..., .width = c(0.5, 0.8, 0.95), point_interval = "median_qi", orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
.width | <numeric> The |
point_interval | <function |string> A function from the |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a multiple-interval geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_interval())the following aesthetics are supported by the underlying geom:
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Deprecated aesthetics
interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_interval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_pointinterval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_interval() + scale_color_brewer()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_interval() + scale_color_brewer()Line + multiple-ribbon plot (shortcut stat)
Description
A combination ofstat_slabinterval() andgeom_lineribbon() with sensible defaultsfor making line + multiple-ribbon plots. Whilegeom_lineribbon() is intended for use on dataframes that have already been summarized using apoint_interval() function,stat_lineribbon() is intended for use directly on data frames of draws or ofanalytical distributions, and will perform the summarization using apoint_interval()function.
Roughly equivalent to:
stat_slabinterval( aes( group = after_stat(level), fill = after_stat(level), order = after_stat(level), size = NULL ), geom = "lineribbon", .width = c(0.5, 0.8, 0.95), show_slab = FALSE, show.legend = NA)
Usage
stat_lineribbon( mapping = NULL, data = NULL, geom = "lineribbon", position = "identity", ..., .width = c(0.5, 0.8, 0.95), point_interval = "median_qi", orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
.width | <numeric> The |
point_interval | <function |string> A function from the |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a line + multiple-ribbon geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.
Aesthetics
The line+ribbonstats andgeoms have a wide variety of aesthetics that controlthe appearance of their two sub-geometries: theline and theribbon.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_lineribbon())the following aesthetics are supported by the underlying geom:
Ribbon-specific aesthetics
xmin: Left edge of the ribbon sub-geometry (iforientation = "horizontal").xmax: Right edge of the ribbon sub-geometry (iforientation = "horizontal").ymin: Lower edge of the ribbon sub-geometry (iforientation = "vertical").ymax: Upper edge of the ribbon sub-geometry (iforientation = "vertical").order: The order in which ribbons are drawn. Ribbons with the smallest mean value oforderare drawn first (i.e., will be drawn below ribbons with larger mean values oforder). Iforderis not supplied togeom_lineribbon(),-abs(xmax - xmin)or-abs(ymax - ymax)(depending onorientation) is used, having the effect of drawing the widest (on average)ribbons on the bottom.stat_lineribbon()usesorder = after_stat(level)by default,causing the ribbons generated from the largest.widthto be drawn on the bottom.
Color aesthetics
colour: (orcolor) The color of theline sub-geometry.fill: The fill color of theribbon sub-geometry.alpha: The opacity of theline andribbon sub-geometries.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width ofline. Inggplot2 < 3.4, was calledsize.linetype: Type ofline (e.g.,"solid","dashed", etc)
Other aesthetics (these work as in standardgeoms)
group
See examples of some of these aesthetics in action invignette("lineribbon").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_lineribbon() for the geom underlying this stat.
Other lineribbon stats:stat_ribbon()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(12345)tibble( x = rep(1:10, 100), y = rnorm(1000, x)) %>% ggplot(aes(x = x, y = y)) + stat_lineribbon() + scale_fill_brewer()# ON ANALYTICAL DISTRIBUTIONS# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticstibble( x = 1:10, sd = seq(1, 3, length.out = 10)) %>% ggplot(aes(x = x, ydist = dist_normal(x, sd))) + stat_lineribbon() + scale_fill_brewer()Blurry MCSE dot plot (stat)
Description
Variant ofstat_dots() for creating blurry dotplots of quantiles. Usesposterior::mcse_quantile() to calculate the Monte Carlo Standard Errorof each quantile computed for the dotplot, yielding anse computed variablethat is by default mapped onto thesd aesthetic ofgeom_blur_dots().
Usage
stat_mcse_dots( mapping = NULL, data = NULL, geom = "blur_dots", position = "identity", ..., quantiles = NA, orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
quantiles | <scalarlogical> Number of quantiles to plot in the dotplot. Use |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | logical. Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
Thedots family of stats and geoms are similar toggplot2::geom_dotplot() but with a number of differences:
Dots geoms act like slabs in
geom_slabinterval()and can be given x positions (or y positions whenin a horizontal orientation).Given the available space to lay out dots, the dots geoms will automatically determine how many bins touse to fit the available space.
Dots geoms use a dynamic layout algorithm that lays out dots from the center out if the input data aresymmetrical, guaranteeing that symmetrical data results in a symmetrical plot. The layout algorithm also preventsdots from overlapping each other.
The shape of the dots in these geoms can be changed using the
slab_shapeaesthetic (when using thedotsintervalfamily) or theshapeorslab_shapeaesthetic (when using thedotsfamily)
Stats and geoms in this family include:
geom_dots(): dotplots on raw data. Ensures the dotplot fits within available space by reducing the sizeof the dots automatically (may result in very small dots).geom_swarm()andgeom_weave(): dotplots on raw data with defaults intended to create "beeswarm" plots.Usedside = "both"by default, and sets the default dot size to the same size asgeom_point()(binwidth = unit(1.5, "mm")), allowing dots to overlap instead of getting very small.stat_dots(): dotplots on raw data,distributional objects, andposterior::rvar()sgeom_dotsinterval(): dotplot + interval plots on raw data with already-calculatedintervals (rarely useful directly).stat_dotsinterval(): dotplot + interval plots on raw data,distributional objects,andposterior::rvar()s (will calculate intervals for you).geom_blur_dots(): blurry dotplots that allow the standard deviation of a blur applied toeach dot to be specified using thesdaesthetic.stat_mcse_dots(): blurry dotplots of quantiles using the Monte Carlo Standard Error of each quantile.
stat_dots() andstat_dotsinterval(), when used with thequantiles argument,are particularly useful for constructing quantile dotplots, which can be an effective way to communicate uncertaintyusing a frequency framing that may be easier for laypeople to understand (Kay et al. 2016, Fernandes et al. 2018).
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a blurry MCSE dot geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.se: For dots, the Monte Carlo Standard Error of the quantile corresponding to each dot.
Aesthetics
The dots+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: thedots (aka theslab), thepoint, and theinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_blur_dots())the following aesthetics are supported by the underlying geom:
Dots-specific (aka Slab-specific) aesthetics
sd: The standard deviation (in data units) of the blur associated with each dot.order: The order in which data points are stacked within bins. Can be used to create the effect of"stacked" dots by ordering dots according to a discrete variable. If omitted (NULL), thevalue of the data points themselves are used to determine stacking order. Only applies whenlayoutis"bin"or"hex", as the other layout methods fully determine bothx andy positions.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.slab_shape: Override forshape: the shape of the dots used to draw the dotplot slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("dotsinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
References
Kay, M., Kola, T., Hullman, J. R., & Munson, S. A. (2016). When (ish) is My Bus? User-centered Visualizationsof Uncertainty in Everyday, Mobile Predictive Systems.Conference on Human Factorsin Computing Systems - CHI '16, 5092–5103.doi:10.1145/2858036.2858558.
Fernandes, M., Walls, L., Munson, S., Hullman, J., & Kay, M. (2018). Uncertainty Displays Using Quantile Dotplotsor CDFs Improve Transit Decision-Making.Conference on Human Factors in Computing Systems - CHI '18.doi:10.1145/3173574.3173718.
See Also
Seegeom_blur_dots() for the geom underlying this stat.Seevignette("dotsinterval") for a variety of examples of use.
Other dotsinterval stats:stat_dots(),stat_dotsinterval()
Examples
library(dplyr)library(ggplot2)theme_set(theme_ggdist())set.seed(1234)data.frame(x = rnorm(1000)) %>% ggplot(aes(x = x)) + stat_mcse_dots(quantiles = 100, layout = "weave")Point + multiple-interval plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_pointinterval() forcreating point + multiple-interval plots.
Roughly equivalent to:
stat_slabinterval( geom = "pointinterval", show_slab = FALSE)
Usage
stat_pointinterval( mapping = NULL, data = NULL, geom = "pointinterval", position = "identity", ..., point_interval = "median_qi", .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
point_interval | <function |string> A function from the |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a point + multiple-interval geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_pointinterval())the following aesthetics are supported by the underlying geom:
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_pointinterval() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_slab(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_pointinterval()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_pointinterval()Multiple-ribbon plot (shortcut stat)
Description
A combination ofstat_slabinterval() andgeom_lineribbon() with sensible defaultsfor making multiple-ribbon plots. Whilegeom_lineribbon() is intended for use on dataframes that have already been summarized using apoint_interval() function,stat_ribbon() is intended for use directly on data frames of draws or ofanalytical distributions, and will perform the summarization using apoint_interval()function.
Roughly equivalent to:
stat_lineribbon( show_point = FALSE)
Usage
stat_ribbon( mapping = NULL, data = NULL, geom = "lineribbon", position = "identity", ..., .width = c(0.5, 0.8, 0.95), point_interval = "median_qi", orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
.width | <numeric> The |
point_interval | <function |string> A function from the |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a multiple-ribbon geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.
Aesthetics
The line+ribbonstats andgeoms have a wide variety of aesthetics that controlthe appearance of their two sub-geometries: theline and theribbon.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_lineribbon())the following aesthetics are supported by the underlying geom:
Ribbon-specific aesthetics
xmin: Left edge of the ribbon sub-geometry (iforientation = "horizontal").xmax: Right edge of the ribbon sub-geometry (iforientation = "horizontal").ymin: Lower edge of the ribbon sub-geometry (iforientation = "vertical").ymax: Upper edge of the ribbon sub-geometry (iforientation = "vertical").order: The order in which ribbons are drawn. Ribbons with the smallest mean value oforderare drawn first (i.e., will be drawn below ribbons with larger mean values oforder). Iforderis not supplied togeom_lineribbon(),-abs(xmax - xmin)or-abs(ymax - ymax)(depending onorientation) is used, having the effect of drawing the widest (on average)ribbons on the bottom.stat_lineribbon()usesorder = after_stat(level)by default,causing the ribbons generated from the largest.widthto be drawn on the bottom.
Color aesthetics
colour: (orcolor) The color of theline sub-geometry.fill: The fill color of theribbon sub-geometry.alpha: The opacity of theline andribbon sub-geometries.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Other aesthetics (these work as in standardgeoms)
group
See examples of some of these aesthetics in action invignette("lineribbon").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_lineribbon() for the geom underlying this stat.
Other lineribbon stats:stat_lineribbon()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(12345)tibble( x = rep(1:10, 100), y = rnorm(1000, x)) %>% ggplot(aes(x = x, y = y)) + stat_ribbon() + scale_fill_brewer()# ON ANALYTICAL DISTRIBUTIONS# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticstibble( x = 1:10, sd = seq(1, 3, length.out = 10)) %>% ggplot(aes(x = x, ydist = dist_normal(x, sd))) + stat_ribbon() + scale_fill_brewer()Slab (ridge) plot (shortcut stat)
Description
Shortcut version ofstat_slabinterval() withgeom_slab() forcreating slab (ridge) plots.
Roughly equivalent to:
stat_slabinterval( aes(size = NULL), geom = "slab", show_point = FALSE, show_interval = FALSE, show.legend = NA)
Usage
stat_slab( mapping = NULL, data = NULL, geom = "slab", position = "identity", ..., p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, limits = NULL, n = waiver(), orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to overridethe default connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a slab (ridge) geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slab())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Deprecated aesthetics
slab_size: Useslab_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slab() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_spike()
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c"), value = rnorm(1500, mean = c(5, 7, 9), sd = c(1, 1.5, 1)))df %>% ggplot(aes(x = value, y = group)) + stat_slab()# ON ANALYTICAL DISTRIBUTIONSdist_df = data.frame( group = c("a", "b", "c"), mean = c( 5, 7, 8), sd = c( 1, 1.5, 1))# Vectorized distribution types, like distributional::dist_normal()# and posterior::rvar(), can be used with the `xdist` / `ydist` aestheticsdist_df %>% ggplot(aes(y = group, xdist = dist_normal(mean, sd))) + stat_slab()# RIDGE PLOTS# "ridge" plots can be created by expanding the slabs to the limits of the plot# (expand = TRUE), allowing the density estimator to be nonzero outside the# limits of the data (trim = FALSE), and increasing the height of the slabs.data.frame( group = letters[1:3], value = rnorm(3000, 3:1)) %>% ggplot(aes(y = group, x = value)) + stat_slab(color = "black", expand = TRUE, trim = FALSE, height = 2)Slab + interval plots for sample data and analytical distributions (ggplot stat)
Description
"Meta" stat for computing distribution functions (densities or CDFs) + intervals for use withgeom_slabinterval(). Useful for creating eye plots, half-eye plots, CCDF bar plots,gradient plots, histograms, and more. Sample data can be supplied to thex andyaesthetics or analytical distributions (in a variety of formats) can be supplied to thexdist andydist aesthetics.SeeDetails.
Usage
stat_slabinterval( mapping = NULL, data = NULL, geom = "slabinterval", position = "identity", ..., p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, point_interval = "median_qi", limits = NULL, n = waiver(), .width = c(0.66, 0.95), orientation = NA, na.rm = FALSE, show.legend = c(size = FALSE), inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to override thedefault connection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
point_interval | <function |string> A function from the |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
.width | <numeric> The |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
A highly configurable stat for generating a variety of plots that combine a "slab"that describes a distribution plus a point summary and any number of intervals.Several "shortcut" stats are providedwhich combine multiple options to create useful geoms, particularlyeye plots(a violin plot of density plus interval),half-eye plots (a density plot plus interval),CCDF bar plots (a complementary CDF plus interval), andgradient plots(a density encoded in color alpha plus interval).
The shortcut stats include:
stat_eye(): Eye plots (violin + interval)stat_halfeye(): Half-eye plots (density + interval)stat_ccdfinterval(): CCDF bar plots (CCDF + interval)stat_cdfinterval(): CDF bar plots (CDF + interval)stat_gradientinterval(): Density gradient + interval plotsstat_slab(): Density plotsstat_histinterval(): Histogram + interval plotsstat_pointinterval(): Point + interval plotsstat_interval(): Interval plots
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a slab or combined slab+interval geometry which canbe added to aggplot() object.
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.
Aesthetics
The slab+intervalstats andgeoms have a wide variety of aesthetics that controlthe appearance of their three sub-geometries: theslab, thepoint, andtheinterval.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_slabinterval())the following aesthetics are supported by the underlying geom:
Slab-specific aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.justification: Justification of the interval relative to the slab, where0indicates bottom/leftjustification and1indicates top/right justification (depending onorientation). IfjustificationisNULL(the default), then it is set automatically based on the value ofside: whensideis"top"/"right"justificationis set to0, whensideis"bottom"/"left"justificationis set to1, and whensideis"both"justificationis set to 0.5.datatype: When using composite geoms directly without astat(e.g.geom_slabinterval()),datatypeis used toindicate which part of the geom a row in the data targets: rows withdatatype = "slab"target theslab portion of the geometry and rows withdatatype = "interval"target the interval portion ofthe geometry. This is set automatically when using ggdiststats.
Interval-specific aesthetics
xmin: Left end of the interval sub-geometry (iforientation = "horizontal").xmax: Right end of the interval sub-geometry (iforientation = "horizontal").ymin: Lower end of the interval sub-geometry (iforientation = "vertical").ymax: Upper end of the interval sub-geometry (iforientation = "vertical").
Point-specific aesthetics
shape: Shape type used to draw thepoint sub-geometry.
Color aesthetics
colour: (orcolor) The color of theinterval andpoint sub-geometries.Use theslab_color,interval_color, orpoint_coloraesthetics (below) toset sub-geometry colors separately.fill: The fill color of theslab andpoint sub-geometries. Use theslab_fillorpoint_fillaesthetics (below) to set sub-geometry colors separately.alpha: The opacity of theslab,interval, andpoint sub-geometries. Use theslab_alpha,interval_alpha, orpoint_alphaaesthetics (below) to set sub-geometry colors separately.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw theinterval (except withgeom_slab(): thenit is the width of theslab). With composite geometries including an interval and slab,useslab_linewidthto set the line width of theslab (see below). Forinterval, rawlinewidthvalues are transformed according to theinterval_size_domainandinterval_size_rangeparameters of thegeom(see above).size: Determines the size of thepoint. Iflinewidthis not provided,sizewillalso determines the width of the line used to draw theinterval (this allows line width andpoint size to be modified together by setting onlysizeand notlinewidth). Rawsizevalues are transformed according to theinterval_size_domain,interval_size_range,andfatten_pointparameters of thegeom(see above). Use thepoint_sizeaesthetic(below) to set sub-geometry size directly without applying the effects ofinterval_size_domain,interval_size_range, andfatten_point.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw theintervaland the outline of theslab (if it is visible). Use theslab_linetypeorinterval_linetypeaesthetics (below) to set sub-geometry line types separately.
Slab-specific color and line override aesthetics
slab_fill: Override forfill: the fill color of the slab.slab_colour: (orslab_color) Override forcolour/color: the outline color of the slab.slab_alpha: Override foralpha: the opacity of the slab.slab_linewidth: Override forlinwidth: the width of the outline of the slab.slab_linetype: Override forlinetype: the line type of the outline of the slab.
Interval-specific color and line override aesthetics
interval_colour: (orinterval_color) Override forcolour/color: the color of the interval.interval_alpha: Override foralpha: the opacity of the interval.interval_linetype: Override forlinetype: the line type of the interval.
Point-specific color and line override aesthetics
point_fill: Override forfill: the fill color of the point.point_colour: (orpoint_color) Override forcolour/color: the outline color of the point.point_alpha: Override foralpha: the opacity of the point.point_size: Override forsize: the size of the point.
Deprecated aesthetics
slab_size: Useslab_linewidth.interval_size: Useinterval_linewidth.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
See Also
Seegeom_slabinterval() for more information on the geom these statsuse by default and some of the options it has.Seevignette("slabinterval") for a variety of examples of use.
Examples
library(dplyr)library(ggplot2)library(distributional)theme_set(theme_ggdist())# EXAMPLES ON SAMPLE DATAset.seed(1234)df = data.frame( group = c("a", "b", "c", "c", "c"), value = rnorm(2500, mean = c(5, 7, 9, 9, 9), sd = c(1, 1.5, 1, 1, 1)))# here are vertical eyes:df %>% ggplot(aes(x = group, y = value)) + stat_eye()# note the sample size is not automatically incorporated into the# area of the densities in case one wishes to plot densities against# a reference (e.g. a prior distribution).# But you may wish to account for sample size if using these geoms# for something other than visualizing posteriors; in which case# you can use after_stat(f*n):df %>% ggplot(aes(x = group, y = value)) + stat_eye(aes(thickness = after_stat(pdf*n)))# EXAMPLES ON ANALYTICAL DISTRIBUTIONSdist_df = tribble( ~group, ~subgroup, ~mean, ~sd, "a", "h", 5, 1, "b", "h", 7, 1.5, "c", "h", 8, 1, "c", "i", 9, 1, "c", "j", 7, 1)# Using functions from the distributional package (like dist_normal()) with the# dist aesthetic can lead to more compact/expressive specificationsdist_df %>% ggplot(aes(x = group, ydist = dist_normal(mean, sd), fill = subgroup)) + stat_eye(position = "dodge")# using the old character vector + args approachdist_df %>% ggplot(aes(x = group, dist = "norm", arg1 = mean, arg2 = sd, fill = subgroup)) + stat_eye(position = "dodge")# the stat_slabinterval family applies a Jacobian adjustment to densities# when plotting on transformed scales in order to plot them correctly.# It determines the Jacobian using symbolic differentiation if possible,# using stats::D(). If symbolic differentation fails, it falls back# to numericDeriv(), which is less reliable; therefore, it is# advisable to use scale transformation functions that are defined in# terms of basic math functions so that their derivatives can be# determined analytically (most of the transformation functions in the# scales package currently have this property).# For example, here is a log-Normal distribution plotted on the log# scale, where it will appear Normal:data.frame(dist = "lnorm", logmean = log(10), logsd = 2*log(10)) %>% ggplot(aes(y = 1, dist = dist, arg1 = logmean, arg2 = logsd)) + stat_halfeye() + scale_x_log10(breaks = 10^seq(-5,7, by = 2))# see vignette("slabinterval") for many more examples.Spike plot (ggplot2 stat)
Description
Stat for drawing "spikes" (optionally with points on them) at specific pointson a distribution (numerical or determined as a function of the distribution),intended for annotatingstat_slabinterval() geometries.
Usage
stat_spike( mapping = NULL, data = NULL, geom = "spike", position = "identity", ..., at = "median", p_limits = c(NA, NA), density = "bounded", adjust = waiver(), trim = waiver(), breaks = waiver(), align = waiver(), outline_bars = waiver(), expand = FALSE, limits = NULL, n = waiver(), orientation = NA, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, check.aes = TRUE, check.param = TRUE)Arguments
mapping | Set of aesthetic mappings created by |
data | The data to be displayed in this layer. There are threeoptions: If A A |
geom | <Geom |string> Use to override the defaultconnection between |
position | <Position |string> Position adjustment,either as a string, or the result of a call to a position adjustment function.Setting this equal to |
... | Other arguments passed to
|
at | <numeric |function |character |list> The points at which toevaluate the PDF and CDF of the distribution. One of:
The values of |
p_limits | <length-2numeric> Probability limits. Used to determine the lower and upperlimits ofanalytical distributions (distributions fromsamples ignore this parameter and determinetheir limits based on the limits of the sample and the value of the |
density | <function |string> Density estimator for sample data. One of:
|
adjust | <scalarnumeric |waiver> Passed to |
trim | <scalarlogical |waiver> Passed to |
breaks | <numeric |function |string |waiver> Passed to
For example, |
align | <scalarnumeric |function |string |waiver> Passed to
For example, |
outline_bars | <scalarlogical |waiver> Passed to |
expand | <logical> For sample data, should the slab be expanded to the limits of the scale? Default |
limits | <length-2numeric> Manually-specified limits for the slab, asa vector of length two. These limits are combined with those computed based on |
n | <scalarnumeric> Number of points at which to evaluate the function that defines the slab. Alsopassed to |
orientation | <string> Whether this geom is drawn horizontally or vertically. One of:
For compatibility with the base ggplot naming scheme for |
na.rm | <scalarlogical> If |
show.legend | <logical> Should this layer be included in the legends? Default is |
inherit.aes | If |
check.aes,check.param | If |
Details
This stat computes slab values (i.e. PDF and CDF values) at specified locationson a distribution, as determined by theat parameter.
To visualize sample data, such as a data distribution, samples from abootstrap distribution, or a Bayesian posterior, you can supply samples tothex ory aesthetic.
To visualize analytical distributions, you can use thexdist orydistaesthetic. For historical reasons, you can also usedist to specify the distribution, thoughthis is not recommended as it does not work as well with orientation detection.These aesthetics can be used as follows:
xdist,ydist, anddistcan be any distribution object from thedistributionalpackage (dist_normal(),dist_beta(), etc)or can be aposterior::rvar()object. Since these functions are vectorized,other columns can be passed directly to them in anaes()specification; e.g.aes(dist = dist_normal(mu, sigma))will work ifmuandsigmaare columns in theinput data frame.distcan be a character vector giving the distribution name. Then thearg1, ...arg9aesthetics (orargsas a list column) specify distribution arguments. Distribution namesshould correspond to R functions that have"p","q", and"d"functions; e.g."norm"is a valid distribution name because R defines thepnorm(),qnorm(), anddnorm()functions for Normal distributions.See the
parse_dist()function for a useful way to generatedistandargsvalues from human-readable distribution specs (like"normal(0,1)"). Such specs are alsoproduced by other packages (like thebrms::get_priorfunction in brms); thus,parse_dist()combined with the stats described here can help you visualize the outputof those functions.
Value
Aggplot2::Stat representing a spike geometry which can be added to aggplot() object.
Aesthetics
The spikegeom has a wide variety of aesthetics that controlthe appearance of its two sub-geometries: thespike and thepoint.
Thesestats support the following aesthetics:
x: x position of the geometry (when orientation ="vertical"); or sample data to be summarized(whenorientation = "horizontal"with sample data).y: y position of the geometry (when orientation ="horizontal"); or sample data to be summarized(whenorientation = "vertical"with sample data).weight: When using samples (i.e. thexandyaesthetics, notxdistorydist), optionalweights to be applied to each draw.xdist: When using analytical distributions, distribution to map on the x axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.ydist: When using analytical distributions, distribution to map on the y axis:adistributional object (e.g.dist_normal())or aposterior::rvar()object.dist: When using analytical distributions, a name of a distribution (e.g."norm"), adistributional object (e.g.dist_normal()), or aposterior::rvar()object. SeeDetails.args: Distribution arguments (argsorarg1, ...arg9). SeeDetails.
In addition, in their default configuration (paired withgeom_spike())the following aesthetics are supported by the underlying geom:
Spike-specific (aka Slab-specific) aesthetics
thickness: The thickness of the slab at eachxvalue (iforientation = "horizontal") oryvalue (iforientation = "vertical") of the slab.side: Which side to place the slab on."topright","top", and"right"are synonymswhich cause the slab to be drawn on the top or the right depending on iforientationis"horizontal"or"vertical"."bottomleft","bottom", and"left"are synonyms which cause the slabto be drawn on the bottom or the left depending on iforientationis"horizontal"or"vertical"."topleft"causes the slab to be drawn on the top or the left, and"bottomright"causes the slab to be drawn on the bottom or the right."both"draws the slab mirrored on bothsides (as in a violin plot).scale: What proportion of the region allocated to this geom to use to draw the slab. Ifscale = 1,slabs that use the maximum range will just touch each other. Default is0.9to leave some spacebetween adjacent slabs. For a comprehensive discussion and examples of slab scaling and normalization,see thethicknessscale article.
Color aesthetics
colour: (orcolor) The color of thespike andpoint sub-geometries.fill: The fill color of thepoint sub-geometry.alpha: The opacity of thespike andpoint sub-geometries.colour_ramp: (orcolor_ramp) A secondary scale that modifies thecolorscale to "ramp" to another color. Seescale_colour_ramp()for examples.fill_ramp: A secondary scale that modifies thefillscale to "ramp" to another color. Seescale_fill_ramp()for examples.
Line aesthetics
linewidth: Width of the line used to draw thespike sub-geometry.size: Size of thepoint sub-geometry.stroke: Width of the outline around thepoint sub-geometry.linetype: Type of line (e.g.,"solid","dashed", etc) used to draw thespike.
Other aesthetics (these work as in standardgeoms)
widthheightgroup
See examples of some of these aesthetics in action invignette("slabinterval").Learn more about the sub-geom override aesthetics (likeinterval_color) in thescales documentation. Learn more about basic ggplot aesthetics invignette("ggplot2-specs").
Computed Variables
The following variables are computed by this stat and made available foruse in aesthetic specifications (aes()) using theafter_stat()function or theafter_stat argument ofstage():
xory: For slabs, the input values to the slab function.For intervals, the point summary from the interval function. Whether it isxorydepends onorientationxminorymin: For intervals, the lower end of the interval from the interval function.xmaxorymax: For intervals, the upper end of the interval from the interval function..width: For intervals, the interval width as a numeric value in[0, 1].For slabs, the width of the smallest interval containing that value of the slab.level: For intervals, the interval width as an ordered factor.For slabs, the level of the smallest interval containing that value of the slab.pdf: For slabs, the probability density function (PDF).Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the PDF at the point summary; intervals also havepdf_minandpdf_maxfor the PDF at the lower and upper ends of the interval.cdf: For slabs, the cumulative distribution function.Ifoptions("ggdist.experimental.slab_data_in_intervals")isTRUE:For intervals, the CDF at the point summary; intervals also havecdf_minandcdf_maxfor the CDF at the lower and upper ends of the interval.n: For slabs, the number of data points summarized into that slab. If the slab was created froman analytical distribution via thexdist,ydist, ordistaesthetic,nwill beInf.f: (deprecated) For slabs, the output values from the slab function (such as the PDF, CDF, or CCDF),determined byslab_type. Instead of usingslab_typeto changefand then mappingfonto anaesthetic, it is now recommended to simply map the corresponding computed variable (e.g.pdf,cdf, or1 - cdf) directly onto the desired aesthetic.at: For spikes, a character vector of names of the functions or expressions used to determinethe points at which the slab functions were evaluated to create spikes. Values of this computedvariable are determined by theatparameter; see its description above.
See Also
Seegeom_spike() for the geom underlying this stat.Seestat_slabinterval() for the stat this shortcut is based on.
Other slabinterval stats:stat_ccdfinterval(),stat_cdfinterval(),stat_eye(),stat_gradientinterval(),stat_halfeye(),stat_histinterval(),stat_interval(),stat_pointinterval(),stat_slab()
Examples
library(ggplot2)library(distributional)library(dplyr)df = tibble( d = c(dist_normal(1), dist_gamma(2,2)), g = c("a", "b"))# annotate the density at the mode of a distributiondf %>% ggplot(aes(y = g, xdist = d)) + stat_slab(aes(xdist = d)) + stat_spike(at = "Mode") + # need shared thickness scale so that stat_slab and geom_spike line up scale_thickness_shared()# annotate the endpoints of intervals of a distribution# here we'll use an arrow instead of a point by setting size = 0arrow_spec = arrow(angle = 45, type = "closed", length = unit(4, "pt"))df %>% ggplot(aes(y = g, xdist = d)) + stat_halfeye(point_interval = mode_hdci) + stat_spike( at = function(x) hdci(x, .width = .66), size = 0, arrow = arrow_spec, color = "blue", linewidth = 0.75 ) + scale_thickness_shared()# annotate quantiles of a sampleset.seed(1234)data.frame(x = rnorm(1000, 1:2), g = c("a","b")) %>% ggplot(aes(x, g)) + stat_slab() + stat_spike(at = function(x) quantile(x, ppoints(10))) + scale_thickness_shared()Scaled and shifted Student's t distribution
Description
Density, distribution function, quantile function and random generation for thescaled and shifted Student's t distribution, parameterized by degrees of freedom (df),location (mu), and scale (sigma).
Usage
dstudent_t(x, df, mu = 0, sigma = 1, log = FALSE)pstudent_t(q, df, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE)qstudent_t(p, df, mu = 0, sigma = 1, lower.tail = TRUE, log.p = FALSE)rstudent_t(n, df, mu = 0, sigma = 1)Arguments
x,q | vector of quantiles. |
df | degrees of freedom ( |
mu | <numeric> Location parameter (median). |
sigma | <numeric> Scale parameter. |
log,log.p | logical; if TRUE, probabilities p are given as log(p). |
lower.tail | logical; if TRUE (default), probabilities are |
p | vector of probabilities. |
n | number of observations. If |
Value
dstudent_tgives the densitypstudent_tgives the cumulative distribution function (CDF)qstudent_tgives the quantile function (inverse CDF)rstudent_tgenerates random draws.
The length of the result is determined byn forrstudent_t, and is the maximum of the lengths ofthe numerical arguments for the other functions.
The numerical arguments other thann are recycled to the length of the result. Only the first elementsof the logical arguments are used.
See Also
parse_dist() and parsing distribution specs and thestat_slabinterval()family of stats for visualizing them.
Examples
library(dplyr)library(ggplot2)expand.grid( df = c(3,5,10,30), scale = c(1,1.5)) %>% ggplot(aes(y = 0, dist = "student_t", arg1 = df, arg2 = 0, arg3 = scale, color = ordered(df))) + stat_slab(p_limits = c(.01, .99), fill = NA) + scale_y_continuous(breaks = NULL) + facet_grid( ~ scale) + labs( title = "dstudent_t(x, df, 0, sigma)", subtitle = "Scale (sigma)", y = NULL, x = NULL ) + theme_ggdist() + theme(axis.title = element_text(hjust = 0))Sub-geometry scales for geom_slabinterval (ggplot2 scales)
Description
These scales allow more specific aesthetic mappings to be made when usinggeom_slabinterval()and stats/geoms based on it (like eye plots).
Usage
scale_point_colour_discrete(..., aesthetics = "point_colour")scale_point_color_discrete(..., aesthetics = "point_colour")scale_point_colour_continuous( ..., aesthetics = "point_colour", guide = guide_colourbar2())scale_point_color_continuous( ..., aesthetics = "point_colour", guide = guide_colourbar2())scale_point_fill_discrete(..., aesthetics = "point_fill")scale_point_fill_continuous( ..., aesthetics = "point_fill", guide = guide_colourbar2())scale_point_alpha_continuous(..., range = c(0.1, 1))scale_point_alpha_discrete(..., range = c(0.1, 1))scale_point_size_continuous(..., range = c(1, 6))scale_point_size_discrete(..., range = c(1, 6), na.translate = FALSE)scale_interval_colour_discrete(..., aesthetics = "interval_colour")scale_interval_color_discrete(..., aesthetics = "interval_colour")scale_interval_colour_continuous( ..., aesthetics = "interval_colour", guide = guide_colourbar2())scale_interval_color_continuous( ..., aesthetics = "interval_colour", guide = guide_colourbar2())scale_interval_alpha_continuous(..., range = c(0.1, 1))scale_interval_alpha_discrete(..., range = c(0.1, 1))scale_interval_size_continuous(..., range = c(1, 6))scale_interval_size_discrete(..., range = c(1, 6), na.translate = FALSE)scale_interval_linetype_discrete(..., na.value = "blank")scale_interval_linetype_continuous(...)scale_slab_colour_discrete(..., aesthetics = "slab_colour")scale_slab_color_discrete(..., aesthetics = "slab_colour")scale_slab_colour_continuous( ..., aesthetics = "slab_colour", guide = guide_colourbar2())scale_slab_color_continuous( ..., aesthetics = "slab_colour", guide = guide_colourbar2())scale_slab_fill_discrete(..., aesthetics = "slab_fill")scale_slab_fill_continuous( ..., aesthetics = "slab_fill", guide = guide_colourbar2())scale_slab_alpha_continuous( ..., limits = function(l) c(min(0, l[[1]]), l[[2]]), range = c(0, 1))scale_slab_alpha_discrete(..., range = c(0.1, 1))scale_slab_size_continuous(..., range = c(1, 6))scale_slab_size_discrete(..., range = c(1, 6), na.translate = FALSE)scale_slab_linewidth_continuous(..., range = c(1, 6))scale_slab_linewidth_discrete(..., range = c(1, 6), na.translate = FALSE)scale_slab_linetype_discrete(..., na.value = "blank")scale_slab_linetype_continuous(...)scale_slab_shape_discrete(..., solid = TRUE)scale_slab_shape_continuous(...)guide_colourbar2(...)guide_colorbar2(...)Arguments
... | Arguments passed to underlying scale or guide functions. E.g. |
aesthetics | <character> Names of aesthetics to set scales for. |
guide | |
range | <length-2numeric> The minimum and maximum size of the plotting symbolafter transformation. |
na.translate | <scalarlogical> In discrete scales, should we show missing values? |
na.value | <linetype> When |
limits | One of:
|
solid | Should the shapes be solid, |
Details
The following additional scales / aesthetics are defined for use withgeom_slabinterval() andrelated geoms:
scale_point_color_* Point color
scale_point_fill_* Point fill color
scale_point_alpha_* Point alpha level / opacity
scale_point_size_* Point size
scale_interval_color_* Interval line color
scale_interval_alpha_* Interval alpha level / opacity
scale_interval_linetype_* Interval line type
scale_slab_color_* Slab outline color
scale_slab_fill_* Slab fill color
scale_slab_alpha_* Slab alpha level / opacity. The default settings of
scale_slab_alpha_continuousdiffer fromscale_alpha_continuous()and are designed for gradient plots (e.g.stat_gradientinterval()) by ensuring thatdensities of 0 get mapped to 0 in the output.scale_slab_linewidth_* Slab outline line width
scale_slab_linetype_* Slab outline line type
scale_slab_shape_* Slab dot shape (for
geom_dotsinterval())
See the corresponding scale documentation in ggplot for more information; e.g.scale_color_discrete(),scale_color_continuous(), etc.
Other scale functions can be used with the aesthetics/scales defined here by using theaestheticsargument to that scale function. For example, to use color brewer scales with thepoint_color aesthetic:
scale_color_brewer(..., aesthetics = "point_color")
With continuous color scales, you may also need to provide a guide as the default guide does not work properly;this is whatguide_colorbar2 is for:
scale_color_distiller(..., guide = "colorbar2", aesthetics = "point_color")
These scales have been deprecated:
scale_interval_size_* Use
scale_linewidth_*scale_slab_size_* Slab
scale_size_linewidth_*
Value
Aggplot2::Scale representing one of the aesthetics used to target the appearance of specific parts of compositeggdist geoms. Can be added to aggplot() object.
Author(s)
Matthew Kay
See Also
Other ggplot2 scales:scale_color_discrete(),scale_color_continuous(), etc.
Other ggdist scales:scale_colour_ramp,scale_side_mirrored(),scale_thickness
Examples
library(dplyr)library(ggplot2)# This plot shows how to set multiple specific aesthetics# NB it is very ugly and is only for demo purposes.data.frame(distribution = "Normal(1,2)") %>% parse_dist(distribution) %>% ggplot(aes(y = distribution, xdist = .dist, args = .args)) + stat_halfeye( shape = 21, # this point shape has a fill and outline point_color = "red", point_fill = "black", point_alpha = .1, point_size = 6, stroke = 2, interval_color = "blue", # interval line widths are scaled from [1, 6] onto [0.6, 1.4] by default # see the interval_size_range parameter in help("geom_slabinterval") linewidth = 8, interval_linetype = "dashed", interval_alpha = .25, # fill sets the fill color of the slab (here the density) slab_color = "green", slab_fill = "purple", slab_linewidth = 3, slab_linetype = "dotted", slab_alpha = .5 )Axis sub-guide for thickness scales
Description
This is a sub-guide intended for annotating thethickness and dot-countsubscales inggdist. It can be used with thesubguide parameter ofgeom_slabinterval() andgeom_dotsinterval().
Supportsautomatic partial function application withwaived arguments.
Usage
subguide_axis( values, title = NULL, breaks = waiver(), labels = waiver(), position = 0, just = 0, label_side = "topright", orientation = "horizontal", theme = theme_get())subguide_inside(..., label_side = "inside")subguide_outside(..., label_side = "outside", just = 1)subguide_integer(..., breaks = scales::breaks_extended(Q = c(1, 5, 2, 4, 3)))subguide_count(..., breaks = scales::breaks_width(1))subguide_slab(values, ...)subguide_dots(values, ...)subguide_spike(values, ...)Arguments
values | <numeric> Values used to construct the scale used for this guide.Typically provided automatically by |
title | <string> The title of the scale shown on the sub-guide's axis. |
breaks | One of:
|
labels | One of:
|
position | <scalarnumeric> Value between |
just | <scalarnumeric> Value between |
label_side | <string> Which side of the axis to draw the ticks and labels on. |
orientation | <string> Orientation of the geometry this sub-guide is for. Oneof |
theme | <theme> Theme used to determine the style that thesub-guide elements are drawn in. The title label is drawn using the |
... | Arguments passed to other functions, typically back to |
Details
subguide_inside() is a shortcut for drawing labels inside of the chartregion.
subguide_outside() is a shortcut for drawing labels outside of the chartregion.
subguide_integer() only draws breaks that are integer values, useful forlabeling counts ingeom_dots().
subguide_count() is a shortcut for drawing labels whereevery whole numberis labeled, useful for labeling counts ingeom_dots(). If your max count islarge,subguide_integer() may be better.
subguide_slab(),subguide_dots(), andsubguide_spike() are aliasesforsubguide_none() that allow you to change the default subguide usedfor thegeom_slabinterval(),geom_dotsinterval(), andgeom_spike()families. If you overwrite these in the global environment, you can setthe corresponding default subguide. For example:
subguide_slab = ggdist::subguide_inside(position = "right")
This will causegeom_slabinterval()s to default to having a guide on theright side of the geom.
See Also
Thethickness datatype.
Thethickness aesthetic ofgeom_slabinterval().
scale_thickness_shared(), for setting athickness scale acrossall geometries using thethickness aesthetic.
subscale_thickness(), for setting athickness sub-scale withina singlegeom_slabinterval().
Other sub-guides:subguide_none()
Examples
library(ggplot2)library(distributional)df = data.frame(d = dist_normal(2:3, 2:3), g = c("a", "b"))# subguides allow you to label thickness axesggplot(df, aes(xdist = d, y = g)) + stat_slabinterval(subguide = "inside")# they respect normalization and use of scale_thickness_shared()ggplot(df, aes(xdist = d, y = g)) + stat_slabinterval(subguide = "inside", normalize = "groups")# they can also be positioned outside the plot area, though# this typically requires manually adjusting plot marginsggplot(df, aes(xdist = d, y = g)) + stat_slabinterval(subguide = subguide_outside(title = "density", position = "right")) + theme(plot.margin = margin(5.5, 50, 5.5, 5.5))# any of the subguide types will also work to indicate bin counts in# geom_dots(); subguide_integer() and subguide_count() can be useful for# dotplots as they only label integers / whole numbers:df = data.frame(d = dist_gamma(2:3, 2:3), g = c("a", "b"))ggplot(df, aes(xdist = d, y = g)) + stat_dots(subguide = subguide_count(label_side = "left", title = "count")) + scale_y_discrete(expand = expansion(add = 0.1)) + scale_x_continuous(expand = expansion(add = 0.5))Empty sub-guide for thickness scales
Description
This is a blank sub-guide that omits annotations for thethickness anddot-count sub-scales inggdist. It can be used with thesubguideparameter ofgeom_slabinterval() andgeom_dotsinterval().
Supportsautomatic partial function application withwaived arguments.
Usage
subguide_none(values, ...)Arguments
values | <numeric> Values used to construct the scale used for this guide.Typically provided automatically by |
... | ignored. |
See Also
Other sub-guides:subguide_axis()
Identity sub-scale for thickness aesthetic
Description
This is an identity sub-scale for thethickness aestheticinggdist. It returns its input as athickness vector withoutrescaling. It can be used with thesubscale parameter ofgeom_slabinterval().
Usage
subscale_identity(x)Arguments
x | <numeric> Vector to be rescaled.Typically provided automatically by |
Value
Athickness vector of the same length asx, with infinitevalues inx squished into the data range.
See Also
Other sub-scales:subscale_thickness()
Sub-scale for thickness aesthetic
Description
This is a sub-scale intended for adjusting the scaling of thethicknessaesthetic at a geometry (or sub-geometry) level inggdist. It can beused with thesubscale parameter ofgeom_slabinterval().
Supportsautomatic partial function application withwaived arguments.
Usage
subscale_thickness( x, limits = function(l) c(min(0, l[1]), l[2]), expand = c(0, 0))Arguments
x | <numeric> Vector to be rescaled.Typically provided automatically by |
limits | |
expand | <numeric> Vector of limit expansion constants of length2 or 4, following the same format used by the |
Details
You can overwritesubscale_thickness in the global environment to setthe default properties of the thickness subscale. For example:
subscale_thickness = ggdist::subscale_thickness(expand = expansion(c(0, 0.05)))
This will causegeom_slabinterval()s to default to a thickness subscalethat expands by 5% at the top of the scale.Always prefix such adefinition withggdist:: to avoid infinite loops caused by recursion.
Value
Athickness vector of the same length asx scaled to be between0 and1.
See Also
Thethickness datatype.
Thethickness aesthetic ofgeom_slabinterval().
scale_thickness_shared(), for setting athickness scale acrossall geometries using thethickness aesthetic.
Other sub-scales:subscale_identity()
Examples
library(ggplot2)library(distributional)df = data.frame(d = dist_normal(2:3, 1), g = c("a", "b"))# breaks on thickness subguides are always limited to the bounds of the# subscale, which may leave labels off near the edge of the subscale# (e.g. here `0.4` is omitted because the max value is approx `0.39`)ggplot(df, aes(xdist = d, y = g)) + stat_slabinterval( subguide = "inside" )# We can use the subscale to expand the upper limit of the thickness scale# by 5% (similar to the default for positional scales), allowing bounds near# (but just less than) the limit, like `0.4`, to be shown.ggplot(df, aes(xdist = d, y = g)) + stat_slabinterval( subguide = "inside", subscale = subscale_thickness(expand = expansion(c(0, 0.5))) )Simple, light ggplot2 theme for ggdist and tidybayes
Description
A simple, relatively minimalist ggplot2 theme, and some helper functions to go with it.
Usage
theme_ggdist( base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22)theme_tidybayes( base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22)facet_title_horizontal()axis_titles_bottom_left()facet_title_left_horizontal()facet_title_right_horizontal()Arguments
base_size | base font size, given in pts. |
base_family | base font family |
base_line_size | base size for line elements |
base_rect_size | base size for rect elements |
Details
This is a relatively minimalist ggplot2 theme, intended to be used for making publication-ready plots.It is currently based onggplot2::theme_light().
A word of warning: this theme may (and very likely will) change in the future as I tweak it to my taste.
theme_ggdist() andtheme_tidybayes() are aliases.
Value
A named list in the format ofggplot2::theme()
Author(s)
Matthew Kay
See Also
ggplot2::theme(),ggplot2::theme_set()
Examples
library(ggplot2)theme_set(theme_ggdist())Thickness (datatype)
Description
A representation of the thickness of a slab: a scaled value (x) where0 is the base of the slab and1 is its maximum extent, and the lower(lower) and upper (upper) limits of the slab values in their originaldata units.
Usage
thickness(x = double(), lower = NA_real_, upper = NA_real_)Arguments
x | <coercible-to-numeric> Anumeric vector or an objectcoercible to anumeric (via |
lower | <numeric> The original lower bounds of thickness values before scaling.May be |
upper | <numeric> The original upper bounds of thickness values before scaling.May be |
Details
This datatype is used byscale_thickness_shared() andsubscale_thickness()to representnumeric()-like objects marked as being in units of slab "thickness".
Unlike regularnumeric()s,thickness() values mapped onto thethicknessaesthetic are not rescaled byscale_thickness_shared() orgeom_slabinterval().In most casesthickness() is not useful directly; though it can be used tomark values that should not be rescaled—see the definitions ofstat_ccdfinterval() andstat_gradientinterval() for some example usages.
thickness objects with unequal lower or upper limits may not be combined.However,thickness objects withNA limits may be combined withthickness objects with non-NA limits. This allows (e.g.) specifyinglocations on thethickness scale that are independent of data limits.
Value
Avctrs::rcrd of class"ggdist_thickness" with fields"x","lower", and"upper".
Author(s)
Matthew Kay
See Also
Thethickness aesthetic ofgeom_slabinterval().
scale_thickness_shared(), for setting athickness scale acrossall geometries using thethickness aesthetic.
subscale_thickness(), for setting athickness sub-scale withina singlegeom_slabinterval().
Examples
thickness(0:1)thickness(0:1, 0, 10)Translate between different tidy data frame formats for draws from distributions
Description
These functions translateggdist/tidybayes-style data frames to/from different data frameformats (each format using a different naming scheme for its columns).
Usage
to_broom_names(data)from_broom_names(data)to_ggmcmc_names(data)from_ggmcmc_names(data)Arguments
data | <data.frame> A data frame to translate. |
Details
Function prefixed withto_ translate from theggdist/tidybayes format to another format, functionsprefixed withfrom_ translate from that format back to theggdist/tidybayes format. Formats include:
to_broom_names() /from_broom_names():
.variable<->term.value<->estimate.prediction<->.fitted.lower<->conf.low.upper<->conf.high
to_ggmcmc_names() /from_ggmcmc_names():
.chain<->Chain.iteration<->Iteration.variable<->Parameter.value<->value
Value
A data frame with (possibly) new names in some columns, according to thetranslation scheme described inDetails.
Author(s)
Matthew Kay
Examples
library(dplyr)data(RankCorr_u_tau, package = "ggdist")df = RankCorr_u_tau %>% dplyr::rename(.variable = i, .value = u_tau) %>% group_by(.variable) %>% median_qi(.value)dfdf %>% to_broom_names()A waived argument
Description
A flag indicating that the default value of an argument should be used.
Usage
waiver()Details
Awaiver() is a flag passed to a function argument that indicates thefunction should use the default value of that argument. It is used in twocases:
ggplot2 functions use it to distinguish between "nothing" (
NULL)and a default value calculated elsewhere (waiver()).ggdist turnsggplot2's convention into a standardized method ofargument-passing: any named argument with a default value in anautomatically partially-applied function can be passed
waiver()when calling the function. This will cause the default value(or the most recently partially-applied value) of that argument to be usedinstead.Note: due to historical limitations,
waiver()cannot currently beused on arguments to thepoint_interval()family of functions.
See Also
auto_partial(),ggplot2::waiver()
Examples
f = auto_partial(function(x, y = "b") { c(x = x, y = y)})f("a")# uses the default value of `y` ("b")f("a", y = waiver())# partially apply `f`g = f(y = "c")g# uses the last partially-applied value of `y` ("c")g("a", y = waiver())Weighted empirical cumulative distribution function
Description
A variation ofecdf() that can be applied to weighted samples.
Usage
weighted_ecdf(x, weights = NULL, na.rm = FALSE)Arguments
x | <numeric> Sample values. |
weights | <numeric |NULL> Weights for the sample. One of:
|
na.rm | <scalarlogical> If |
Details
Generates a weighted empirical cumulative distribution function,F(x).Givenx, a sorted vector (derived fromx), andw_i, the correspondingweight forx_i,F(x) is a step function with steps at eachx_iwithF(x_i) equal to the sum of all weights up to and includingw_i.
Value
weighted_ecdf() returns a function of class"weighted_ecdf", which alsoinherits from thestepfun() class. Thus, it also hasplot() andprint()methods. Likeecdf(),weighted_ecdf() also provides aquantile() method,which dispatches toweighted_quantile().
See Also
Examples
weighted_ecdf(1:3, weights = 1:3)plot(weighted_ecdf(1:3, weights = 1:3))quantile(weighted_ecdf(1:3, weights = 1:3), 0.4)Weighted sample quantiles
Description
A variation ofquantile() that can be applied to weighted samples.
Usage
weighted_quantile( x, probs = seq(0, 1, 0.25), weights = NULL, n = NULL, na.rm = FALSE, names = TRUE, type = 7, digits = 7)weighted_quantile_fun(x, weights = NULL, n = NULL, na.rm = FALSE, type = 7)Arguments
x | <numeric> Sample values. |
probs | <numeric> Vector of probabilities in |
weights | <numeric |NULL> Weights for the sample. One of:
|
n | <scalarnumeric> Presumed effective sample size. If this is greater than 1 andcontinuous quantiles (
|
na.rm | <scalarlogical> If |
names | <scalarlogical> If |
type | <scalarinteger> Value between 1 and 9: determines the type of quantile estimatorto be used. Types 1 to 3 are for discontinuous quantiles, types 4 to 9 arefor continuous quantiles. SeeDetails. |
digits | <scalarnumeric> The number of digits to use to format percentageswhen |
Details
Calculates weighted quantiles using a variation of the quantile types basedon a generalization ofquantile().
Type 1–3 (discontinuous) quantiles are directly a function of the inverseCDF as a step function, and so can be directly translated to the weightedcase using the natural definition of the weighted ECDF as the cumulativesum of the normalized weights.
Type 4–9 (continuous) quantiles require some translation from the definitionsinquantile().quantile() defines continuous estimators in terms ofx_k, which is thekth order statistic, andp_k, which is a function ofkandn (the sample size). In the weighted case, we instead takex_k as thekthsmallest value ofx in the weighted sample (not necessarily an order statistic,because of the weights). Then we can re-write the formulas forp_k in terms ofF(x_k) (the empirical CDF atx_k, i.e. the cumulative sum of normalizedweights) andf(x_k) (the normalized weight atx_k), by using thefact that, in the unweighted case,k = F(x_k) \cdot n and1/n = f(x_k):
- Type 4
p_k = \frac{k}{n} = F(x_k)- Type 5
p_k = \frac{k - 0.5}{n} = F(x_k) - \frac{f(x_k)}{2}- Type 6
p_k = \frac{k}{n + 1} = \frac{F(x_k)}{1 + f(x_k)}- Type 7
p_k = \frac{k - 1}{n - 1} = \frac{F(x_k) - f(x_k)}{1 - f(x_k)}- Type 8
p_k = \frac{k - 1/3}{n + 1/3} = \frac{F(x_k) - f(x_k)/3}{1 + f(x_k)/3}- Type 9
p_k = \frac{k - 3/8}{n + 1/4} = \frac{F(x_k) - f(x_k) \cdot 3/8}{1 + f(x_k)/4}
Then the quantile function (inverse CDF) is the piece-wise linear functiondefined by the points(p_k, x_k).
Value
weighted_quantile() returns a numeric vector oflength(probs) with theestimate of the corresponding quantile fromprobs.
weighted_quantile_fun() returns a function that takes a single argument,a vector of probabilities, which itself returns the corresponding quantileestimates. It may be useful whenweighted_quantile() needs to be calledrepeatedly for the same sample, re-using some pre-computation.