time_cut {timeplyr} | R Documentation |
Cut dates and datetimes into regularly spaced date or datetime intervals
Description
time_breaks
and time_cut()
are very useful for
plotting with dates and date-times as the breaks are of regular width.
Usage
time_cut(
x,
n = 5,
time_by = NULL,
from = NULL,
to = NULL,
time_floor = FALSE,
week_start = getOption("lubridate.week.start", 1),
time_type = getOption("timeplyr.time_type", "auto"),
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "NA"),
as_interval = getOption("timeplyr.use_intervals", FALSE)
)
time_breaks(
x,
n = 5,
time_by = NULL,
from = NULL,
to = NULL,
time_floor = FALSE,
week_start = getOption("lubridate.week.start", 1),
time_type = getOption("timeplyr.time_type", "auto"),
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "NA")
)
Arguments
x |
Time variable. |
n |
Number of breaks. |
time_by |
Time unit.
|
from |
Time series start date. |
to |
Time series end date. |
time_floor |
Logical. Should the initial date/datetime be floored before building the sequence? |
week_start |
day on which week starts following ISO conventions - 1
means Monday (default), 7 means Sunday.
This is only used when |
time_type |
If "auto", |
roll_month |
Control how impossible dates are handled when
month or year arithmetic is involved.
Options are "preday", "boundary", "postday", "full" and "NA".
See |
roll_dst |
See |
as_interval |
Should result be a |
Details
To retrieve regular time breaks that simply spans the range of x
,
use time_seq()
or time_aggregate()
.
This can also be achieved in time_cut()
by supplying n = Inf
.
By default time_cut()
will try to find
the prettiest way of cutting the interval by
trying to cut the date/date-times into
groups of the highest possible time units,
starting at years and ending at milliseconds.
When x
is a numeric vector, time_cut
will behave similar to time_cut
except for 3 things:
The intervals are all right-open and of equal width.
The left value of the leftmost interval is always
min(x)
.Up to
n
breaks are created, i.e<= n
breaks. This is to prioritise pretty breaks.
time_cut
is a generalisation of time_summarisev
such that the
below identity should always hold:
identical(time_cut(x, n = Inf, as_factor = FALSE), time_summarisev(x))
Or also:
breaks <- time_breaks(x, n = Inf) identical(breaks[unclass(time_cut(x, n = Inf))], time_summarisev(x))
Value
time_breaks
returns a vector of breaks.
time_cut
returns either a factor
, time_interval
or a vector the
same class as x
.
Examples
library(timeplyr)
library(lubridate)
library(ggplot2)
library(dplyr)
time_cut(1:10, n = 5)
# Easily create custom time breaks
df <- nycflights13::flights %>%
fslice_sample(n = 10, seed = 8192821) %>%
select(time_hour) %>%
farrange(time_hour) %>%
mutate(date = as_date(time_hour))
# time_cut() and time_breaks() automatically find a
# suitable way to cut the data
options(timeplyr.use_intervals = TRUE)
time_cut(df$date)
# Works with datetimes as well
time_cut(df$time_hour, n = 5) # <= 5 breaks
# Custom formatting
options(timeplyr.interval_sub_formatter =
function(x) format(x, format = "%Y %b"))
time_cut(df$date, time_by = "month")
# Just the breaks
time_breaks(df$date, n = 5, time_by = "month")
cut_dates <- time_cut(df$date)
date_breaks <- time_breaks(df$date)
# WHen n = Inf and as_factor = FALSE, it should be equivalent to using
# time_aggregate or time_summarisev
identical(time_cut(df$date, n = Inf, time_by = "month"),
time_summarisev(df$date, time_by = "month"))
identical(time_summarisev(df$date, time_by = "month"),
time_aggregate(df$date, time_by = "month"))
# To get exact breaks at regular intervals, use time_expandv
weekly_breaks <- time_expandv(df$date,
time_by = "5 weeks",
week_start = 1, # Monday
time_floor = TRUE)
weekly_labels <- format(weekly_breaks, "%b-%d")
df %>%
time_by(date, time_by = "week", .name = "date") %>%
count() %>%
mutate(date = interval_start(date)) %>%
ggplot(aes(x = date, y = n)) +
geom_bar(stat = "identity") +
scale_x_date(breaks = weekly_breaks,
labels = weekly_labels)
reset_timeplyr_options()