Apply the uniform styling inputs to a module's figure
Source:R/plot_tiers.R
apply_uniform_styling.RdThe plot modules build their figures in two steps. The first draws the plot
itself from the data and every input that changes what is drawn; that is the
expensive part (a ggplot built and converted with plotly::ggplotly(), say),
and it is cached. This function is the second step: it applies the inputs
that only style the figure – titles, fonts, ticks and their notation,
gridlines, the padding around the data, reference lines, the modebar, the
shape-drawing tool, and the legend – to the figure
the first step returned. Changing one of those inputs re-runs only this step,
which takes a few milliseconds rather than a full rebuild.
Usage
apply_uniform_styling(
fig,
input,
isolate_fn = isolate,
facet.by = NULL,
title_y = 0.95,
include.modebar.buttons = TRUE,
fig.fn = NULL,
ggplot.axis.styling = TRUE,
legend.position = NULL,
padding.axes = c("x", "y")
)Arguments
- fig
A plotly figure: the module's plot, before styling.
- input
The module's Shiny
input, holding the uniform inputs fromuniform_axes_inputs_ui(),uniform_lines_inputs_ui(),uniform_plotly_inputs_ui(), anduniform_legend_inputs_ui().- isolate_fn
The function from
setup_auto_update_logic()that every input read goes through.- facet.by
The facetting column(s), or
NULL. A faceted figure has shared axis titles drawn as annotations, which are styled to match, and no main title (seeadd_plot_config()).- title_y
Vertical position of the main title, in paper coordinates.
Passed to
add_plot_config().- fig.fn
Optional
function(fig, input, isolate_fn)returning the figure, run after the legend is styled and before the axis titles become draggable annotations, so any axis title text it sets is the one made draggable. Read inputs in it asisolate_fn(input$key). It runs after any overlay panels are drawn, so anything it places on"paper"coordinates can land on them; usexref = "x domain"andyref = "y domain"to stay within the plot's panel.- ggplot.axis.styling
Passed to
create_axis_styles().TRUEfor a figure whose axis lines come from its ggplot theme (seecreate_ggplot_axis_style());FALSEsets them from the Axes inputs here.- legend.position
Passed to
apply_legend_inputs();NULLkeeps the legend where it is.- padding.axes
Which of the module's axes,
"x"and"y", take theaxis.padding.x/axis.padding.yinputs (seeapply_axis_padding()). Leave out an axis with its own limit inputs, so its limits stand. Wheninput$rotateisTRUEthe padding follows the variables onto the swapped axes.
Details
The figure passed in is never modified (R copies on write), so each call styles the cached figure afresh and nothing appended here (reference lines, say) accumulates across calls.
Axes used only by overlay panels drawn alongside the plot (the scatter
plot's marginal distributions, see add_marginal_plots()) keep their own
styling and get no reference lines.
Examples
fig <- plotly::plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", mode = "markers")
# The uniform inputs, as a module's `input` would hold them.
input <- list(
title.font.size = 20, title.font.family = "Arial", title.font.color = "#000000",
axis.title.horizontal.position = 0.5, axis.title.font.size = 16,
axis.title.font.family = "Arial", axis.title.font.color = "#000000",
axis.tickfont.size = 12, axis.tickfont.family = "Arial", axis.tickfont.color = "#000000",
axis.tickangle.x = 0, axis.tickangle.y = 0, axis.ticks = "outside",
axis.tickcolor = "#000000", axis.ticklen = 5, axis.tickwidth = 1,
show.grid.x = TRUE, show.grid.y = FALSE, grid.color = "#CCCCCC",
hline.intercepts = "20", hline.colors = "#D55E00",
download.format = "svg", legend.text.size = 12, legend.title.size = 14
)
apply_uniform_styling(fig, input, isolate_fn = identity)