Skip to contents

This function creates an age-sex pyramid visualisation, 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 = "age", date_of_birth = "date_of_birth", sex =
    "sex", age_group = "age_group", value = "value", lowercl = "lowercl", uppercl =
    "uppercl"), colours = c("#440154", "#2196F3"), x_breaks = 10, y_title =
    "Age group (years)", x_title = "Number of cases", text_size = 12, conf_limits =
    FALSE, age_breakpoints = c(0, 5, 19, 65, Inf), age_calc_refdate = Sys.Date(), grouped
    = FALSE, legend_position = "top", legend_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

Name of the column containing age values. Default is 'age'.

date_of_birth

Name of the column containing date of birth values. Default is 'date_of_birth'.

sex

Name of the column containing sex values. Default is 'sex'.

age_group

Name of the column containing pre-grouped age groups (if grouped = TRUE).

value

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

lowercl

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

uppercl

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

colours

A vector of colours to be used in the plot. Default is c("#440154", "#fde725").

x_breaks

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

y_title

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

x_title

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

text_size

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

conf_limits

Logical. If TRUE, confidence limits are displayed on the pyramid. Default is FALSE.

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_position

Position of the legend. Default is "top".

legend_title

Title of the legend. Default is "".

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) {
# Example using a line list
df <- epiviz::lab_data
age_sex_pyramid(
  dynamic = FALSE,
  params = list(
    df = df,
    var_map = list(age = 'age', date_of_birth = 'date_of_birth', sex = '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),
  lowercl = c(90, 110, 140, 70),
  uppercl = c(110, 130, 160, 90)
)
age_sex_pyramid(
  dynamic = FALSE,
  params = list(
    df = grouped_df,
    var_map = list(age_group = 'age_group', sex = 'sex', value = 'value'),
    grouped = TRUE
  )
)
}