age_pyramid: Age-pyramid plot function

View source: R/age_pyramid.R

age_pyramidR Documentation

Age-pyramid plot function

Description

Plot an age pyramid using ggplot2.

Usage

age_pyramid(
  df,
  age_var = "Alder",
  gender_var = "Kon",
  man_level = "Man",
  age_breaks = c(0, 39, 44, 49, 54, 59, 64, 69, 74, 79, 84, Inf),
  age_labels = c("0-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74",
    "75-79", "80-84", "85+"),
  percent = TRUE,
  x_breaks = 5,
  x_breaks_end = x_breaks * 1e+05,
  title = "",
  subtitle = NULL,
  title_size = 14,
  subtitle_size = 10,
  title_margin = 1,
  y_lab = NULL,
  x_lab = NULL,
  background_color = "moccasin",
  panel_grid_color = "grey",
  panel_grid_size = 0.3,
  contour_line_size = 0.1,
  axis_size = 0.3,
  axis_text_angle = 0,
  text_size = 8,
  fill_colors = c("#377EB8", "#E41A1C"),
  legend_pos = c(0, 0),
  legend_labels = ggplot2::waiver(),
  label_breaks = ggplot2::waiver(),
  legend_row = NULL,
  legend_col = NULL
)

Arguments

df

Data frame.

age_var

Name of age variable.

gender_var

Name of gender variable.

man_level

Name of man level, probably "Man" or "Män".

age_breaks

Each age observation in an interval.

age_labels

Label of the interval.

percent

If TRUE, x-axis is in percent form. Otherwise in count form.

x_breaks

Length between each break on x-axis.

x_breaks_end

Break end, default for 100000. Works for all count values below that.

title, subtitle

Plot title/subtitle, NULL for no title.

title_size, subtitle_size

Text size of title/subtitle in pt.

title_margin

Margin for title

x_lab, y_lab

X/Y-axis labels, use NULL for no label.

background_color

Color of the panel background.

panel_grid_color

Color of the panel grid lines.

panel_grid_size

Size of the panel grid lines in plot and contour lines around bars, useful to change if large dpi!

contour_line_size

Passed to argument "size" to geom_bar

axis_size

Size of the axis lines.

axis_text_angle

Angle of the tick texts, 45 is recommended for many x levels.

text_size

Size of the text in pt.

fill_colors

Colors of the genders.

legend_pos

Position of the legend in plot, if c(1,1), c(1,0) etc, legend inside plot.

legend_labels

Label for each legend key.

label_breaks

Order of the legend keys.

legend_row, legend_col

How many rows/columns for the legends.

Value

ggplot object containing age pyramid plot.

Examples

# Creating data
df <- data.frame(age = rpois(100000, 65),
  gender = sample(c('Woman', 'Woman', 'Man'), 100000, replace = TRUE))

# Age pyramid
age_pyramid(df, age_var = 'age', gender_var = 'gender',
  man_level = 'Man', title = "This is an age pyramid")

# Age pyramid with percent = FALSE
age_pyramid(df, age_var = 'age', gender_var = 'gender',
  man_level = 'Man', percent = FALSE, x_breaks = 5000,
  title = "This is an age pyramid")


swehip/shprplotfun documentation built on Oct. 21, 2022, 8:26 a.m.