Skip to contents

Parameters to customize legend in roboplots and robomaps.

Usage

set_legend(...)

set_plot_legend(
  position = NULL,
  orientation = NULL,
  maxwidth = NULL,
  title = FALSE,
  tidy = getOption("roboplot.legend.tidy"),
  ...
)

set_map_legend(
  breaks = 5,
  position = "bottomleft",
  orientation = "vertical",
  title = NULL,
  opacity = 1,
  labformat = NULL,
  gradient = NULL,
  ...
)

Arguments

...

Arguments passed on to set_plot_legend, set_map_legend

position

Character. Either "bottom" or NA for no legend for roboplot, or one of "bottomleft", "bottomright", or "none" for robomap. On a roboplot, the legend is removed on default if column in data for param d of the roboplot that is defined by param color of that plot has only one observation.

orientation

Character. Currently unused.

maxwidth

Numeric. All roboplot legend items (and y-axis values for horizontal barplots) longer than this will be trunctated with an ellipsis.

title

Logical or character. TRUE if you want the parameter color from the roboplot to be the legend title. Character if you want to provide your own legend title.

tidy

Logical. Controls whether the roboplot legend items will have matching widths across columns. Default is FALSE.

breaks

Numeric vector. A length 1 vector attempts to make that many robomap legend entries. Length 2+ vector should be the breaks in values of param d of robomap where the legend should be split.

opacity

Numeric. The opacity of the robomap legend, ranging from 0 to 1.

labformat

Function. Specify custom label formatting function. The function must take a numeric vector of labels the map legend might have. Mostly useful for small number of specific labels.

gradient

Logical. If TRUE, the legend will be a gradient. Default TRUE for numeric data, FALSE for factor data.

Value

A list of class roboplotr.set_legend

Examples

# You can use `set_legend` to control how legends are displayed on a `roboplot()`.

d <- energiantuonti |>
  dplyr::filter(Alue == "Venäjä")

# Use `position` NA to remove the legend.
d |>
  roboplot(Suunta, legend = set_legend(position = NA))
