apexcharter/R/labs.R

60 lines
1.3 KiB
R
Raw Normal View History

2019-02-16 19:56:22 +01:00
#' Modify axis, legend, and chart labels
#'
#' @template ax-default
2019-02-16 19:56:22 +01:00
#' @param title Text for the title.
#' @param subtitle Text for the subtitle.
#' @param x Text for the x-axis label.
#' @param y Text for the y-axis label.
#'
#' @export
#'
#' @examples
#' meteo_paris <- data.frame(
#' month = month.name,
#' tmax = c(7, 8, 12, 15, 19, 23, 25, 25, 21, 16, 11, 8),
#' tmin = c(3, 3, 5, 7, 11, 14, 16, 16, 13, 10, 6, 3)
#' )
#'
2020-02-14 18:13:24 +01:00
#' apex(meteo_paris, type = "column", aes(x = month, y = tmin)) %>%
2019-02-16 19:56:22 +01:00
#' ax_labs(
#' title = "Average minimal temperature in Paris",
#' subtitle = "Data from NOAA",
2020-02-14 18:13:24 +01:00
#' x = "Month",
#' y = "Temperature (\u00b0C)"
2019-02-16 19:56:22 +01:00
#' )
ax_labs <- function(ax, title = NULL, subtitle = NULL, x = NULL, y = NULL) {
if (!is.null(title)) {
2020-04-29 11:14:54 +02:00
ax <- ax_title(
ax = ax,
text = title,
style = list(fontWeight = 700, fontSize = "16px")
)
2019-02-16 19:56:22 +01:00
}
if (!is.null(subtitle)) {
2020-04-29 11:14:54 +02:00
ax <- ax_subtitle(
ax = ax,
text = subtitle,
style = list(fontWeight = 400, fontSize = "14px")
)
2019-02-16 19:56:22 +01:00
}
if (!is.null(x)) {
2020-04-29 11:14:54 +02:00
ax <- ax_xaxis(
ax = ax,
2020-04-29 11:35:06 +02:00
title = list(text = x, style = list(fontWeight = 400, fontSize = "14px"))
2020-04-29 11:14:54 +02:00
)
2019-02-16 19:56:22 +01:00
}
if (!is.null(y)) {
2020-04-29 11:14:54 +02:00
ax <- ax_yaxis(
ax = ax,
2020-04-29 11:35:06 +02:00
title = list(text = y, style = list(fontWeight = 400, fontSize = "14px"))
2020-04-29 11:14:54 +02:00
)
2019-02-16 19:56:22 +01:00
}
ax
}