Skip to contents

Parameters to configure axis settings in roboplots. Includes options for specifying axis columns, formatting, titles, limits, and font settings, as well as handling secondary y-axes.

Usage

set_axes(
  y = NULL,
  x = NULL,
  yticktype = NULL,
  xticktype = NULL,
  ytitle = "",
  xtitle = "",
  yformat = NULL,
  xformat = NULL,
  ylim = c(NA, NA),
  xlim = c(NA, NA),
  yfont = NULL,
  xfont = NULL,
  xangle = NULL,
  yangle = NULL,
  y2 = NULL,
  y2font = NULL,
  ylegend = NULL,
  y2title = "",
  y2legend = NULL,
  xanchor = NULL,
  xstep = NULL
)

Arguments

x, y

Character. Names of columns from d in roboplot for the axes. Defaults to "time" and "value".

xticktype, yticktype

Characters. Axis formatting: "character", "date", or "numeric". Defaults to "date" and "numeric".

xtitle, ytitle, y2title

Character. Titles for the axes.

xformat, yformat

Character. Formatting for axis tick text. Use d3 time format for dates and d3 number format for numbers.

xlim, ylim

Vector of length 2. Axis limits. Should match the axis values in the plot by type.

xfont, yfont, y2font

Functions. Use set_font(). Secondary y-axis uses the main y-axis font size but allows separate family and color.

xangle, yangle

Numeric. Angle for axis tick text.

y2

Character vector. Observations from color in plots using a secondary y-axis.

ylegend, y2legend

Characters. Labels for legend title when y2 is given.

xanchor

Date. Only usable for date x-axes. Sets the point at which tick marks are drawn from. Must provide xstep.

xstep

Numeric. The interval of tick marks for date x-axes in months. Must provide xstart.

Value

List of class roboplotr.set_axes

Examples

# The primary usage is for creating horizontal bar plots when combining the
# roboplotr::roboplot plot_axes control with plot_mode "horizontal".

set_roboplot_options(caption_template = "Lähde: {text}.")

d <- energiantuonti |>
  dplyr::filter(Alue %in% c("USA","Norja","Iso-Britannia"))
attr(d, "frequency") <- "Quarterly"

# Control the plot axes with roboplotr::set_axes, setting the columns
# from roboplotr::roboplot variable 'd' as the axis data sources with 'x' and
# 'y', and possible define ticktypes with 'xticktype' and 'yticktype'. The latter
# is not mandatory, though.
d |> dplyr::filter(time == max(time)) |>
  roboplot(Suunta,
           stringr::str_glue("Energian tuonti {lubridate::year(max(d$time))}"),
           "Milj. €","Tilastokeskus",
           plot_type = "bar",
           plot_mode = "horizontal",
           plot_axes = set_axes(
             y = "Alue",
             yticktype = "character",
             x = "value",
             xticktype = "numeric")
  )