#> Error in map(split_d, function(g) {    tracetype <- unique(g$roboplot.plot.type)    trace_labels <- roboplotr_trace_labels(tracetype, labels,         names(d))    hoverlab <- case_when(tracetype == "pie" ~ "label", any(c("horizontal",         "horizontalfill", "horizontalstack") %in% g$roboplot.plot.type) &         is.null(pattern) & quo_name(color) != ticktypes$y ~ "text",         TRUE ~ "customdata")    hovertemplate <- roboplotr_hovertemplate(hovertext, lab = hoverlab,         ticktypes)    if (any(unique(pull(g, !!color)) %in% secondary_yaxis)) {        legend_rank <- mean(g$roboplot.legend.rank) + max(d$roboplot.legend.rank)    }    else {        legend_rank <- mean(g$roboplot.legend.rank)    }    .fontsize <- getOption("roboplot.font.main")$size    g <- mutate(g, roboplot.bg.color = roboplotr_alter_color(.data$roboplot.trace.color,         "dark"), roboplot.tx.color = roboplotr_text_color_picker(.data$roboplot.bg.color,         .fontsize))    text_inside <- any(tracetype == "pie" & trace_labels$style !=         "none", tracetype == "bar" & trace_labels$style %in%         c("mini", "auto", "inside"))    if (text_inside) {        if (trace_labels$style == "mini") {            g <- mutate(g, roboplot.in.tx.color = "#FFFFFF00")        }        else {            g <- mutate(g, roboplot.in.tx.color = roboplotr_text_color_picker(.data$roboplot.trace.color,                 getOption("roboplot.font.caption")$size))        }    }    if (!is.null(attributes(g)$roboplot.confidence.area)) {        show.legend <- attributes(g)$roboplot.confidence.area$show_legend        g <- mutate(g, roboplot.confidence.label = str_c(.data$roboplot.plot.text,             "\n", .data[[attributes(g)$roboplot.confidence.area$col]]),             roboplot.plot.text = .data[[attributes(g)$roboplot.confidence.area$col]],             )        if (!is.character(g[[attributes(g)$roboplot.confidence.area$col]])) {            legend_rank <- legend_rank + 10 + as.numeric(unique(g[[attributes(g)$roboplot.confidence.area$col]]))        }        else {            legend_rank <- legend_rank + 10 + 1        }    }    else {        if (is.null(highlight)) {            if (!is.null(pattern_showlegend) & trace_showlegend) {                show.legend <- pattern_showlegend[as.character(unique(g[[as_name(pattern)]]))]            }            else {                show.legend <- trace_showlegend            }        }        else {            show.legend <- roboplotr_highlight_legend(highlight,                 g)        }    }    .legendgrouptitle <- NULL    if ("pie" %in% plot_type) {        .legendgroup <- "pie"    }    else {        .legendgroup <- as.character(unique(pull(g, {            {                color            }        })))    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            .legendgrouptitle <- ticktypes$y2legend            .legendgroup <- "R"        }        else {            .legendgrouptitle <- ticktypes$ylegend            .legendgroup <- "L"        }    }    error_x <- NULL    error_y <- NULL    if (!is.null(confidence$type)) {        if (confidence$type == "bars") {            if (!quo_is_null(confidence$error_x)) {                error_x <- list(array = confidence$error_x, color = confidence$xcolor)            }            if (!quo_is_null(confidence$error_y)) {                error_y <- list(array = g[["roboplot.errorbar.max"]],                   arrayminus = g[["roboplot.errorbar.min"]],                   color = confidence$ycolor)            }        }    }    if (is.null(updatemenu)) {        trace_visible <- T    }    else {        trace_visible <- unique(g$roboplot.update.menu) == updatemenu$selected    }    plotting_params <- list(color = color, constraintext = ifelse(!is.null(trace_labels$size),         "none", "both"), customdata = if (!is.null(attributes(g)$roboplot.confidence.area)) {        ~roboplot.confidence.label    } else if (!quo_is_null(hovertext$col)) {        as.formula(str_c("~", as_label(hovertext$col)))    } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%         g$roboplot.plot.mode)) {        ~roboplot.horizontal.label    } else {        ~roboplot.plot.text    }, data = g, direction = "clockwise", error_x = error_x,         error_y = error_y, fill = unlist(ifelse(!is.null(attributes(g)$roboplot.confidence.area),             list("toself"), list(NULL))), fillcolor ~ color,         hoverlabel = list(bgcolor = ~roboplot.bg.color, bordercolor = first(unlist(unique(getOption("roboplot.grid")[c("xcolor",             "ycolor")]))), font = ~append(getOption("roboplot.font.main")[c("family",             "size")], list(color = roboplot.tx.color))), hoveron = "points+fills",         hovertemplate = hovertemplate, insidetextfont = list(family = getOption("roboplot.font.main")$family,             size = trace_labels$size %||% getOption("roboplot.font.main")$size,             color = ~roboplot.in.tx.color), labels = color, legendgroup = .legendgroup,         legendgrouptitle = list(text = .legendgrouptitle, font = getOption("roboplot.font.main")[c("color",             "family", "size")]), legendrank = legend_rank, line = ~list(width = roboplot.linewidth,             dash = roboplot.dash, smoothing = attributes(g)$roboplot.confidence.area$smoothing %||%                 0.5, shape = case_when(!is.null(attributes(g)$roboplot.confidence.area) ~                 "spline", roboplot.plot.mode == "step" ~ "hv",                 roboplot.plot.mode == "smooth" ~ "spline", TRUE ~                   "linear")), marker = list(colors = ~roboplot.trace.color,             line = getOption("roboplot.trace.border"), symbol = markers$symbol,             size = markers$size, pattern = ~list(shape = roboplot.pattern)),         mode = {            .mode <- str_replace_all(unique(g$roboplot.plot.mode),                 c(`^smooth$` = "lines", `^line$` = "lines", `^step$` = "lines",                   `^scatter$` = "markers", `scatter\\+line` = "markers+lines"))            if (tracetype == "scatter" & trace_labels$style !=                 "none") {                .mode <- str_c(.mode, "+text")            }            .mode        }, name = ~if (!is.null(legend$maxwidth)) {            str_trunc(as.character(roboplot.plot.text), legend$maxwidth)        } else {            roboplot.plot.text        }, offset = if ("horizontalfill" %in% g$roboplot.plot.mode) {            ~roboplot.bar.offset        } else {            NULL        }, offsetgroup = ~str_c(roboplot.pattern, roboplot.trace.color),         opacity = attributes(g)$roboplot.confidence.area$opacity,         orientation = ifelse(any(c("horizontal", "horizontalfill",             "horizontalstack") %in% g$roboplot.plot.mode) & "bar" %in%             g$roboplot.plot.type, "h", "v"), rotation = rotation,         showlegend = show.legend, sort = F, text = if (any(c("horizontal",             "horizontalfill", "horizontalstack") %in% g$roboplot.plot.mode)) {            ~roboplot.horizontal.label        } else {            ~roboplot.plot.text        }, textfont = list(color = ~trace_labels$color %||% roboplot.trace.color,             size = ~trace_labels$size %||% getOption("roboplot.font.main")$size),         textinfo = "percent", textposition = {            if (tracetype == "scatter" & trace_labels$style ==                 "auto") {                .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                   2, 0))                str_replace_all(as.character(tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                   .y[2], 1, -1), sign(diff(.y))), 0)), value)$value),                   c(`-1` = "bottom center", `1` = "top center"))            } else {                case_when(trace_labels$style == "none" ~ "none",                   tracetype == "bar" & trace_labels$style ==                     "mini" ~ "auto", tracetype == "bar" ~ trace_labels$style,                   tracetype == "scatter" & trace_labels$style ==                     "last" ~ "middle right", tracetype == "scatter" &                     trace_labels$style == "auto" ~ "top center",                   tracetype == "pie" ~ "inside", TRUE ~ "none")            }        }, texttemplate = if (!quo_is_null(trace_labels$text_col)) {            if (trace_labels$style == "last") {                .y <- g[[as_label(trace_labels$text_col)]]                .y[-length(.y)] <- ""                .y            } else {                as.formula(str_c("~", as_label(trace_labels$text_col)))            }        } else if (tracetype == "pie") {            NULL        } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%             g$roboplot.plot.mode)) {            "%{x:,.1f}"        } else if (tracetype == "scatter" & trace_labels$style ==             "auto") {            .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                 2, 0))            .p <- tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                 .y[2], 1, -1), sign(diff(.y))), 0)), value)$value            .y <- pull(mutate(tidyr::fill(tibble(check = na_if(c(ifelse(sign(.y[2] -                 .y[1]) < 0, -1, 1), sign(diff(.y))), 0)), check),                 check = slider::slide_dbl(check, ~.x[[1]] !=                   .x[[2]], .after = 1, .complete = T)), check)            .y[c(1, length(.y))] <- 1            .y <- roboplotr_format_robotable_numeric(ifelse(.y,                 g[[ticktypes$y]], NA), rounding = max(hovertext$rounding -                 1, 0), na_value = "")            ifelse(.p > 0, str_c(.y, "\n"), str_c("\n", .y))        } else if (trace_labels$style == "last") {            .y <- g[[ticktypes$y]]            .y[-length(.y)] <- ""            .y        } else {            "%{y:,.1f}"        }, type = ~tracetype, values = as.formula(str_c("~",             ticktypes$y)), visible = trace_visible, width = if ("horizontalfill" %in%             g$roboplot.plot.mode) {            ~roboplot.bar.width        } else {            NULL        }, x = as.formula(str_c("~", ticktypes$x)), xhoverformat = ifelse(ticktypes$xticktype ==             "date", ticktypes$dateformat, ""), y = as.formula(str_c("~",             ifelse(!is.null(legend$maxwidth) & ticktypes$y !=                 "value", "roboplot.trunc", ticktypes$y))))    shared_params <- c("customdata", "data", "error_x", "error_y",         "hoverlabel", "hovertemplate", "legendgroup", "legendgrouptitle",         "legendrank", "showlegend", "text", "textposition", "texttemplate",         "type", "visible")    if (!is.null(attributes(g)$roboplot.confidence.area)) {        shared_params <- c(shared_params, "fill", "fillcolor",             "opacity", "hoveron")    }    if (text_inside) {        shared_params <- c(shared_params, "insidetextfont")    }    if (!is.null(trace_labels$color) | !is.null(trace_labels$size)) {        shared_params <- c(shared_params, "textfont")    }    if (!is.null(trace_labels$size) & tracetype == "bar") {        shared_params <- c(shared_params, "constraintext")    }    if (tracetype %in% "scatter" & any(c("line", "step", "smooth") %in%         g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter+line" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "marker", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "mode", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "bar" & any(c("horizontal", "horizontalfill",         "horizontalstack") %in% g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "orientation", "offset", "width",             "color", "name", "marker")]    }    else if (tracetype == "bar") {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "pie") {        plotting_params <- plotting_params[c(shared_params, "labels",             "textinfo", "direction", "rotation", "sort", "marker",             "values")]    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            plotting_params$yaxis <- "y2"        }    }    plotting_params}):  In index: 1.
#> Caused by error in `na_if()`:
#> ! could not find function "na_if"

