apyramid

The goal of {apyramid} is to provide a quick method for visualizing census data stratified by age and one or two categorical variables (e.g. gender and health status). This is a product of the R4EPIs project; learn more at https://r4epis.netlify.com.

Installation

You can install {apyramid} from CRAN:

install.packages("apyramid")
Click here for alternative installation options If there is a bugfix or feature that is not yet on CRAN, you can install it via the {drat} package: ``` r # install.packages("drat") drat::addRepo("R4EPI") install.packages("apyramid") ``` You can also install the in-development version from GitHub using the {remotes} package (but there’s no guarantee that it will be stable): ``` r # install.packages("remotes") remotes::install_github("R4EPI/apyramid") ```

The {apyramid} package was primarily designed for quick visualisation of un-aggregated linelist data in field epidemiological situations. It has one available function:

  • age_pyramid() returns age pyramid visualizations of linelist, survey, or pre-aggregated census data as a ggplot object.
library("apyramid")
library("ggplot2")   # load ggplot2 to control plot aesthetics
library("outbreaks") # load the outbreaks package for linelist data
old_theme <- theme_set(theme_classic(base_size = 18))

Example

We can demonstrate plotting of un-aggregated data with the fluH7N9_china_2013 data set in the {outbreaks} package that records 136 cases of Influenza A H7N9 in China in 2013 (source: https://doi.org/10.5061/dryad.2g43n)

Since the result is a ggplot2 object, it can be customized like one:

One of the advantages of {apyramid} is that it will adjust to account for non-binary categorical variables. For example, in the flu data set, there are two cases with no gender reported. If we set na.rm = FALSE, we can the age distribution of these two cases:

age_pyramid(flu, age_group, split_by = gender, na.rm = FALSE)

Pre-aggregated data

{apyramid} can also be used to visualize pre-aggregated data. This example is the US census data from 2018:

You can also use another factor to split the data:

data(us_ins_2018) # stratified by gender and health insurance status
data(us_gen_2018) # stratified by gender and generational status
p_ins <- age_pyramid(us_ins_2018, age_group = age, split_by = gender, stack_by = insured, count = count)
p_gen <- age_pyramid(us_gen_2018, age_group = age, split_by = gender, stack_by = generation, count = count)
p_ins + us_labels

Survey Data

Beyond that, survey data can be incorporated with the help of srvyr. Note that while it will show the weighted counts, it will not show the confidence intervals as that highly depends on the appropriate choice of CI estimator. This is meant as more of quick visualization tool for EDA.

library(srvyr, warn.conflicts = FALSE)
data(api, package = "survey")

dstrata <- apistrat %>%
  mutate(apicat = cut(api00, pretty(api00), include.lowest = TRUE, right = TRUE)) %>%
  as_survey_design(strata = stype, weights = pw)
 
age_pyramid(dstrata, apicat, split_by = stype)


theme_set(old_theme)