ggpyramid: Function for plotting population pyramids with ggplot2

Description Usage Arguments Value Examples

View source: R/ggpyramid.R

Description

Function for plotting population pyramids with ggplot2

Usage

1
2
3
4
5
6
7
8
9
ggpyramid(
  data = data,
  age = age,
  values = pop,
  gender = gender,
  men = "Men",
  women = "Women",
  fill = gender
)

Arguments

data

Data frame with population data by age and gender

age

Column with the age groups

values

Column with population counts

gender

Column with gender data

men

How males are define in "gender" (default = "Men")

women

How females are defined in "gender" (default = "Women")

fill

Fill bars based on another variable (default = gender)

Value

population pyramid

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
library(tidyverse)
library(ggpyramid)
library(danstat)

# Load data from Denmark using (danstat)
var_input <- list(list(code = "OMRÃ…DE", values = "000"),
                  list(code = "KØN", values = c(1, 2)),
                  list(code = "ALDER", values = seq(0, 125, 1)),
                  list(code = "tid", values = "2020K4"))

df <- get_data("FOLK1A", variables = var_input) %>%
  rename(country = OMRÃ…DE,
         gender = KØN,
         age = ALDER,
         year = TID,
         pop = INDHOLD) %>%
  mutate(age = parse_number(age),
         age = as.integer(age))

df %>%
  ggpyramid(values = pop, fill = gender) +
  scale_fill_manual(name = "Gender", values = c("#0072B2", "#F0E442")) +
  labs(title = "Population pyramith of Denmark",
       caption = "Source: Statistics Denmark")

javiereliomedina/ggpyramid documentation built on Dec. 20, 2021, 9:08 p.m.