Learn R Programming

grates (version 1.4.3)

scale_x_grates_year: Year scale

Description

ggplot2 scale for year vector.

Usage

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

Value

A scale for use with ggplot2.

Arguments

...

Not currently used.

breaks

A <grates_isoweek> 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.

Examples

Run this code
if (FALSE) { # requireNamespace("outbreaks") && requireNamespace("ggplot2")

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

# calculate yearly cases by date of infection
x <- as_year(linelist$date_of_infection)
(dat <- aggregate(list(cases = x), by = list(year = x), FUN = length))

# by default labels are centred
(year_plot <-
    ggplot2::ggplot(dat, ggplot2::aes(year, cases)) +
    ggplot2::geom_col(width = 1, colour = "white") +
    ggplot2::theme_bw() +
    ggplot2::xlab(""))

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

}

Run the code above in your browser using DataLab