# `roboplot()` omits the legend to give more space for the plot area, when there
# is only a single trace for `color`. Use `position` to force the legend to show.
# You can also use `title` to set a title.

d |>
  dplyr::filter(Suunta == "Tuonti") |>
  roboplot(Suunta, legend = set_legend(title = "Example", position = "bottom"))
#> Error in map(split_d, function(g) {    tracetype <- unique(g$roboplot.plot.type)    trace_labels <- roboplotr_trace_labels(tracetype, labels,         names(d))    hoverlab <- case_when(tracetype == "pie" ~ "label", any(c("horizontal",         "horizontalfill", "horizontalstack") %in% g$roboplot.plot.type) &         is.null(pattern) & quo_name(color) != ticktypes$y ~ "text",         TRUE ~ "customdata")    hovertemplate <- roboplotr_hovertemplate(hovertext, lab = hoverlab,         ticktypes)    if (any(unique(pull(g, !!color)) %in% secondary_yaxis)) {        legend_rank <- mean(g$roboplot.legend.rank) + max(d$roboplot.legend.rank)    }    else {        legend_rank <- mean(g$roboplot.legend.rank)    }    .fontsize <- getOption("roboplot.font.main")$size    g <- mutate(g, roboplot.bg.color = roboplotr_alter_color(.data$roboplot.trace.color,         "dark"), roboplot.tx.color = roboplotr_text_color_picker(.data$roboplot.bg.color,         .fontsize))    text_inside <- any(tracetype == "pie" & trace_labels$style !=         "none", tracetype == "bar" & trace_labels$style %in%         c("mini", "auto", "inside"))    if (text_inside) {        if (trace_labels$style == "mini") {            g <- mutate(g, roboplot.in.tx.color = "#FFFFFF00")        }        else {            g <- mutate(g, roboplot.in.tx.color = roboplotr_text_color_picker(.data$roboplot.trace.color,                 getOption("roboplot.font.caption")$size))        }    }    if (!is.null(attributes(g)$roboplot.confidence.area)) {        show.legend <- attributes(g)$roboplot.confidence.area$show_legend        g <- mutate(g, roboplot.confidence.label = str_c(.data$roboplot.plot.text,             "\n", .data[[attributes(g)$roboplot.confidence.area$col]]),             roboplot.plot.text = .data[[attributes(g)$roboplot.confidence.area$col]],             )        if (!is.character(g[[attributes(g)$roboplot.confidence.area$col]])) {            legend_rank <- legend_rank + 10 + as.numeric(unique(g[[attributes(g)$roboplot.confidence.area$col]]))        }        else {            legend_rank <- legend_rank + 10 + 1        }    }    else {        if (is.null(highlight)) {            if (!is.null(pattern_showlegend) & trace_showlegend) {                show.legend <- pattern_showlegend[as.character(unique(g[[as_name(pattern)]]))]            }            else {                show.legend <- trace_showlegend            }        }        else {            show.legend <- roboplotr_highlight_legend(highlight,                 g)        }    }    .legendgrouptitle <- NULL    if ("pie" %in% plot_type) {        .legendgroup <- "pie"    }    else {        .legendgroup <- as.character(unique(pull(g, {            {                color            }        })))    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            .legendgrouptitle <- ticktypes$y2legend            .legendgroup <- "R"        }        else {            .legendgrouptitle <- ticktypes$ylegend            .legendgroup <- "L"        }    }    error_x <- NULL    error_y <- NULL    if (!is.null(confidence$type)) {        if (confidence$type == "bars") {            if (!quo_is_null(confidence$error_x)) {                error_x <- list(array = confidence$error_x, color = confidence$xcolor)            }            if (!quo_is_null(confidence$error_y)) {                error_y <- list(array = g[["roboplot.errorbar.max"]],                   arrayminus = g[["roboplot.errorbar.min"]],                   color = confidence$ycolor)            }        }    }    if (is.null(updatemenu)) {        trace_visible <- T    }    else {        trace_visible <- unique(g$roboplot.update.menu) == updatemenu$selected    }    plotting_params <- list(color = color, constraintext = ifelse(!is.null(trace_labels$size),         "none", "both"), customdata = if (!is.null(attributes(g)$roboplot.confidence.area)) {        ~roboplot.confidence.label    } else if (!quo_is_null(hovertext$col)) {        as.formula(str_c("~", as_label(hovertext$col)))    } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%         g$roboplot.plot.mode)) {        ~roboplot.horizontal.label    } else {        ~roboplot.plot.text    }, data = g, direction = "clockwise", error_x = error_x,         error_y = error_y, fill = unlist(ifelse(!is.null(attributes(g)$roboplot.confidence.area),             list("toself"), list(NULL))), fillcolor ~ color,         hoverlabel = list(bgcolor = ~roboplot.bg.color, bordercolor = first(unlist(unique(getOption("roboplot.grid")[c("xcolor",             "ycolor")]))), font = ~append(getOption("roboplot.font.main")[c("family",             "size")], list(color = roboplot.tx.color))), hoveron = "points+fills",         hovertemplate = hovertemplate, insidetextfont = list(family = getOption("roboplot.font.main")$family,             size = trace_labels$size %||% getOption("roboplot.font.main")$size,             color = ~roboplot.in.tx.color), labels = color, legendgroup = .legendgroup,         legendgrouptitle = list(text = .legendgrouptitle, font = getOption("roboplot.font.main")[c("color",             "family", "size")]), legendrank = legend_rank, line = ~list(width = roboplot.linewidth,             dash = roboplot.dash, smoothing = attributes(g)$roboplot.confidence.area$smoothing %||%                 0.5, shape = case_when(!is.null(attributes(g)$roboplot.confidence.area) ~                 "spline", roboplot.plot.mode == "step" ~ "hv",                 roboplot.plot.mode == "smooth" ~ "spline", TRUE ~                   "linear")), marker = list(colors = ~roboplot.trace.color,             line = getOption("roboplot.trace.border"), symbol = markers$symbol,             size = markers$size, pattern = ~list(shape = roboplot.pattern)),         mode = {            .mode <- str_replace_all(unique(g$roboplot.plot.mode),                 c(`^smooth$` = "lines", `^line$` = "lines", `^step$` = "lines",                   `^scatter$` = "markers", `scatter\\+line` = "markers+lines"))            if (tracetype == "scatter" & trace_labels$style !=                 "none") {                .mode <- str_c(.mode, "+text")            }            .mode        }, name = ~if (!is.null(legend$maxwidth)) {            str_trunc(as.character(roboplot.plot.text), legend$maxwidth)        } else {            roboplot.plot.text        }, offset = if ("horizontalfill" %in% g$roboplot.plot.mode) {            ~roboplot.bar.offset        } else {            NULL        }, offsetgroup = ~str_c(roboplot.pattern, roboplot.trace.color),         opacity = attributes(g)$roboplot.confidence.area$opacity,         orientation = ifelse(any(c("horizontal", "horizontalfill",             "horizontalstack") %in% g$roboplot.plot.mode) & "bar" %in%             g$roboplot.plot.type, "h", "v"), rotation = rotation,         showlegend = show.legend, sort = F, text = if (any(c("horizontal",             "horizontalfill", "horizontalstack") %in% g$roboplot.plot.mode)) {            ~roboplot.horizontal.label        } else {            ~roboplot.plot.text        }, textfont = list(color = ~trace_labels$color %||% roboplot.trace.color,             size = ~trace_labels$size %||% getOption("roboplot.font.main")$size),         textinfo = "percent", textposition = {            if (tracetype == "scatter" & trace_labels$style ==                 "auto") {                .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                   2, 0))                str_replace_all(as.character(tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                   .y[2], 1, -1), sign(diff(.y))), 0)), value)$value),                   c(`-1` = "bottom center", `1` = "top center"))            } else {                case_when(trace_labels$style == "none" ~ "none",                   tracetype == "bar" & trace_labels$style ==                     "mini" ~ "auto", tracetype == "bar" ~ trace_labels$style,                   tracetype == "scatter" & trace_labels$style ==                     "last" ~ "middle right", tracetype == "scatter" &                     trace_labels$style == "auto" ~ "top center",                   tracetype == "pie" ~ "inside", TRUE ~ "none")            }        }, texttemplate = if (!quo_is_null(trace_labels$text_col)) {            if (trace_labels$style == "last") {                .y <- g[[as_label(trace_labels$text_col)]]                .y[-length(.y)] <- ""                .y            } else {                as.formula(str_c("~", as_label(trace_labels$text_col)))            }        } else if (tracetype == "pie") {            NULL        } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%             g$roboplot.plot.mode)) {            "%{x:,.1f}"        } else if (tracetype == "scatter" & trace_labels$style ==             "auto") {            .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                 2, 0))            .p <- tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                 .y[2], 1, -1), sign(diff(.y))), 0)), value)$value            .y <- pull(mutate(tidyr::fill(tibble(check = na_if(c(ifelse(sign(.y[2] -                 .y[1]) < 0, -1, 1), sign(diff(.y))), 0)), check),                 check = slider::slide_dbl(check, ~.x[[1]] !=                   .x[[2]], .after = 1, .complete = T)), check)            .y[c(1, length(.y))] <- 1            .y <- roboplotr_format_robotable_numeric(ifelse(.y,                 g[[ticktypes$y]], NA), rounding = max(hovertext$rounding -                 1, 0), na_value = "")            ifelse(.p > 0, str_c(.y, "\n"), str_c("\n", .y))        } else if (trace_labels$style == "last") {            .y <- g[[ticktypes$y]]            .y[-length(.y)] <- ""            .y        } else {            "%{y:,.1f}"        }, type = ~tracetype, values = as.formula(str_c("~",             ticktypes$y)), visible = trace_visible, width = if ("horizontalfill" %in%             g$roboplot.plot.mode) {            ~roboplot.bar.width        } else {            NULL        }, x = as.formula(str_c("~", ticktypes$x)), xhoverformat = ifelse(ticktypes$xticktype ==             "date", ticktypes$dateformat, ""), y = as.formula(str_c("~",             ifelse(!is.null(legend$maxwidth) & ticktypes$y !=                 "value", "roboplot.trunc", ticktypes$y))))    shared_params <- c("customdata", "data", "error_x", "error_y",         "hoverlabel", "hovertemplate", "legendgroup", "legendgrouptitle",         "legendrank", "showlegend", "text", "textposition", "texttemplate",         "type", "visible")    if (!is.null(attributes(g)$roboplot.confidence.area)) {        shared_params <- c(shared_params, "fill", "fillcolor",             "opacity", "hoveron")    }    if (text_inside) {        shared_params <- c(shared_params, "insidetextfont")    }    if (!is.null(trace_labels$color) | !is.null(trace_labels$size)) {        shared_params <- c(shared_params, "textfont")    }    if (!is.null(trace_labels$size) & tracetype == "bar") {        shared_params <- c(shared_params, "constraintext")    }    if (tracetype %in% "scatter" & any(c("line", "step", "smooth") %in%         g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter+line" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "marker", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "mode", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "bar" & any(c("horizontal", "horizontalfill",         "horizontalstack") %in% g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "orientation", "offset", "width",             "color", "name", "marker")]    }    else if (tracetype == "bar") {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "pie") {        plotting_params <- plotting_params[c(shared_params, "labels",             "textinfo", "direction", "rotation", "sort", "marker",             "values")]    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            plotting_params$yaxis <- "y2"        }    }    plotting_params}):  In index: 1.
#> Caused by error in `na_if()`:
#> ! could not find function "na_if"

