Skip to contents

This function creates an age-sex pyramid visualization, either as a static ggplot or an interactive plotly chart. The function can take either a line list (ungrouped data) or already grouped data as input. When using a line list, the function processes the data, groups it by age and sex, and then generates the pyramid. If grouped data is provided, it directly creates the pyramid.

Usage

age_sex_pyramid(
  dynamic = FALSE,
  base = NULL,
  params = list(df, var_map = list(age_var = "age", dob_var = "date_of_birth", sex_var =
    "sex", age_group_var = "age_group", value_var = "value", ci_lower = "ci_lower",
    ci_upper = "ci_upper"), mf_colours = c("#440154", "#2196F3"), x_breaks = 10,
    x_axis_title = "Number of cases", y_axis_title = "Age group (years)", text_size = 12,
    ci = NULL, ci_colour = "red", age_breakpoints = c(0, 5, 19, 65, Inf),
    age_calc_refdate = Sys.Date(), grouped = FALSE, legend_pos = "top", chart_title = "")
)

Arguments

dynamic

Logical. If TRUE, the function returns an interactive plotly chart. If FALSE, a static ggplot chart is returned.

base

An optional base plot to add the pyramid to. Default is NULL.

params

A list of parameters including:

df

Data frame containing the data to be used.

var_map

A list mapping variable names in the data frame to the expected names used in the function.

age_var

Name of the variable in df containing age values. Default is 'age'.

dob_var

Name of the variable in df containing date of birth values. Default is 'date_of_birth'.

sex_var

Name of the variable in df containing sex values. Default is 'sex'.

age_group_var

Name of the variable in df containing pre-grouped age groups (if grouped = TRUE).

value_var

Name of the column containing the value counts (if grouped = TRUE).

ci_lower

Name of the column containing lower confidence limits (if ci = TRUE).

ci_upper

Name of the column containing upper confidence limits (if ci = TRUE).

mf_colours

A vector of 2 colours used to fill the male and female bars in the plot. The first colour will be used for males, and the second for females. Default is c("#440154", "#2196F3").

x_breaks

Number of breaks on the x-axis. Default is 10.

y_axis_title

Title of the y-axis. Default is "Individual count".

x_axis_title

Title of the x-axis. Default is "Number of cases".

text_size

Size of the text in the plot. Default is 12.

ci

Confidence interval. If ci = "errorbar" then confidence intervals will be be plotted with each bar as errorbars. If ci = "errorbar" and grouped = FALSE, then default confidence intervals are applied using the normal approximation to the Poisson distribution, with bounds set at \(\pm 1.96 \times \sqrt{n}\).

ci_colour

Colour of the plotted errorbars if ci = TRUE. Default is "red".

age_breakpoints

A numeric vector specifying the breakpoints for age groups. Default is c(0, 5, 19, 65, Inf).

age_calc_refdate

Reference date for calculating age from date of birth. Default is Sys.Date().

grouped

Logical. If TRUE, assumes the data is pre-grouped by age and sex. If FALSE, the function processes the line list data. Default is FALSE.

legend_pos

Position of the legend. Default is "top".

chart_title

Text to use as the chart title.

Value

A ggplot or plotly object representing the age-sex pyramid, depending on the value of dynamic.

Details

When grouped = FALSE, the function processes a line list by grouping the data by age and sex, calculating age based on either the provided age column or date of birth, and then generating the age-sex pyramid. When grouped = TRUE, it assumes the data is already grouped and uses the provided values directly to generate the pyramid.

Examples

if (FALSE) { # \dontrun{
# Example using a line list
df <- epiviz::lab_data
age_sex_pyramid(
  dynamic = FALSE,
  params = list(
    df = df,
    var_map = list(age_var = 'age', dob_var = 'date_of_birth', sex_var = 'sex'),
    grouped = FALSE
  )
)

# Example using pre-grouped data
grouped_df <- data.frame(
  age_group = c("0-4", "5-18", "19-64", "65+"),
  sex = c("Male", "Female"),
  value = c(100, 120, 150, 80),
  ci_lower = c(90, 110, 140, 70),
  ci_upper = c(110, 130, 160, 90)
)
age_sex_pyramid(
  dynamic = FALSE,
  params = list(
    df = grouped_df,
    var_map = list(age_group_var = 'age_group', sex_var = 'sex', value = 'value'),
    grouped = TRUE
  )
)
} # }