age_pyramid | R Documentation |
Plot an age pyramid using ggplot2.
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 )
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 |
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. |
ggplot object containing age pyramid plot.
# 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.