# Legend title is distinct from axis-specific legend titles which are controlled
# by `set_axes()` parameters `ylegend` and `y2legend`, when `y2` is used to move
# items from `color` to a secondary y-axis.

d |>
  roboplot(
    color = Suunta,
    plot_axes = set_axes(
      y2 = "Tuonti",
      ylegend = "Left yaxis",
      y2legend = "Right yaxis"
    ),
    legend = set_legend(title = "Example")
  )
#> Error in map(split_d, function(g) {    tracetype <- unique(g$roboplot.plot.type)    trace_labels <- roboplotr_trace_labels(tracetype, labels,         names(d))    hoverlab <- case_when(tracetype == "pie" ~ "label", any(c("horizontal",         "horizontalfill", "horizontalstack") %in% g$roboplot.plot.type) &         is.null(pattern) & quo_name(color) != ticktypes$y ~ "text",         TRUE ~ "customdata")    hovertemplate <- roboplotr_hovertemplate(hovertext, lab = hoverlab,         ticktypes)    if (any(unique(pull(g, !!color)) %in% secondary_yaxis)) {        legend_rank <- mean(g$roboplot.legend.rank) + max(d$roboplot.legend.rank)    }    else {        legend_rank <- mean(g$roboplot.legend.rank)    }    .fontsize <- getOption("roboplot.font.main")$size    g <- mutate(g, roboplot.bg.color = roboplotr_alter_color(.data$roboplot.trace.color,         "dark"), roboplot.tx.color = roboplotr_text_color_picker(.data$roboplot.bg.color,         .fontsize))    text_inside <- any(tracetype == "pie" & trace_labels$style !=         "none", tracetype == "bar" & trace_labels$style %in%         c("mini", "auto", "inside"))    if (text_inside) {        if (trace_labels$style == "mini") {            g <- mutate(g, roboplot.in.tx.color = "#FFFFFF00")        }        else {            g <- mutate(g, roboplot.in.tx.color = roboplotr_text_color_picker(.data$roboplot.trace.color,                 getOption("roboplot.font.caption")$size))        }    }    if (!is.null(attributes(g)$roboplot.confidence.area)) {        show.legend <- attributes(g)$roboplot.confidence.area$show_legend        g <- mutate(g, roboplot.confidence.label = str_c(.data$roboplot.plot.text,             "\n", .data[[attributes(g)$roboplot.confidence.area$col]]),             roboplot.plot.text = .data[[attributes(g)$roboplot.confidence.area$col]],             )        if (!is.character(g[[attributes(g)$roboplot.confidence.area$col]])) {            legend_rank <- legend_rank + 10 + as.numeric(unique(g[[attributes(g)$roboplot.confidence.area$col]]))        }        else {            legend_rank <- legend_rank + 10 + 1        }    }    else {        if (is.null(highlight)) {            if (!is.null(pattern_showlegend) & trace_showlegend) {                show.legend <- pattern_showlegend[as.character(unique(g[[as_name(pattern)]]))]            }            else {                show.legend <- trace_showlegend            }        }        else {            show.legend <- roboplotr_highlight_legend(highlight,                 g)        }    }    .legendgrouptitle <- NULL    if ("pie" %in% plot_type) {        .legendgroup <- "pie"    }    else {        .legendgroup <- as.character(unique(pull(g, {            {                color            }        })))    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            .legendgrouptitle <- ticktypes$y2legend            .legendgroup <- "R"        }        else {            .legendgrouptitle <- ticktypes$ylegend            .legendgroup <- "L"        }    }    error_x <- NULL    error_y <- NULL    if (!is.null(confidence$type)) {        if (confidence$type == "bars") {            if (!quo_is_null(confidence$error_x)) {                error_x <- list(array = confidence$error_x, color = confidence$xcolor)            }            if (!quo_is_null(confidence$error_y)) {                error_y <- list(array = g[["roboplot.errorbar.max"]],                   arrayminus = g[["roboplot.errorbar.min"]],                   color = confidence$ycolor)            }        }    }    if (is.null(updatemenu)) {        trace_visible <- T    }    else {        trace_visible <- unique(g$roboplot.update.menu) == updatemenu$selected    }    plotting_params <- list(color = color, constraintext = ifelse(!is.null(trace_labels$size),         "none", "both"), customdata = if (!is.null(attributes(g)$roboplot.confidence.area)) {        ~roboplot.confidence.label    } else if (!quo_is_null(hovertext$col)) {        as.formula(str_c("~", as_label(hovertext$col)))    } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%         g$roboplot.plot.mode)) {        ~roboplot.horizontal.label    } else {        ~roboplot.plot.text    }, data = g, direction = "clockwise", error_x = error_x,         error_y = error_y, fill = unlist(ifelse(!is.null(attributes(g)$roboplot.confidence.area),             list("toself"), list(NULL))), fillcolor ~ color,         hoverlabel = list(bgcolor = ~roboplot.bg.color, bordercolor = first(unlist(unique(getOption("roboplot.grid")[c("xcolor",             "ycolor")]))), font = ~append(getOption("roboplot.font.main")[c("family",             "size")], list(color = roboplot.tx.color))), hoveron = "points+fills",         hovertemplate = hovertemplate, insidetextfont = list(family = getOption("roboplot.font.main")$family,             size = trace_labels$size %||% getOption("roboplot.font.main")$size,             color = ~roboplot.in.tx.color), labels = color, legendgroup = .legendgroup,         legendgrouptitle = list(text = .legendgrouptitle, font = getOption("roboplot.font.main")[c("color",             "family", "size")]), legendrank = legend_rank, line = ~list(width = roboplot.linewidth,             dash = roboplot.dash, smoothing = attributes(g)$roboplot.confidence.area$smoothing %||%                 0.5, shape = case_when(!is.null(attributes(g)$roboplot.confidence.area) ~                 "spline", roboplot.plot.mode == "step" ~ "hv",                 roboplot.plot.mode == "smooth" ~ "spline", TRUE ~                   "linear")), marker = list(colors = ~roboplot.trace.color,             line = getOption("roboplot.trace.border"), symbol = markers$symbol,             size = markers$size, pattern = ~list(shape = roboplot.pattern)),         mode = {            .mode <- str_replace_all(unique(g$roboplot.plot.mode),                 c(`^smooth$` = "lines", `^line$` = "lines", `^step$` = "lines",                   `^scatter$` = "markers", `scatter\\+line` = "markers+lines"))            if (tracetype == "scatter" & trace_labels$style !=                 "none") {                .mode <- str_c(.mode, "+text")            }            .mode        }, name = ~if (!is.null(legend$maxwidth)) {            str_trunc(as.character(roboplot.plot.text), legend$maxwidth)        } else {            roboplot.plot.text        }, offset = if ("horizontalfill" %in% g$roboplot.plot.mode) {            ~roboplot.bar.offset        } else {            NULL        }, offsetgroup = ~str_c(roboplot.pattern, roboplot.trace.color),         opacity = attributes(g)$roboplot.confidence.area$opacity,         orientation = ifelse(any(c("horizontal", "horizontalfill",             "horizontalstack") %in% g$roboplot.plot.mode) & "bar" %in%             g$roboplot.plot.type, "h", "v"), rotation = rotation,         showlegend = show.legend, sort = F, text = if (any(c("horizontal",             "horizontalfill", "horizontalstack") %in% g$roboplot.plot.mode)) {            ~roboplot.horizontal.label        } else {            ~roboplot.plot.text        }, textfont = list(color = ~trace_labels$color %||% roboplot.trace.color,             size = ~trace_labels$size %||% getOption("roboplot.font.main")$size),         textinfo = "percent", textposition = {            if (tracetype == "scatter" & trace_labels$style ==                 "auto") {                .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                   2, 0))                str_replace_all(as.character(tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                   .y[2], 1, -1), sign(diff(.y))), 0)), value)$value),                   c(`-1` = "bottom center", `1` = "top center"))            } else {                case_when(trace_labels$style == "none" ~ "none",                   tracetype == "bar" & trace_labels$style ==                     "mini" ~ "auto", tracetype == "bar" ~ trace_labels$style,                   tracetype == "scatter" & trace_labels$style ==                     "last" ~ "middle right", tracetype == "scatter" &                     trace_labels$style == "auto" ~ "top center",                   tracetype == "pie" ~ "inside", TRUE ~ "none")            }        }, texttemplate = if (!quo_is_null(trace_labels$text_col)) {            if (trace_labels$style == "last") {                .y <- g[[as_label(trace_labels$text_col)]]                .y[-length(.y)] <- ""                .y            } else {                as.formula(str_c("~", as_label(trace_labels$text_col)))            }        } else if (tracetype == "pie") {            NULL        } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%             g$roboplot.plot.mode)) {            "%{x:,.1f}"        } else if (tracetype == "scatter" & trace_labels$style ==             "auto") {            .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                 2, 0))            .p <- tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                 .y[2], 1, -1), sign(diff(.y))), 0)), value)$value            .y <- pull(mutate(tidyr::fill(tibble(check = na_if(c(ifelse(sign(.y[2] -                 .y[1]) < 0, -1, 1), sign(diff(.y))), 0)), check),                 check = slider::slide_dbl(check, ~.x[[1]] !=                   .x[[2]], .after = 1, .complete = T)), check)            .y[c(1, length(.y))] <- 1            .y <- roboplotr_format_robotable_numeric(ifelse(.y,                 g[[ticktypes$y]], NA), rounding = max(hovertext$rounding -                 1, 0), na_value = "")            ifelse(.p > 0, str_c(.y, "\n"), str_c("\n", .y))        } else if (trace_labels$style == "last") {            .y <- g[[ticktypes$y]]            .y[-length(.y)] <- ""            .y        } else {            "%{y:,.1f}"        }, type = ~tracetype, values = as.formula(str_c("~",             ticktypes$y)), visible = trace_visible, width = if ("horizontalfill" %in%             g$roboplot.plot.mode) {            ~roboplot.bar.width        } else {            NULL        }, x = as.formula(str_c("~", ticktypes$x)), xhoverformat = ifelse(ticktypes$xticktype ==             "date", ticktypes$dateformat, ""), y = as.formula(str_c("~",             ifelse(!is.null(legend$maxwidth) & ticktypes$y !=                 "value", "roboplot.trunc", ticktypes$y))))    shared_params <- c("customdata", "data", "error_x", "error_y",         "hoverlabel", "hovertemplate", "legendgroup", "legendgrouptitle",         "legendrank", "showlegend", "text", "textposition", "texttemplate",         "type", "visible")    if (!is.null(attributes(g)$roboplot.confidence.area)) {        shared_params <- c(shared_params, "fill", "fillcolor",             "opacity", "hoveron")    }    if (text_inside) {        shared_params <- c(shared_params, "insidetextfont")    }    if (!is.null(trace_labels$color) | !is.null(trace_labels$size)) {        shared_params <- c(shared_params, "textfont")    }    if (!is.null(trace_labels$size) & tracetype == "bar") {        shared_params <- c(shared_params, "constraintext")    }    if (tracetype %in% "scatter" & any(c("line", "step", "smooth") %in%         g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter+line" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "marker", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "mode", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "bar" & any(c("horizontal", "horizontalfill",         "horizontalstack") %in% g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "orientation", "offset", "width",             "color", "name", "marker")]    }    else if (tracetype == "bar") {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "pie") {        plotting_params <- plotting_params[c(shared_params, "labels",             "textinfo", "direction", "rotation", "sort", "marker",             "values")]    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            plotting_params$yaxis <- "y2"        }    }    plotting_params}):  In index: 1.
#> Caused by error in `na_if()`:
#> ! could not find function "na_if"

