Skip to contents

[Experimental]

Usage

show_cutoffs(plot, part, color = "#1e3a8a", size = 4, labels = FALSE)

Arguments

plot

A ggplot of one distribution – a histogram, bar chart, density, dotplot, or gf_squareplot().

part

A distribution part, e.g. middle(Thumb, .95). Optional: without it, the part is read off the plot's fill aesthetic.

color

Marker/line color. Default "#1e3a8a".

size

Marker size. Default 4.

labels

Whether to annotate the cutoffs. Default FALSE.

Value

A ggplot object with cutoff markers and optional labels.

Details

Adds downward-pointing triangle markers at the empirical quantile cutoffs of a distribution part – middle(), tails(), upper(), lower(), or outer(). By default the part is read off the plot's fill aesthetic, e.g. fill = ~middle(Thumb, .95). Passing part overrides that reading: it marks whatever part is named there instead, and the fill (if any) is ignored – marking the 99% cutoffs on a plot shaded for the 95% is a deliberate, lossless override, not a mismatch.

Calling show_cutoffs() more than once on the same plot stacks a second, independent set of markers on top of the first; nothing about the plot or the first call needs to change for the second one to land correctly. A second labels = TRUE call, though, draws its labels at the same height as the first's and the two overlap – show_cutoffs() warns about that and draws anyway, because the picture is still the one asked for, just harder to read.

show_cutoffs() refuses a plot whose first layer does not draw a distribution (a scatterplot, for instance) and, when part is given explicitly, a part that names a variable other than the one the plot puts on x.

See also

StatCutoff, a ggplot2::Stat that computes the same rule per panel, for building a marker into a plot with ggplot2::layer() directly.

Examples

gf_histogram(~Thumb, data = Fingers, binwidth = 5, fill = ~middle(Thumb, .95)) %>%
  show_cutoffs(labels = TRUE)


# an explicit part overrides the fill instead of requiring it to match
gf_histogram(~Thumb, data = Fingers, binwidth = 5, fill = ~middle(Thumb, .95)) %>%
  show_cutoffs(middle(Thumb, .99))