| scale_mixtime | R Documentation |
These are the default scales for mixtime vectors, responsible for mapping
time points to aesthetics along with identifying break points and labels for
the axes and guides. To override the scales behaviour manually, use
scale_*_mixtime. The primary purpose of these scales is to scale time
points across multiple granularities onto a common time scale. This is
achieved by identifying and coercing all time points to the finest chronon
that all time points can be represented in. This common time chronon is
automatically identified, but can be manually specified using the
time_chronon argument.
scale_x_mixtime(
name = waiver(),
breaks = waiver(),
time_breaks = waiver(),
minor_breaks = waiver(),
time_minor_breaks = waiver(),
labels = waiver(),
time_labels = waiver(),
time_chronon = waiver(),
align_discrete = aes_nudge(),
transform = "identity",
limits = NULL,
expand = waiver(),
oob = scales::censor,
guide = waiver(),
position = "bottom",
sec.axis = waiver()
)
scale_y_mixtime(
name = waiver(),
breaks = waiver(),
time_breaks = waiver(),
minor_breaks = waiver(),
time_minor_breaks = waiver(),
labels = waiver(),
time_labels = waiver(),
time_chronon = waiver(),
align_discrete = aes_nudge(),
transform = "identity",
limits = NULL,
expand = waiver(),
oob = scales::censor,
guide = waiver(),
position = "left",
sec.axis = waiver()
)
name |
The name of the scale. Used as the axis or legend title. If
|
breaks |
One of:
|
time_breaks |
A duration giving the distance between breaks, such as
|
minor_breaks |
One of:
|
time_minor_breaks |
A duration giving the distance between minor
breaks, such as |
labels |
One of the options below. Please note that when
|
time_labels |
A mixtime format string to format the labels, as described
in |
time_chronon |
A time granule that defines the common chronon to use for
mixed granularity (e.g. |
align_discrete |
Either a single number between 0 and 1, or a
If a single number is supplied, it is used for all positional aesthetics: 0 means start alignment, 1 means end alignment, and 0.5 means center alignment (the default). To specify different offsets for different positional aesthetics (e.g.
'align_discrete = aes_nudge(center = 0.5, left = 0.25, right = 0.75)“ The |
transform |
A transformation applied to the time scale, after time
points have been mapped onto the common time scale. Given as either a
|
limits |
One of:
|
expand |
For position scales, a vector of range expansion constants used to add some
padding around the data to ensure that they are placed some distance
away from the axes. Use the convenience function |
oob |
One of:
|
guide |
A function used to create a guide or its name. See
|
position |
For position scales, The position of the axis.
|
sec.axis |
|
When using mixtime vectors to represent time variables in ggplot2, these
scales are automatically applied. In most cases, the default behaviour will
be sufficient for scaling time points into plot aesthetics. These scales can
be used to manually adjust the scaling behaviour, such as adjusting the
breaks and labels or using a different common time scale.
Similarly to the temporal scales in ggplot2 (ggplot2::scale_x_date() and
ggplot2::scale_x_datetime()), these scales can adjust the breaks and labels
using duration-based intervals and time formatting. These time aware
options are prefixed with time_ (e.g. time_breaks and time_labels),
and take precedence over the non-time aware options (e.g. breaks and
labels). The scale's breaks are specified with mixtime::duration()
objects (e.g. time_breaks = mixtime::months(1L)) or a time granule.
Labels are specified with mixtime format strings, which describe a time point
as glue-style {} placeholders holding the granules to show. Since the
granules come from a calendar, this works across calendars rather than only
Gregorian ones: time_labels = "{cyc(month, year, label = TRUE, abbreviate = TRUE)} {lin(year)}" gives "Jan 2020". See
vignette("time-format-strings", package = "mixtime") for the full syntax.
A core feature of these scales is the ability to handle time from multiple
timezones, granularities, and calendars. This is achieved by mapping all time
points to a common time scale, which is automatically identifying the finest
compatible chronon that can represent the input data. This allows time points
across different granularities (e.g. base::POSIXt, base::Date, and
mixtime::yearmonth) to be plotted together on a common time scale. In this
case the finest chronon is 1 second (from base::POSIXt), so all time points
are mapped to a 1 second chronon for plotting. Mapping day and month chronons
to seconds introduces indeterminancy - which second should be used to
represent a day or month? This is resolved using the align_discrete argument,
which defaults to center alignment. This means that a day is mapped to noon,
and a month is mapped to the middle of the month.
Further details about time specific scale options are described in the following sections.
Visualising mixed granularity time data introduces indeterminacy in the
mapping of less precise time points onto a common time scale. For example,
plotting monthly and daily data together raises the question of where to
place the monthly points relative to the daily points. By default, mixtime
uses center alignment, mapping the monthly points to the middle of the
month. This is controlled using the align_discrete argument, which accepts a
value between 0 (start alignment) and 1 (end alignment) and defaults to 0.5.
The common time scale that defines how all granularities are mapped is
automatically identified based on the input data. This is achieved by finding
the finest chronon that all time points can be represented in. For example,
if the data contains both monthly and daily time points, the common time scale
will be daily, with the monthly points aligned according to the align_discrete
argument. If multiple time zones are present, the common time zone will
default to UTC. The common time scale can be manually specified using the
time_chronon argument, which accepts a mixtime::time_unit.
library(ggplot2)
library(dplyr)
uad_month <- tibble(
time = mixtime::yearmonth("1973 Jan") + 0:71,
value = USAccDeaths
)
uad_year <- uad_month %>%
group_by(time = mixtime::year(time)) %>%
summarise(value = mean(value), .groups = "drop")
bind_rows(
month = uad_month,
year = uad_year,
.id = "grain"
) %>%
ggplot(aes(time, value, color = grain)) +
geom_line() +
scale_x_mixtime()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.