| scale_tind | R Documentation |
These functions provide ggplot2 scales for tind. Scales will
be added automatically by ggplot2, but the default behaviour
can be overridden by adding scale_*_tind to the plot.
scale_x_tind(
name = ggplot2::waiver(),
breaks = ggplot2::waiver(),
minor_breaks = ggplot2::waiver(),
n.breaks = 7L,
labels = ggplot2::waiver(),
limits = NULL,
expand = ggplot2::waiver(),
guide = ggplot2::waiver(),
position = "bottom",
format = NULL,
locale = NULL
)
scale_y_tind(
name = ggplot2::waiver(),
breaks = ggplot2::waiver(),
minor_breaks = ggplot2::waiver(),
n.breaks = 7L,
labels = ggplot2::waiver(),
limits = NULL,
expand = ggplot2::waiver(),
guide = ggplot2::waiver(),
position = "left",
format = NULL,
locale = NULL
)
name |
a character string with axis name, |
breaks |
|
minor_breaks |
|
n.breaks |
an integer value, desired number of breaks. |
labels |
|
limits |
|
expand, guide |
see scale_continuous. |
position |
a character string determining axis position,
|
format |
(optional) a character string determining label format
(see |
locale |
(optional) a character string determining locale to be used for formatting labels, see calendar-names for information on locale settings. |
The algorithm determining positioning of breaks and minor breaks always takes
the resolution of time indices (see resolution_t) into account.
For example, for monthly data with breaks placed every three months (January,
April, July, October) minor breaks will never be placed in the middle (mid of
February, May, August, November) but rather every month. The algorithm
overrides the default approach of ggplot2 — axis limits are determined
based on breaks and breaks on time indices and their resolution, whereas
ggplot2 starts with limits based on data and determines breaks based
on limits only (ignoring the resolution of time indices).
Argument list is a bit different from that of to scale_*_date and
scale_*_datetime. Firstly, n.breaks argument is supported,
allowing users to set the expected number of breaks on time axis. Secondly,
labels cannot be a function. Formatting functions can be provided via
format argument, which also supports character string with format
specification (see format) rendering date_labels
argument redundant. locale argument controls the language used
for month and weekday names, see calendar-names.
breaks cannot be a function, but can be a tdiff / character string
determining distance between breaks rendering date_breaks argument
redundant. limits argument is expected to determine time interval
and not limits in Cartesian coordinates. Open-ended intervals are supported.
breaks, minor_breaks, and limits cannot be functions
as ggplot2 assumes that breaks and limits can be properly set based
on (automatic) limits only without taking into account the resolution of time
indices, which is not true.
Secondary axes are not supported.
A continuous scale as returned by continuous_scale.
Due to the fact that limits method is currently
(as of version 4.0.0) not exported, users cannot use xlim and
ylim with tind scales. Limits on time-indexed axes can be set
using limits argument to scale_*_tind.
pretty for computing pretty breakpoints,
axis_t for computing time axis parameters,
axis.tind for creating axes with graphics package.
# artificial data
d <- seq(floor_t(today(), "y"), ceiling_t(today(), "y", ceiling = "last"))
df <- data.frame(d = d, D = as.Date(d), y = cumsum(rnorm(length(d))),
q = paste0("Q", quarter(d)))
# load ggplot2
have_ggplot2 <- require("ggplot2", quietly = FALSE)
# default scale
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw()
}
# compare with the default scale for Date
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = D, y = y)) + theme_bw()
}
# change format
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw() +
scale_x_tind(format = "%b '%y")
}
# set breaks every 4 months
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw() +
scale_x_tind(breaks = "4m")
}
# set limits
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y), na.rm = TRUE) + theme_bw() +
scale_x_tind(limits = c(today() %-m% 4, today()))
}
# facets with custom formatting and reduced number of breaks
if (have_ggplot2) {
ggplot(df) + geom_line(aes(x = d, y = y)) + theme_bw() +
scale_x_tind(n.breaks = 4, format = "%b '%y") +
facet_wrap(~q, scales = "free_x")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.