library(dplyr)
# Example data: Age group, Sex, and number of cases
prop_data <- tibble::tibble(
age_cat = rep(c("0-9", "10-19", "20-29", "30-39"), each = 2),
Sex = rep(c("Man", "Vrouw"), times = 4),
n = c(12, 8, 15, 12, 20, 18, 10, 7)
) |>
group_by(age_cat) |>
mutate(
total = sum(n),
prop = n / total,
prop_label = paste0(round(prop * 100, 1), "%"),
bar_label = age_cat
) |>
ungroup()
# Define color palette using your color function
sex_colors <- c(
"Man" = ro_color("hemelblauw"),
"Vrouw" = ro_color("robijnrood")
)
# Use ro_ply_theme with barmode = "stack"
theme_rivm <- ro_ply_theme(barmode = "stack")
## ! Can't find the `base_family` = "RijksoverheidSansWebText" in installed fonts,
## and neither fall-back fonts, "Verdana" and "Arial", are installed.
## ℹ Falling back to "URW Gothic", the first font found on this system. Check
## installed system fonts with `systemfonts::system_fonts()`.
# Create the proportional stacked bar chart (horizontal)
fig <- plot_ly(
data = prop_data,
y = ~bar_label,
x = ~prop,
color = ~Sex,
colors = sex_colors,
type = "bar",
orientation = "h",
text = ~paste0(
"Leeftijdsgroep: <b>", age_cat, "</b><br>",
"Geslacht: <b>", Sex, "</b><br>",
"Percentage: <b>", prop_label, "</b>"
),
textposition = "auto",
hovertemplate = ~paste0(
"Leeftijdsgroep: <b>", age_cat, "</b><br>",
"Geslacht: <b>", Sex, "</b><br>",
"Percentage: <b>", prop_label, "</b>"
),
hoverinfo = "text"
)
fig <- ro_ply_add_theme(fig, theme_rivm)
fig <- ro_ply_title(
fig,
y_data = prop_data$bar_label,
y_label = "Leeftijdsgroep",
title = "Percentage COVID-19 cases per leeftijdsgroep en geslacht",
x_label = "Percentage"
)
## ! Can't find the `base_family` = "RijksoverheidSansWebText" in installed fonts,
## and neither fall-back fonts, "Verdana" and "Arial", are installed.
## ℹ Falling back to "URW Gothic", the first font found on this system. Check
## installed system fonts with `systemfonts::system_fonts()`.
fig <- ro_ply_cursor(fig)
fig <- ro_ply_legend_theme(fig)
fig <- ro_ply_dim(fig)
# Format x-axis as percentage
fig <- fig |>
layout(
xaxis = list(
tickformat = ".0%",
range = c(0, 1),
title = "Percentage"
)
)
fig