scale_x_year
is a wrapper around ggplot2::scale_x_date()
that automates some of its boilerplate.
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:
Set the span of the x-axis ticks to include the entire range of the data
Position the ticks between years
Set the start, end, and by years of the x-axis labels, indicating only years and not entire dates.
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))