Skip to contents

Computes weeks for date data. This is mostly equivalent to ggplot2::stat_bin() with the bins fixed to weeks starting on a particular day.

Usage

stat_week(
  mapping = NULL,
  data = NULL,
  geom = "bar",
  position = "stack",
  ...,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE,
  week_start = getOption("phylepic.week_start"),
  pad = FALSE
)

Arguments

mapping, data, geom, position, na.rm, show.legend, inherit.aes, pad, ...

See ggplot2::stat_bin().

week_start

Day the week begins (defaults to Monday). Can be specified as a case-insensitive English weekday name such as "Monday" or an integer. Since you generally won't want to mix definitions, it is more convenient to control this globally with the "phylepic.week_start" option, e.g. options(phylepic.week_start = "Monday").

Value

ggplot2 stat layer.

Examples

library(ggplot2)

set.seed(1)
events <- rep(as.Date("2024-01-31") - 0:30, rpois(31, 2))
df <- data.frame(date = events)

ggplot(df) + stat_week(aes(date), week_start = "Monday")


# or equivalently:
# ggplot(df) + geom_bar(aes(date), stat = "week", week_start = "Monday")