Skip to contents

scale_x_year is a wrapper around ggplot2::scale_x_date() that automates some of its boilerplate.

Usage

scale_x_year(
  from = ggplot2::waiver(),
  to = ggplot2::waiver(),
  by = ggplot2::waiver(),
  n = 5,
  expand = 0,
  ...
)

Arguments

from

The first year to show in the x-axis labels (breaks).

to

The last year to show in the x-axis labels (breaks).

by

The spacing between labels, in years.

n

The suggested number of levels, to be passed to scales::breaks_pretty() in case from/to/by are not set.

expand

Increase the size of the x-axis by this fraction.

...

other arguments passed on to pretty()

Details

In particular, the function will:

  1. Set the span of the x-axis ticks to include the entire range of the data

  2. Position the ticks between years

  3. Set the start, end, and by years of the x-axis labels, indicating only years and not entire dates.

  4. Position the x-axis labels in the midpoint of each year.

It is equivalent to e.g.:

scale_x_date(minor_breaks=seq(from=as.Date("2003-01-01"),
                              to=as.Date("2023-01-01"),
                              by="1 years"),
             breaks=seq(from=as.Date("2004-06-30"),
                        to=as.Date("2023-06-30"),
                        by="3 years"),
             date_labels="%Y",
             expand=expansion(mult=.05))