# You can use `xtitle` and `ytitle` to provide labeling for axis titles, # and control the axis format with d3 time (https://d3js.org/api#d3-time) # and number (https://d3js.org/api#d3-format) -formatted vectors for `xlim` # and 'ylim'. These are not typechecked by `roboplot()`, but must match the # corresponding axis values. Alter axis tick labels' angle with `xangle` and `yangle`. d |> dplyr::filter(Suunta == "Tuonti") |> roboplot(Alue,"Energian tuonti","Milj. €","Tilastokeskus", plot_axes = set_axes( ytitle = "Arvo", xformat = "Y%y", yformat = "+$05,", ylim = c(-200,1500), xlim = c("2015-01-01","2023-01-01"), yangle = 45, xangle = 66 ) )
# You can use `set_axes(xanchor, xstep)` to control xaxis ticks for date axes. Use # `xanchor` to set the point at which tick marks are drawn from, and `xstep` to set the # interval in months. Use NA for numeric axis limits to derive one end from data. d |> dplyr::filter(Suunta == "Tuonti") |> roboplot(Alue,"Energian tuonti","Milj. €","Tilastokeskus", plot_axes = set_axes( xanchor = as.Date("2020-01-01"), xstep = 12, ylim = c(-120, NA) ) )
# Additionally, you may use logartihmic axis for any numeric variable used in # `plot_axes```. d |> dplyr::filter(Suunta == "Tuonti") |> roboplot(Alue,"Energian tuonti","Milj. €","Tilastokeskus", plot_axes = set_axes(yticktype = "log") )
# Providing a vector of strings matching observations from `roboplot()` param # `color` as `y2` will add a secondary y-axis. Any provided `zeroline`does not # work for both axes at the time. d |> dplyr::filter(Suunta == "Tuonti", Alue %in% c("Iso-Britannia","Norja")) |> roboplot(Alue, "Energian tuonti", "Milj. \u20AC", "Tilastokeskus", plot_axes = set_axes(y2 = "Iso-Britannia"))
# Giving no observations that match param `color` in `roboplot()` fails. d2 <- d |> dplyr::filter(Suunta == "Tuonti", Alue %in% c("Iso-Britannia", "Norja")) if (FALSE) { # \dontrun{ d2 |> roboplot(Alue, "Energian tuonti", "Milj. \u20AC", "Tilastokeskus", plot_axes = set_axes(y2 = "USA")) } # } # You might want to override the default axis reference labels that are added # to legend. Using them without giving any items in `y2` does nothing. You probably # want to explicitly reorder the observations. d2 |> dplyr::mutate(Alue = forcats::fct_relevel(Alue, "Iso-Britannia","Norja")) |> roboplot( Alue, "Energian tuonti", "Milj. \u20AC", "Tilastokeskus", plot_axes = set_axes( y2 = "Iso-Britannia", ylegend = "1.", y2legend = "2." ) )
# Or maybe you only have two observations and you want to match them by color # instead of labeling. d2 |> dplyr::mutate(Alue = forcats::fct_relevel(Alue, "Iso-Britannia","Norja")) |> roboplot( Alue, "Energian tuonti", "Milj. \u20AC", "Tilastokeskus", plot_axes = set_axes( y2 = "Iso-Britannia", yfont = set_font(color = getOption("roboplot.colors.traces")[2]), y2font = set_font(color = getOption("roboplot.colors.traces")[1]), ylegend = "", y2legend = "" ) )
# Or you could use the axis titles to differentiate between the observations. # This might be especially appropriate if the values are on a completely different # scale. d2 |> dplyr::mutate(Alue = forcats::fct_relevel(Alue, "Iso-Britannia","Norja")) |> dplyr::mutate(value = ifelse(Alue == "Norja", value / 1000, value)) |> roboplot( Alue, "Energian tuonti", caption = "Tilastokeskus", plot_axes = set_axes( y2 = "Iso-Britannia", ytitle = "Norja, Mrd. €", y2title = "Iso-Britannia, Milj €", ylegend = "", y2legend = "" ) )
# `set_axes()` also gives the user fine-grained control for plots where there # might not be data in a format that is directly transferable to date or numeric # format. d2 <- dplyr::tribble( ~time, ~value, "2021 Jan-Oct", 7.2, "Jan-Nov", 6.0, "Jan-Dec", 4.4, "2022 Jan-Feb", 3.7, "Jan-Mar", 0.7, "Jan-Apr", -2.7, "Jan-May", -4.0, "Jan-Jun", -5.4, "Jan-Jul", -6.4, "Jan-Aug", -7.4, "Jan-Sep", -8.0, "Jan-Oct", -8.8 ) set_roboplot_options( caption_template = "Source: {text}." ) d2 |> roboplotr::roboplot(title = "Growth Rate of Investment", caption = "National bureau of statistics, China", plot_axes = set_axes(xticktype = "character"))
# Or you might have numeric data on both axes. `roboplot()` will draw any # traces in the order they appear in the data, so it is up to the user to # order the traces properly. d3 <- dplyr::tibble(time = seq.Date(from = lubridate::floor_date( lubridate::today(), "years" )-lubridate::years(10), to = lubridate::floor_date(lubridate::today(),"years"), by = "year"), obs1 = round(runif(11, min = 0, max = 120),1), value = round(runif(11, min = -120, max = 120),1)) d3 |> dplyr::arrange(obs1, value) |> roboplotr::roboplot(title = "Random values", caption = "Robonomist", plot_axes = set_axes( x = "obs1", xticktype = "numeric"))
set_roboplot_options( caption_template = "Lähde {text}." ) # You might just want to switch the axes for time and value d |> dplyr::filter(Suunta == "Tuonti") |> roboplotr::roboplot(Alue, "Energian tuonti","Milj. €", "Tilastokeskus", plot_axes = set_axes( y = "time", yticktype = "date", x = "value", xticktype = "numeric" ))
# Or you might want to draw character strings on some axis. d |> dplyr::filter(Suunta == "Tuonti") |> roboplotr::roboplot(Alue, "Energian tuonti","Milj. €", "Tilastokeskus", plot_axes = set_axes( y = "Alue", yticktype = "character", x = "value", xticktype = "numeric" ))
# Making a reasonable line plot like this is a challenge, though, and you are # better off with a horizontal bar plot. d |> dplyr::filter(Suunta == "Tuonti", time == max(time)) |> roboplotr::roboplot(Alue, "Energian tuonti","Milj. €", "Tilastokeskus", plot_type = "bar", plot_mode = "horizontalfill", plot_axes = set_axes( y = "Alue", yticktype = "character", x = "value", xticktype = "numeric" ))
# Revert to defaults: set_roboplot_options(reset = TRUE)