# Use `tidy` to force legend items to have equal horizontal space across columns
# for slightly neater looking plots. Avoid if space is at premium.

energiantuonti |>
  dplyr::filter(Suunta == "Tuonti") |>
  roboplot(
    Alue,
    legend = set_legend(tidy = TRUE)
  )
#> Error in map(split_d, function(g) {    tracetype <- unique(g$roboplot.plot.type)    trace_labels <- roboplotr_trace_labels(tracetype, labels,         names(d))    hoverlab <- case_when(tracetype == "pie" ~ "label", any(c("horizontal",         "horizontalfill", "horizontalstack") %in% g$roboplot.plot.type) &         is.null(pattern) & quo_name(color) != ticktypes$y ~ "text",         TRUE ~ "customdata")    hovertemplate <- roboplotr_hovertemplate(hovertext, lab = hoverlab,         ticktypes)    if (any(unique(pull(g, !!color)) %in% secondary_yaxis)) {        legend_rank <- mean(g$roboplot.legend.rank) + max(d$roboplot.legend.rank)    }    else {        legend_rank <- mean(g$roboplot.legend.rank)    }    .fontsize <- getOption("roboplot.font.main")$size    g <- mutate(g, roboplot.bg.color = roboplotr_alter_color(.data$roboplot.trace.color,         "dark"), roboplot.tx.color = roboplotr_text_color_picker(.data$roboplot.bg.color,         .fontsize))    text_inside <- any(tracetype == "pie" & trace_labels$style !=         "none", tracetype == "bar" & trace_labels$style %in%         c("mini", "auto", "inside"))    if (text_inside) {        if (trace_labels$style == "mini") {            g <- mutate(g, roboplot.in.tx.color = "#FFFFFF00")        }        else {            g <- mutate(g, roboplot.in.tx.color = roboplotr_text_color_picker(.data$roboplot.trace.color,                 getOption("roboplot.font.caption")$size))        }    }    if (!is.null(attributes(g)$roboplot.confidence.area)) {        show.legend <- attributes(g)$roboplot.confidence.area$show_legend        g <- mutate(g, roboplot.confidence.label = str_c(.data$roboplot.plot.text,             "\n", .data[[attributes(g)$roboplot.confidence.area$col]]),             roboplot.plot.text = .data[[attributes(g)$roboplot.confidence.area$col]],             )        if (!is.character(g[[attributes(g)$roboplot.confidence.area$col]])) {            legend_rank <- legend_rank + 10 + as.numeric(unique(g[[attributes(g)$roboplot.confidence.area$col]]))        }        else {            legend_rank <- legend_rank + 10 + 1        }    }    else {        if (is.null(highlight)) {            if (!is.null(pattern_showlegend) & trace_showlegend) {                show.legend <- pattern_showlegend[as.character(unique(g[[as_name(pattern)]]))]            }            else {                show.legend <- trace_showlegend            }        }        else {            show.legend <- roboplotr_highlight_legend(highlight,                 g)        }    }    .legendgrouptitle <- NULL    if ("pie" %in% plot_type) {        .legendgroup <- "pie"    }    else {        .legendgroup <- as.character(unique(pull(g, {            {                color            }        })))    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            .legendgrouptitle <- ticktypes$y2legend            .legendgroup <- "R"        }        else {            .legendgrouptitle <- ticktypes$ylegend            .legendgroup <- "L"        }    }    error_x <- NULL    error_y <- NULL    if (!is.null(confidence$type)) {        if (confidence$type == "bars") {            if (!quo_is_null(confidence$error_x)) {                error_x <- list(array = confidence$error_x, color = confidence$xcolor)            }            if (!quo_is_null(confidence$error_y)) {                error_y <- list(array = g[["roboplot.errorbar.max"]],                   arrayminus = g[["roboplot.errorbar.min"]],                   color = confidence$ycolor)            }        }    }    if (is.null(updatemenu)) {        trace_visible <- T    }    else {        trace_visible <- unique(g$roboplot.update.menu) == updatemenu$selected    }    plotting_params <- list(color = color, constraintext = ifelse(!is.null(trace_labels$size),         "none", "both"), customdata = if (!is.null(attributes(g)$roboplot.confidence.area)) {        ~roboplot.confidence.label    } else if (!quo_is_null(hovertext$col)) {        as.formula(str_c("~", as_label(hovertext$col)))    } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%         g$roboplot.plot.mode)) {        ~roboplot.horizontal.label    } else {        ~roboplot.plot.text    }, data = g, direction = "clockwise", error_x = error_x,         error_y = error_y, fill = unlist(ifelse(!is.null(attributes(g)$roboplot.confidence.area),             list("toself"), list(NULL))), fillcolor ~ color,         hoverlabel = list(bgcolor = ~roboplot.bg.color, bordercolor = first(unlist(unique(getOption("roboplot.grid")[c("xcolor",             "ycolor")]))), font = ~append(getOption("roboplot.font.main")[c("family",             "size")], list(color = roboplot.tx.color))), hoveron = "points+fills",         hovertemplate = hovertemplate, insidetextfont = list(family = getOption("roboplot.font.main")$family,             size = trace_labels$size %||% getOption("roboplot.font.main")$size,             color = ~roboplot.in.tx.color), labels = color, legendgroup = .legendgroup,         legendgrouptitle = list(text = .legendgrouptitle, font = getOption("roboplot.font.main")[c("color",             "family", "size")]), legendrank = legend_rank, line = ~list(width = roboplot.linewidth,             dash = roboplot.dash, smoothing = attributes(g)$roboplot.confidence.area$smoothing %||%                 0.5, shape = case_when(!is.null(attributes(g)$roboplot.confidence.area) ~                 "spline", roboplot.plot.mode == "step" ~ "hv",                 roboplot.plot.mode == "smooth" ~ "spline", TRUE ~                   "linear")), marker = list(colors = ~roboplot.trace.color,             line = getOption("roboplot.trace.border"), symbol = markers$symbol,             size = markers$size, pattern = ~list(shape = roboplot.pattern)),         mode = {            .mode <- str_replace_all(unique(g$roboplot.plot.mode),                 c(`^smooth$` = "lines", `^line$` = "lines", `^step$` = "lines",                   `^scatter$` = "markers", `scatter\\+line` = "markers+lines"))            if (tracetype == "scatter" & trace_labels$style !=                 "none") {                .mode <- str_c(.mode, "+text")            }            .mode        }, name = ~if (!is.null(legend$maxwidth)) {            str_trunc(as.character(roboplot.plot.text), legend$maxwidth)        } else {            roboplot.plot.text        }, offset = if ("horizontalfill" %in% g$roboplot.plot.mode) {            ~roboplot.bar.offset        } else {            NULL        }, offsetgroup = ~str_c(roboplot.pattern, roboplot.trace.color),         opacity = attributes(g)$roboplot.confidence.area$opacity,         orientation = ifelse(any(c("horizontal", "horizontalfill",             "horizontalstack") %in% g$roboplot.plot.mode) & "bar" %in%             g$roboplot.plot.type, "h", "v"), rotation = rotation,         showlegend = show.legend, sort = F, text = if (any(c("horizontal",             "horizontalfill", "horizontalstack") %in% g$roboplot.plot.mode)) {            ~roboplot.horizontal.label        } else {            ~roboplot.plot.text        }, textfont = list(color = ~trace_labels$color %||% roboplot.trace.color,             size = ~trace_labels$size %||% getOption("roboplot.font.main")$size),         textinfo = "percent", textposition = {            if (tracetype == "scatter" & trace_labels$style ==                 "auto") {                .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                   2, 0))                str_replace_all(as.character(tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                   .y[2], 1, -1), sign(diff(.y))), 0)), value)$value),                   c(`-1` = "bottom center", `1` = "top center"))            } else {                case_when(trace_labels$style == "none" ~ "none",                   tracetype == "bar" & trace_labels$style ==                     "mini" ~ "auto", tracetype == "bar" ~ trace_labels$style,                   tracetype == "scatter" & trace_labels$style ==                     "last" ~ "middle right", tracetype == "scatter" &                     trace_labels$style == "auto" ~ "top center",                   tracetype == "pie" ~ "inside", TRUE ~ "none")            }        }, texttemplate = if (!quo_is_null(trace_labels$text_col)) {            if (trace_labels$style == "last") {                .y <- g[[as_label(trace_labels$text_col)]]                .y[-length(.y)] <- ""                .y            } else {                as.formula(str_c("~", as_label(trace_labels$text_col)))            }        } else if (tracetype == "pie") {            NULL        } else if (any(c("horizontal", "horizontalfill", "horizontalstack") %in%             g$roboplot.plot.mode)) {            "%{x:,.1f}"        } else if (tracetype == "scatter" & trace_labels$style ==             "auto") {            .y <- round(g[[ticktypes$y]], max(hovertext$rounding -                 2, 0))            .p <- tidyr::fill(tibble(value = na_if(c(ifelse(.y[1] >=                 .y[2], 1, -1), sign(diff(.y))), 0)), value)$value            .y <- pull(mutate(tidyr::fill(tibble(check = na_if(c(ifelse(sign(.y[2] -                 .y[1]) < 0, -1, 1), sign(diff(.y))), 0)), check),                 check = slider::slide_dbl(check, ~.x[[1]] !=                   .x[[2]], .after = 1, .complete = T)), check)            .y[c(1, length(.y))] <- 1            .y <- roboplotr_format_robotable_numeric(ifelse(.y,                 g[[ticktypes$y]], NA), rounding = max(hovertext$rounding -                 1, 0), na_value = "")            ifelse(.p > 0, str_c(.y, "\n"), str_c("\n", .y))        } else if (trace_labels$style == "last") {            .y <- g[[ticktypes$y]]            .y[-length(.y)] <- ""            .y        } else {            "%{y:,.1f}"        }, type = ~tracetype, values = as.formula(str_c("~",             ticktypes$y)), visible = trace_visible, width = if ("horizontalfill" %in%             g$roboplot.plot.mode) {            ~roboplot.bar.width        } else {            NULL        }, x = as.formula(str_c("~", ticktypes$x)), xhoverformat = ifelse(ticktypes$xticktype ==             "date", ticktypes$dateformat, ""), y = as.formula(str_c("~",             ifelse(!is.null(legend$maxwidth) & ticktypes$y !=                 "value", "roboplot.trunc", ticktypes$y))))    shared_params <- c("customdata", "data", "error_x", "error_y",         "hoverlabel", "hovertemplate", "legendgroup", "legendgrouptitle",         "legendrank", "showlegend", "text", "textposition", "texttemplate",         "type", "visible")    if (!is.null(attributes(g)$roboplot.confidence.area)) {        shared_params <- c(shared_params, "fill", "fillcolor",             "opacity", "hoveron")    }    if (text_inside) {        shared_params <- c(shared_params, "insidetextfont")    }    if (!is.null(trace_labels$color) | !is.null(trace_labels$size)) {        shared_params <- c(shared_params, "textfont")    }    if (!is.null(trace_labels$size) & tracetype == "bar") {        shared_params <- c(shared_params, "constraintext")    }    if (tracetype %in% "scatter" & any(c("line", "step", "smooth") %in%         g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter+line" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "line", "mode", "name", "marker", "color", "xhoverformat")]    }    else if (tracetype == "scatter" & "scatter" %in% g$roboplot.plot.mode) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "mode", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "bar" & any(c("horizontal", "horizontalfill",         "horizontalstack") %in% g$roboplot.plot.mode)) {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "orientation", "offset", "width",             "color", "name", "marker")]    }    else if (tracetype == "bar") {        plotting_params <- plotting_params[c(shared_params, "x",             "y", "offsetgroup", "name", "color", "marker", "xhoverformat")]    }    else if (tracetype == "pie") {        plotting_params <- plotting_params[c(shared_params, "labels",             "textinfo", "direction", "rotation", "sort", "marker",             "values")]    }    if (!is.null(secondary_yaxis)) {        if (as.character(unique(pull(g, {            {                color            }        }))) %in% secondary_yaxis) {            plotting_params$yaxis <- "y2"        }    }    plotting_params}):  In index: 1.
#> Caused by error in `na_if()`:
#> ! could not find function "na_if"

