• Show All Code
  • Hide All Code

Age Pyramid

Subasish Das

Age Pyramid

Option 1

Data

library(apyramid)
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))

autocut <- function(x) {
  cut(x, breaks = pretty(x), right = TRUE, include.lowest = TRUE)
}

library(data.table)
setwd("C:/Users/s-das/Syncplicity/MyProjects_IMP/MY_Papers_V2/TRB 2021/EScotter_BayesianRule/")
it01 <- read.csv("EScotter_Fin2a.csv", header=T, na.strings=c("","NA"))
mn01 <- it01[, c("SEVERITY_CD", "DR_SEX", "DR_AGE")]
mn02= mn01[complete.cases(mn01),]
dim(mn02)
## [1] 491   3
dim(mn01)
## [1] 574   3
head(mn02)
##   SEVERITY_CD DR_SEX DR_AGE
## 1           C      F     28
## 2           C      M     34
## 3           D      F     23
## 4           D      F     18
## 5           D      M     33
## 6           C      M     36

Figure

mn02= subset(mn02, DR_AGE < 100)
mn02$age_group <- autocut(as.integer(mn02$DR_AGE))

map <- age_pyramid(mn02, age_group, split_by = DR_SEX)
map

Option 2

Data

mn03= subset(mn01, DR_AGE < 100)
mn03$age_group <- autocut(as.integer(mn03$DR_AGE))
dim(mn03)
## [1] 476   4
head(mn03)
##   SEVERITY_CD DR_SEX DR_AGE age_group
## 1           C      F     28   (20,40]
## 2           C      M     34   (20,40]
## 3           D      F     23   (20,40]
## 4           D      F     18    [0,20]
## 5           D      M     33   (20,40]
## 6           C      M     36   (20,40]

Figure

age_pyramid(mn03, age_group, split_by = DR_SEX, na.rm = FALSE)