Description Usage Arguments Value Examples
Function for plotting population pyramids with ggplot2
1 2 3 4 5 6 7 8 9 |
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) |
population pyramid
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")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.