Parameters to customize labels displayed with roboplots traces. Use set_title, set_caption, set_legend, and set_plot_axes with other labeling.
Arguments
- style
Character. Use 'none' for no labels (the default for all but pie plots). With
plot_type
"scatter" you can use 'auto' or 'last', withplot_type
"bar" you can use 'auto', 'outside' or 'mini', and withplot_type
pie you can use 'auto' or 'percent'.- color
Symbol. Use this to control the color of text outside the corresponding trace. The default is trace color. Color of text inside a trace is always controlled by roboplot to ensure accessibility.
- text_col
Symbol. The column used for labeling if something else than your trace values.
- ...
When using set_roboplot_options to set labels, you should provide any or all of set_labels(scatter, bar, pie), with 'scatter' controlling the global behavior of both lines and scatters. Do not use these when providing label styles inside roboplot, use
style
instead.
Examples
# Use `set_labels(style = "auto")` to automatically set the labels in bar plots
# inside or outside of bars according to how they fit.
d <- energiantuonti |> dplyr::filter(Alue == "USA")
d |> roboplot(Suunta, plot_type = "bar", plot_mode = "dodge", labels = set_labels("auto"))
#> 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 `set_labels()`:
#> ! could not find function "set_labels"
# Use `style = "mini"` to label only the bars where the labels don't fit (ie. the
# smallest bars). You can omit using the `set_labels()`-function if you only wish
# to provide the state
d |> roboplot(Suunta, plot_type = "bar", plot_mode = "dodge", labels = "mini")
# `"inside"` and `"outside"` to force either state
d |> roboplot(Suunta, plot_type = "bar", plot_mode = "dodge", labels = "outside")
# Give `color` if you want to override the colors of labels outside the bars.
# `roboplot()` will always use its internal color paletter for the labels inside
# the bars to ensure accessibility. Give `size`to override the automatic font sizes
# plotly uses.
d |> roboplot(
Suunta,
plot_type = "bar",
labels = set_labels("auto", color = "black", size = 15)
)
#> 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 `set_labels()`:
#> ! could not find function "set_labels"
# Works with horizontal bars, too
energiantuonti |>
dplyr::filter(time == max(time), Suunta == "Tuonti") |>
roboplot(
Suunta,
plot_type = "bar",
plot_mode = "horizontalfill",
plot_axes = set_axes(y = "Alue"),
labels = set_labels(style = "auto", size = 22)
)
#> 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 `set_labels()`:
#> ! could not find function "set_labels"
# For scatter traces, use "auto" to highlight data points. You most likely will
# need to provide y-axis limits manually.
d |> roboplot(Suunta, labels = "auto", plot_axes = set_axes(ylim = c(-60, 700)))
#> 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"
# If you specify axis limits or `xaxis_ceiling`, `roboplotr()` will not be able
# to detect x-axis limits correctly. Use xaxis_ceiling "default" should work.
d |> roboplot(Suunta, labels = "auto", xaxis_ceiling = "guess")
#> 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"
# Pies can handle only no labels, or percentage labels.
energiantuonti |>
dplyr::filter(time == max(time), Suunta == "Tuonti") |>
roboplot(Alue, plot_type = "pie")
# Set defaults with `set_roboplot_options()` by trace type.
set_roboplot_options(labels = set_labels(scatter = "auto", bar = "mini", pie = "none"))