Skip to contents

Sets the legend title and entry-label font sizes on a plotly figure so the "Legend" UI inputs behave consistently across plot types. Existing legend settings (orientation, position, font family/colour) are preserved because plotly::layout() merges the supplied attributes into the current layout. NULL or NA sizes are ignored, leaving the corresponding font size untouched.

Usage

apply_legend_styling(fig, title.size = NULL, text.size = NULL, position = NULL)

Arguments

fig

A plotly figure object.

title.size

Numeric font size for the legend (or colorbar) title, or NULL to leave unchanged.

text.size

Numeric font size for the legend entry labels (or colorbar tick labels), or NULL to leave unchanged.

position

vector of an integer, an xanchor string, and a orientation argument. e.g. c(1.02, "left", "v"). Controls horizontal positioning of the legend.

Value

The plotly figure with the requested legend font sizes applied. Returns the figure unchanged when fig is NULL or no valid sizes are supplied.

Details

Numeric colour mappings (for example fill.by/color.by on a continuous variable) are rendered as a colorbar rather than a categorical legend. The layout-level legend font does not affect a colorbar, so the colorbar title and tick fonts are updated directly on each trace (and on any shared coloraxis) using the same sizes. This keeps the "Legend" controls functional for both categorical and continuous legends.

Author

Jared Andrews

Examples

fig <- plotly::plot_ly(iris,
    x = ~Sepal.Length, y = ~Sepal.Width,
    color = ~Species, type = "scatter", mode = "markers"
)
apply_legend_styling(fig, title.size = 16, text.size = 10)