| Title: | Create interactive data visualisations in Rijksoverheid style using 'plotly' |
|---|---|
| Description: | Provides accessible, keyboard-navigable 'plotly' visualisations in Rijksoverheid style. |
| Authors: | ROvis team [cre, aut] |
| Maintainer: | ROvis team <[email protected]> |
| License: | EUPL (>= 1.2) |
| Version: | 0.1.0 |
| Built: | 2026-10-19 12:32:37 UTC |
| Source: | https://github.com/rivm-syso/ROvis.plotly |
Applies a specified Plotly theme (as a list) to a Plotly figure object.
ro_ply_add_theme(fig, theme)ro_ply_add_theme(fig, theme)
fig |
A Plotly figure object created by |
theme |
A named list of layout parameters to apply as a theme (e.g., font, colors, margins). |
A Plotly figure object with the specified theme applied.
Other plotly:
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
## Not run: fig <- ro_ply_add_theme(fig, theme = theme_rivm) ## End(Not run)## Not run: fig <- ro_ply_add_theme(fig, theme = theme_rivm) ## End(Not run)
Changes the mouse cursor style for a Plotly figure from the default crosshair to the standard arrow cursor.
ro_ply_cursor(fig)ro_ply_cursor(fig)
fig |
A Plotly figure object created by |
A Plotly figure object with the cursor style set to "default" (arrow).
Other plotly:
ro_ply_add_theme(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
Other ggplotly:
ro_ply_disable_toolbar(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline()
## Not run: fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers') fig_cursor <- ro_ply_cursor(fig) fig_cursor ## End(Not run)## Not run: fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers') fig_cursor <- ro_ply_cursor(fig) fig_cursor ## End(Not run)
Applies custom width and height to a Plotly figure, ensuring the plot displays at the specified dimensions. The function also sets the plot to autosize for responsive layouts.
ro_ply_dim(fig, width = 776, height = 400)ro_ply_dim(fig, width = 776, height = 400)
fig |
A Plotly figure object created by |
width |
Desired plot width in pixels. Default is 776. |
height |
Desired plot height in pixels. Default is 400. |
A Plotly figure object with updated sizing policy and layout.
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
## Not run: fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers') fig_dimmed <- ro_ply_dim(fig, width = 800, height = 500) fig_dimmed ## End(Not run)## Not run: fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers') fig_dimmed <- ro_ply_dim(fig, width = 800, height = 500) fig_dimmed ## End(Not run)
ro_ply_disable_toolbar(fig)ro_ply_disable_toolbar(fig)
fig |
A |
Removes the plotly modebar (toolbar) for a cleaner look and disables the pop-up for the legend, which improves accessibility for keyboard users.
A plotly object without the modebar and with improved legend accessibility.
Other ggplotly:
ro_ply_cursor(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline()
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
## Not run: data <- tidyr::tibble( expand.grid(`Age group` = c("A", "B", "C", "D", "E"), `Sex` = c("Man", "Vrouw")), `Number of Cases` = sample(1:50, 10, TRUE) ) library(ggplot2) library(plotly) p <- ggplot(data, aes( x = `Number of Cases`, y = `Age group`, fill = Sex, text = paste0( "Leeftijdsgroep: <b>", `Age group`, "</b><br>", "Geslacht: <b>", Sex, "</b><br>", "Aantal cases: <b>", `Number of Cases`, "</b>" ) )) + geom_col(position = "dodge") + labs( x = "Aantal cases", y = "Leeftijdsgroep", fill = "Geslacht" ) fig <- ggplotly(p, tooltip = "text") fig <- ro_ply_keyboard_nav_barchart(fig) fig <- ro_ply_keyboard_nav_legend(fig) fig <- ro_ply_disable_toolbar(fig) fig ## End(Not run)## Not run: data <- tidyr::tibble( expand.grid(`Age group` = c("A", "B", "C", "D", "E"), `Sex` = c("Man", "Vrouw")), `Number of Cases` = sample(1:50, 10, TRUE) ) library(ggplot2) library(plotly) p <- ggplot(data, aes( x = `Number of Cases`, y = `Age group`, fill = Sex, text = paste0( "Leeftijdsgroep: <b>", `Age group`, "</b><br>", "Geslacht: <b>", Sex, "</b><br>", "Aantal cases: <b>", `Number of Cases`, "</b>" ) )) + geom_col(position = "dodge") + labs( x = "Aantal cases", y = "Leeftijdsgroep", fill = "Geslacht" ) fig <- ggplotly(p, tooltip = "text") fig <- ro_ply_keyboard_nav_barchart(fig) fig <- ro_ply_keyboard_nav_legend(fig) fig <- ro_ply_disable_toolbar(fig) fig ## End(Not run)
ro_ply_focus_linechart()ro_ply_focus_linechart()
Adds custom CSS to your Shiny app to remove the outline and box-shadow that appear when lines in a Plotly linechart receive focus (via keyboard navigation). This improves the visual appearance for certain use-cases, but be aware that it may affect accessibility for keyboard users.
HTML code (using tags$head and tags$style) to be included in a Shiny UI.
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
if (interactive()) { ui <- fluidPage( ro_ply_focus_linechart(), plotlyOutput("myplot") ) server <- function(input, output, session) { output$myplot <- renderPlotly({ plot_ly(x = 1:10, y = rnorm(10), type = "scatter", mode = "lines") }) } }if (interactive()) { ui <- fluidPage( ro_ply_focus_linechart(), plotlyOutput("myplot") ) server <- function(input, output, session) { output$myplot <- renderPlotly({ plot_ly(x = 1:10, y = rnorm(10), type = "scatter", mode = "lines") }) } }
Applies advanced custom styling to the legend of a Plotly figure.
This function adds a box around each individual legend item and changes legend symbols to rectangles.
The modifications are applied using JavaScript via htmlwidgets::onRender().
ro_ply_legend_theme(fig)ro_ply_legend_theme(fig)
fig |
A Plotly figure object created by |
The Plotly figure object with the custom legend styling applied.
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_theme(),
ro_ply_title()
## Not run: fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~hp, color = ~factor(cyl), type = 'scatter', mode = 'markers') fig_styled <- ro_ply_legend_theme(fig) fig_styled ## End(Not run)## Not run: fig <- plotly::plot_ly(data = mtcars, x = ~mpg, y = ~hp, color = ~factor(cyl), type = 'scatter', mode = 'markers') fig_styled <- ro_ply_legend_theme(fig) fig_styled ## End(Not run)
Returns a standardized RIVM theme as a list for use with plotly charts, allowing customization of fonts, colors, grid, axis, legend, and tooltip styling.
ro_ply_theme( barmode = "vgroup", base_family = "RijksoverheidSansWebText", title_color = ro_color(color_name = "lintblauw"), title_size = 17, y_axis_title_size = 13, x_axis_title_size = 13, axis_tick_size = 12, axis_title_color = ro_color(color_name = "lintblauw"), axis_tick_color = ro_color(color_name = "grijs_7"), grid_color = ro_color(color_name = "grijs_3"), grid_width = 0.25, y_axis_showgrid = TRUE, x_axis_showgrid = FALSE, x_axis_tick_format = NULL, y_axis_tick_format = ",.0f", y_axis_showline = FALSE, x_axis_showline = TRUE, x_line_color = ro_color(color_name = "grijs_7"), y_line_color = ro_color(color_name = "lintblauw"), x_line_width = 1.5, y_line_width = 0, y_axis_spike = FALSE, x_axis_spike = TRUE, cross_hair_color = "#535353", cross_hair_style = "dash", cross_hair_width = 1, colorway = c(ro_color(color_name = "hemelblauw"), ro_color(color_name = "donkergeel"), ro_color(color_name = "robijnrood"), ro_color(color_name = "paars_tint90"), ro_color(color_name = "mintgroen_tint110"), ro_color(color_name = "oranje"), ro_color(color_name = "groen"), ro_color(color_name = "donkerbruin")), legend_font_size = 13, legend_color = ro_color(color_name = "grijs_7"), plot_bgcolor = "#ffffff", paper_bgcolor = "#ffffff", tooltip_color = "#000000", tooltip_border_color = "#000000", tooltip_background_color = "#ffffff", tooltip_font_size = 13 )ro_ply_theme( barmode = "vgroup", base_family = "RijksoverheidSansWebText", title_color = ro_color(color_name = "lintblauw"), title_size = 17, y_axis_title_size = 13, x_axis_title_size = 13, axis_tick_size = 12, axis_title_color = ro_color(color_name = "lintblauw"), axis_tick_color = ro_color(color_name = "grijs_7"), grid_color = ro_color(color_name = "grijs_3"), grid_width = 0.25, y_axis_showgrid = TRUE, x_axis_showgrid = FALSE, x_axis_tick_format = NULL, y_axis_tick_format = ",.0f", y_axis_showline = FALSE, x_axis_showline = TRUE, x_line_color = ro_color(color_name = "grijs_7"), y_line_color = ro_color(color_name = "lintblauw"), x_line_width = 1.5, y_line_width = 0, y_axis_spike = FALSE, x_axis_spike = TRUE, cross_hair_color = "#535353", cross_hair_style = "dash", cross_hair_width = 1, colorway = c(ro_color(color_name = "hemelblauw"), ro_color(color_name = "donkergeel"), ro_color(color_name = "robijnrood"), ro_color(color_name = "paars_tint90"), ro_color(color_name = "mintgroen_tint110"), ro_color(color_name = "oranje"), ro_color(color_name = "groen"), ro_color(color_name = "donkerbruin")), legend_font_size = 13, legend_color = ro_color(color_name = "grijs_7"), plot_bgcolor = "#ffffff", paper_bgcolor = "#ffffff", tooltip_color = "#000000", tooltip_border_color = "#000000", tooltip_background_color = "#ffffff", tooltip_font_size = 13 )
barmode |
Bar mode for bar charts. "vgroup" by default. |
base_family |
Font family for all text (default = "RijksoverheidSansWebText"). |
title_color |
Color for the main title text. |
title_size |
Font size for the main title. |
y_axis_title_size |
Font size for y-axis title. |
x_axis_title_size |
Font size for x-axis title. |
axis_tick_size |
Font size for axis tick labels. |
axis_title_color |
Color for axis titles. |
axis_tick_color |
Color for axis tick labels and axis lines. |
grid_color |
Color for grid lines. |
grid_width |
Width of grid lines. |
y_axis_showgrid |
Show grid lines for y-axis. |
x_axis_showgrid |
Show grid lines for x-axis. |
x_axis_tick_format |
Format string for x-axis tick labels (e.g., ",.0f" for thousands separator as comma, ".0%" for percent, ".0f" for no thousand seperator, for more information on time-formats see https://d3js.org/d3-time-format) |
y_axis_tick_format |
Format string for x-axis tick labels (e.g., ",.0f" for thousands separator as comma, ".0%" for percent, ".0f" for no thousand seperator, for more information on time-formats see https://d3js.org/d3-time-format) |
y_axis_showline |
Show y-axis line. |
x_axis_showline |
Show x-axis line. |
x_line_color |
Color for the x-axis line. |
y_line_color |
Color for the y-axis line. |
x_line_width |
Width of the x-axis line. |
y_line_width |
Width of the y-axis line. |
y_axis_spike |
Show crosshair/spike on y-axis. |
x_axis_spike |
Show crosshair/spike on x-axis. |
cross_hair_color |
Color for crosshair/spike lines. |
cross_hair_style |
Style for crosshair/spike lines. |
cross_hair_width |
Width for crosshair/spike lines. |
colorway |
Colors for data series. |
legend_font_size |
Font size for the legend. |
legend_color |
Color for the legend text. |
plot_bgcolor |
Plot background color. |
paper_bgcolor |
Figure (paper) background color. |
tooltip_color |
Font color for tooltips. |
tooltip_border_color |
Border color for tooltips. |
tooltip_background_color |
Background color for tooltips. |
tooltip_font_size |
Font size for tooltips. |
A named list with plotly theming options, suitable for passing
to plotly::layout() or as a theme in a plotly chart.
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_title()
## Not run: # Example data: number of cases per age group and sex stacked_data <- tibble::tibble( Agegroup = rep(c("0-9", "10-19", "20-29", "30-39"), each = 3), Sex = rep(c("Man", "Vrouw", "Onbekend"), times = 4), n = c(10, 14, 2, 15, 17, 1, 23, 18, 0, 9, 11, 3) ) # Define color palette using your color function sex_colors <- c( "Man" = ro_color("hemelblauw"), "Vrouw" = ro_color("robijnrood"), "Onbekend" = ro_color("donkergeel") ) # Use ro_ply_theme with barmode = "stack" theme_rivm <- ro_ply_theme(barmode = "stack") # Create the plotly stacked bar chart fig <- plotly::plot_ly( data = stacked_data, x = ~Agegroup, y = ~n, type = "bar", color = ~Sex, colors = sex_colors, text = ~paste0( "Leeftijdsgroep: <b>", Agegroup, "</b>", "<br>Geslacht: <b>", Sex, "</b>", "<br>Aantal cases: <b>", format(round(n, 0), big.mark = ".", decimal.mark = ",", scientific = FALSE), "</b>" ), hoverinfo = "text" ) fig <- ro_ply_add_theme(fig, theme_rivm) fig ## End(Not run)## Not run: # Example data: number of cases per age group and sex stacked_data <- tibble::tibble( Agegroup = rep(c("0-9", "10-19", "20-29", "30-39"), each = 3), Sex = rep(c("Man", "Vrouw", "Onbekend"), times = 4), n = c(10, 14, 2, 15, 17, 1, 23, 18, 0, 9, 11, 3) ) # Define color palette using your color function sex_colors <- c( "Man" = ro_color("hemelblauw"), "Vrouw" = ro_color("robijnrood"), "Onbekend" = ro_color("donkergeel") ) # Use ro_ply_theme with barmode = "stack" theme_rivm <- ro_ply_theme(barmode = "stack") # Create the plotly stacked bar chart fig <- plotly::plot_ly( data = stacked_data, x = ~Agegroup, y = ~n, type = "bar", color = ~Sex, colors = sex_colors, text = ~paste0( "Leeftijdsgroep: <b>", Agegroup, "</b>", "<br>Geslacht: <b>", Sex, "</b>", "<br>Aantal cases: <b>", format(round(n, 0), big.mark = ".", decimal.mark = ",", scientific = FALSE), "</b>" ), hoverinfo = "text" ) fig <- ro_ply_add_theme(fig, theme_rivm) fig ## End(Not run)
This function adds a (custom styled) main title, y-axis label, and/or x-axis label as annotations to a Plotly figure, using RIVM theming and Dutch number formatting. It automatically calculates left margin spacing based on the longest y-axis label.
ro_ply_title( fig, y_data = NULL, y_label = NULL, x_label = NULL, title = NULL, base_family = "RijksoverheidSansWebText", canvas_width = 776, left_margin_base = 10, char_width = 7, title_size = 17, axis_title_size = 13, title_color = ro_color(color_name = "lintblauw"), thousand_seperator = ".", decimal_seperator = "," )ro_ply_title( fig, y_data = NULL, y_label = NULL, x_label = NULL, title = NULL, base_family = "RijksoverheidSansWebText", canvas_width = 776, left_margin_base = 10, char_width = 7, title_size = 17, axis_title_size = 13, title_color = ro_color(color_name = "lintblauw"), thousand_seperator = ".", decimal_seperator = "," )
fig |
A plotly figure object created by |
y_data |
Used to determine the longest y-axis label for margin calculation. |
y_label |
Y-axis label to display. |
x_label |
X-axis label to display. |
title |
Main title to display at the top left. |
base_family |
Font family to use. |
canvas_width |
Width of the plotting area in pixels. |
left_margin_base |
Base left margin in pixels. |
char_width |
Estimated width per character in pixels. |
title_size |
Font size for the main title. |
axis_title_size |
Font size for axis titles. |
title_color |
Color for all titles/labels. |
thousand_seperator |
Separator for thousands in y-axis labels. |
decimal_seperator |
Separator for decimals in y-axis labels. |
The plotly figure object with added (and styled) title and/or axis labels as annotations.
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme()
## Not run: # Title only: ro_ply_title(fig, y_data = data$'Number of Cases', title = "Fancy title") # Y-axis label only: ro_ply_title(fig, y_label = "Number of cases") # X-axis label only: ro_ply_title(fig, x_label = "Groups") # Title, Y-axis label, and X-axis label in one go: ro_ply_title(fig, y_data = data$'Number of Cases', y_label = "Number of cases", title = "Fancy title", x_label = "groups") ## End(Not run)## Not run: # Title only: ro_ply_title(fig, y_data = data$'Number of Cases', title = "Fancy title") # Y-axis label only: ro_ply_title(fig, y_label = "Number of cases") # X-axis label only: ro_ply_title(fig, x_label = "Groups") # Title, Y-axis label, and X-axis label in one go: ro_ply_title(fig, y_data = data$'Number of Cases', y_label = "Number of cases", title = "Fancy title", x_label = "groups") ## End(Not run)