Pr_ {ggdist}R Documentation

Probability expressions in ggdist aesthetics

Description

Experimental probability-like expressions that can be used in place of some after_stat() expressions in aesthetic assignments in ggdist stats.

Usage

Pr_(x)

p_(x)

Arguments

x

Bare (unevaluated) expressions. See Details.

Details

Pr_() and p_() are an experimental mini-language for specifying aesthetic values based on probabilities and probability densities derived from distributions supplied to ggdist stats (e.g., in stat_slabinterval(), stat_dotsinterval(), etc.). They generate expressions that use after_stat() and the computed variables of the stat (such as cdf and pdf; see e.g. the Computed Variables section of stat_slabinterval()) to compute the desired probabilities or densities.

For example, one way to map the density of a distribution onto the alpha aesthetic of a slab is to use after_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)))

Where p_(x) is the probability density function. The use of ⁠!!⁠ is necessary to splice the generated expression into the aes() call; for more information, see quasiquotation.

Probability expressions

Probability expressions consist of a call to Pr_() or p_() containing a small number of valid combinations of operators and variable names.

Valid variables in probability expressions include:

Pr_() generates expressions for probabilities, e.g. cumulative distribution functions (CDFs). Valid operators inside Pr_() are:

p_() generates expressions for probability density functions or probability mass functions (depending on if the underlying distribution is continuous or discrete). It currently does not allow any operators in the expression, and must be passed one of x, y, or value.

See Also

The Computed Variables section of stat_slabinterval() (especially cdf and pdf) and the after_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 fill
ggplot(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 fill
ggplot(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)


[Package ggdist version 3.3.2 Index]