Learn R Programming

censuspyrID (version 1.0.2)

area_trends: Plot Population Area Trends by Age Group and Sex

Description

This function builds an area plot showing the proportion of population distributed across three broad age groups (young, working-age, old) over census years. The plot can be displayed separately by sex or combined.

Usage

area_trends(data, sex = 1, color = "Fresh and bright")

Value

A ggplot2 object showing the population area trends.

Arguments

data

A data frame containing population trends data for a specific region over years. Must include variables year, sex, age5, and pop.

sex

Integer indicating which sex to include in the plot:

  • 1 = All sexes

  • 2 = Male

  • 3 = Female

  • 4 = Male+Female

Default is 1 (all sexes).

color

Character string specifying the palette name from ggthemes::canva_palettes. Default is "Fresh and bright".

Details

The function aggregates population into three age groups:

  • 0–14 years (Young)

  • 15–64 years (Working age)

  • 65+ years (Old)

It then calculates the proportion of each age group within each sex and year. The result is plotted as a stacked area chart, optionally faceted by sex.

See Also

pyr_trends(), load_pop_data(), pop_data_by_reg(), get_code_label()

Examples

Run this code
if (FALSE) {
# Example: area trends for Indonesia
data_idn <- load_pop_data(harmonized = TRUE, smoothing = 1) |>
  pop_data_by_reg(0) #Indonesia
area_trends(data_idn, sex = 1) #All sexes
area_trends(data_idn, sex = 2) #Male
area_trends(data_idn, sex = 3) #Female
area_trends(data_idn, sex = 4) #Male+Female
}

Run the code above in your browser using DataLab