# `set_legend()` works with `robomap()` too, but with a bit different parameters.
if (requireNamespace("sf", quietly = TRUE)) {

d <- vaesto_postinumeroittain |>
  dplyr::filter(stringr::str_detect(Postinumero, "^00(8|9)"))

# Control number of legend items with `breaks`. `robomap()` attempts to use that
# many items, but might settle for a near value.

d |> robomap(Postinumero, "Väkiluku", legend = set_legend(breaks = 5))

# If your legend won't be gradient, you can set specific breakpoints
d |>
  robomap(Postinumero,
          "V\u00e4kiluku",
          legend = set_legend(breaks = c(9000, 12000, 18000), gradient = FALSE))

# Adjust position and opacity.
d |>
  robomap(Postinumero,
          "V\u00e4kiluku",
          legend = set_legend(position = "bottomright", opacity = 0.3))

# Use factor column as value to have labeled legend.
d |>
  dplyr::mutate(
    value = dplyr::case_when(
      value >= stats::quantile(d$value)["75%"] ~ "Suuri",
      value <= stats::quantile(d$value)["25%"] ~ "Pieni",
      TRUE ~ "Normaali"
  ),
  value = forcats::fct_relevel(value, "Pieni","Normaali","Suuri")
  ) |>
  robomap(Postinumero, "V\u00e4kiluku")

}
#> Warning: Some values were outside the color scale and will be treated as NA