Set up an external menu for filtering plot data based on a categorical variable. This function creates a configuration for an external menu that can be used to filter plot data based on a categorical variable. The menu will be rendered as a draggable overlay on the plot, with checkboxes for each unique value of the specified variable. The menu can be toggled from the modebar.
Source:R/externalmenu.R
set_externalmenu.RdSet up an external menu for filtering plot data based on a categorical variable. This function creates a configuration for an external menu that can be used to filter plot data based on a categorical variable. The menu will be rendered as a draggable overlay on the plot, with checkboxes for each unique value of the specified variable. The menu can be toggled from the modebar.
Usage
set_externalmenu(
col = NULL,
title = TRUE,
box = set_infobox(background = getOption("roboplot.colors.background")),
btn = set_infobox(),
max_items = NULL,
selected = NULL
)Arguments
- col
Symbol or character. Column from
dofroboplot()that contains the categorical variable for filtering. This column will be used to create the checkboxes in the menu.- title
Logical. Whether to include a title in the menu. Default is TRUE.
- box
A list of styling options for the menu box. Use
set_infobox()to create this list.- btn
A list of styling options for the box selector. Use
set_infobox()to create this list.- max_items
Numeric. Maximum number of items that can be selected at once. If NULL (default), there is no limit.
- selected
Character vector. Initial selection of items in the menu. If NULL (default), all items are selected initially. If
max_itemsis set, the initial selection will be trimmed to the firstmax_itemsvalues found in the data.
Examples
# You might want to make items selectable outside of the legend selection. Use
# the parameter `externalmenu` with `set_externalmenu()` to provide the column
# that controls visiblity of items in the plot available from the `roboplot`
# modebar. See more examples in the `set_externalmenu()` documentation.
energiantuonti |>
dplyr::filter(time == max(time)) |>
roboplot(Suunta, plot_type = "bar", plot_mode = "horizontal",
plot_axes = set_axes(x = "value", y = "Alue"),
externalmenu = set_externalmenu(Alue)
)
# You might want to make items selectable outside of the legend selection. Use
# the parameter `externalmenu` with `set_externalmenu()` to provide the column
# that controls visiblity of items in the plot. This way you can construct,
# for example, a horizontal bar plot where you have the legend controlling
# the color variable, but still filter the items in the y-axis through the
# external menu, available from the `roboplot` modebar.
energiantuonti |>
dplyr::filter(time == max(time)) |>
roboplot(Suunta, plot_type = "bar", plot_mode = "horizontal",
plot_axes = set_axes(x = "value", y = "Alue"),
externalmenu = set_externalmenu(Alue)
)
# You can combine external menu with pattern to have the pattern variable
# controlled by the external menu.
energiantuonti |>
roboplot(Alue,
pattern = Suunta,
externalmenu = set_externalmenu(Suunta)
)
# You could also use the external menu to control the visibility of items in the
# legend to save space on the plot itself. This is not as obvious to user and
# the external menu won't have the legend colors available, but can be useful
# in some situations.
energiantuonti |>
dplyr::filter(Suunta == "Tuonti") |>
roboplot(Alue,
legend = set_legend(position = "none"),
externalmenu = set_externalmenu(Alue)
)
# Any arbitary column works, but the results in the plot are up to the user.
energiantuonti |>
dplyr::filter(Suunta == "Vienti") |>
dplyr::mutate(menu = purrr::map_chr(Alue, ~ sample(c("a","b","c","d"),1))) |>
roboplot(Alue,
externalmenu = set_externalmenu(menu)
)
# You can even filter for the dates, but remember that the external menu is
# not meant for filtering the data, but rather for controlling the visibility
# of items in the plot. If you want to filter the data, it's better to do it
# before plotting, or use something more appropriate like a rangeslider.
energiantuonti |>
dplyr::filter(Suunta == "Vienti") |>
roboplot(Alue,
externalmenu = set_externalmenu(time),
rangeslider = TRUE
)
# Or at least construct a reasonable filter for an appropriate plot.
energiantuonti |>
dplyr::filter(Suunta == "Vienti", Alue %in% c("Venäjä", "Kanada")) |>
dplyr::mutate(Vuosi = lubridate::year(time)) |>
roboplot(Alue,
plot_type = "bar",
externalmenu = set_externalmenu(Vuosi)
)
# Or some other arbitary grouping
energiantuonti |>
dplyr::filter(Suunta == "Vienti") |>
dplyr::mutate(Suunta = ifelse(Alue %in% c("Venäjä","Viro","Latvia"), "Itä", "Länsi")) |>
roboplot(Alue,
externalmenu = set_externalmenu(Suunta))
# You can control the appearance of the externalmenu somewhat by using
# `set_infobox()` with set_externalmenu(box, btn), or omit the title with
# `set_externalmenu(title = FALSE)`.
energiantuonti |>
dplyr::filter(Suunta == "Tuonti") |>
roboplot(
Alue,
legend = set_legend(position = "none"),
externalmenu = set_externalmenu(
Alue,
title = FALSE,
box = set_infobox(background = "orange"),
btn = set_infobox(
border = "brown",
border_width = 4,
background = "red"
)
)
)
# Technically, nothing prevents you from using the external menu along with
# legend, but it might be quite confusing for the user.
energiantuonti |>
dplyr::filter(Suunta == "Vienti") |>
roboplot(Alue,
externalmenu = set_externalmenu(Alue)
)