Skip to contents

A thin wrapper around ggplot2::scale_y_continuous() that sets the upper limit with nice_max() and, by default, formats the axis as percentages. Designed for the percentage bar charts produced by calc_percentage().

Usage

scale_y_pct(values = NULL, unit = 25, pad = 0, max = NULL, labels = TRUE, ...)

Arguments

values

Numeric vector used to determine the axis ceiling, typically the column being plotted. If NULL (default) the limit is left to ggplot2 and only the label formatting is applied.

unit, pad

Passed to nice_max().

max

Optional fixed upper limit. If supplied (e.g. 100), it overrides the dynamic nice_max() ceiling. Defaults to NULL.

labels

One of:

  • TRUE (default) – format breaks as "42%".

  • FALSE – hide axis labels entirely (labels = NULL).

  • a function – used directly as the labels argument.

...

Further arguments passed to ggplot2::scale_y_continuous().

Value

A ggplot2 scale you can add to a plot with +.

Details

The upper limit is chosen in one of three ways, in order of precedence: a fixed max if you supply one; otherwise nice_max() of values; otherwise left to ggplot2. Labels default to whole-percent strings via label_pct(). plot_bars() calls this for you, reading its unit and max from the pct_axis_unit / pct_axis_max options (see ezrsurvey_options()), so you rarely add it by hand – but it is exported for custom charts.

Examples

df <- calc_percentage(podracing_survey, demo_gender)
ggplot(df, aes(demo_gender, pct)) +
  geom_col() +
  scale_y_pct(df$pct)            # axis capped at a tidy multiple, "%" labels