date_short() attempts to construct the shortest format string that will uniquely identify dates. It's inspired by matplotlib's ConciseDateFormatter, but uses a slightly different approach: ConciseDateFormatter formats "firsts" (e.g. first day of month, first day of day) specially; date_short() formats changes (e.g. new month, new year) specially.

date_format(format = "%Y-%m-%d", tz = "UTC")

date_short(format = c("%Y", "%b", "%d", "%H:%M"), sep = "\n")

time_format(format = "%H:%M:%S", tz = "UTC")

Arguments

format

For date_format() and time_format() a date/time format string using standard POSIX specification. See strptime() for details.

For date_short() a character vector of length 4 giving the format components to use for year, month, day, and hour respectively.

tz

a time zone name, see timezones(). Defaults to UTC

sep

Separator to use when combining date formats into a single string.

Examples

date_range <- function(start, days) { start <- as.POSIXct(start) c(start, start + days * 24 * 60 * 60) } two_months <- date_range("2020-05-01", 60) demo_datetime(two_months)
#> ggplot(df, aes(x, 1)) + #> geom_blank() + #> scale_x_datetime()
demo_datetime(two_months, labels = date_format("%m/%d"))
#> ggplot(df, aes(x, 1)) + #> geom_blank() + #> scale_x_datetime(labels = date_format("%m/%d"))
# ggplot2 provides a short-hand: demo_datetime(two_months, date_labels = "%m/%d")
#> ggplot(df, aes(x, 1)) + #> geom_blank() + #> scale_x_datetime(date_labels = "%m/%d")
# An alternative labelling system is date_short() demo_datetime(two_months, date_breaks = "7 days", labels = date_short())
#> ggplot(df, aes(x, 1)) + #> geom_blank() + #> scale_x_datetime(date_breaks = "7 days", labels = date_short())
# This is particularly effective for dense labels one_year <- date_range("2020-05-01", 365) demo_datetime(one_year, date_breaks = "month")
#> ggplot(df, aes(x, 1)) + #> geom_blank() + #> scale_x_datetime(date_breaks = "month")
demo_datetime(one_year, date_breaks = "month", labels = date_short())
#> ggplot(df, aes(x, 1)) + #> geom_blank() + #> scale_x_datetime(date_breaks = "month", labels = date_short())