Skip to contents

[Experimental]

Usage

show_mean(plot, color = "#E60000", linetype = "longdash", linewidth = 0.7)

Arguments

plot

A plot of one distribution.

color

Line color. Default "#E60000".

linetype

Line type. Default "longdash".

linewidth

Line width. Default 0.7.

Value

The plot, with a tagged mean line added.

Details

Draws a vertical line at the mean of the variable a distribution is built from, spanning the count axis. Works on any plot of one distribution – gf_histogram(), gf_dotplot(), gf_squareplot(). A faceted plot gets one line per panel, at that panel's own mean, because a facet is a region with its own subset of the data. Each line spans the count axis of the panel it is drawn in, so scales = "free_y" is supported and no panel is stretched to hold another panel's line.

A plot with a variable on each axis is refused. The middle of a two-variable plot is a model, and gf_model() draws models – an hline at the mean of the outcome for the empty model. Averaging the x variable of a scatterplot would draw a line nobody asked for.

Facet the plot before calling show_mean(), not after: each panel's top is measured once, from the plot as it stands when show_mean() is called, so a panel added later has no top to draw against and is left without a line.

See also

show_dgp() frames a sampling distribution with the process that generated it; gf_model() draws the mean of a two-variable plot's outcome.

Examples

gf_histogram(~Thumb, data = Fingers, binwidth = 5) %>% show_mean()


# a facet is a region with its own subset, so each panel gets its own mean
gf_histogram(~Thumb | Sex, data = Fingers, binwidth = 5) %>% show_mean()