scale_x_grates_yearquarter: Yearquarter scale

View source: R/yearquarter-scale.R

scale_x_grates_yearquarterR Documentation

Yearquarter scale

Description

ggplot2 scale for a yearquarter vector.

Usage

scale_x_grates_yearquarter(
  ...,
  breaks = ggplot2::waiver(),
  n.breaks = 6L,
  format = NULL
)

Arguments

...

Not currently used.

breaks

A ⁠<grates_yearquarter>⁠ vector of the desired breaks.

n.breaks

⁠[integer]⁠

Approximate number of breaks calculated using scales::breaks_pretty (default 6L).

Will only have an effect if breaks = waiver().

format

Format to use if "Date" scales are required.

If not NULL then the value is used by format.Date() and can be any input acceptable by that function.

Value

A scale for use with ggplot2.

Examples



# use simulated linelist data from the outbreaks package
linelist <- outbreaks::ebola_sim_clean$linelist

# calculate quarterly cases by date of infection
x <- as_yearquarter(linelist$date_of_infection)
(dat <- aggregate(list(cases = x), by = list(quarter = x), FUN = length))

# by default labels are centred
(quarter_plot <-
    ggplot2::ggplot(dat, ggplot2::aes(quarter, cases)) +
    ggplot2::geom_col(width = 1, colour = "white") +
    ggplot2::theme_bw() +
    ggplot2::theme(
        axis.text.x = ggplot2::element_text(
            angle = 45,
            hjust = 1
        )
    ) +
    ggplot2::xlab(""))

# To obtain centred labels you must explicitly set a date format
# in the scale:
quarter_plot + scale_x_grates_yearquarter(format = "%Y-%m-%d")


grates documentation built on April 3, 2025, 10:49 p.m.