| Title: | Icon-Based Population Charts and Plots for 'ggplot2' |
|---|---|
| Description: | Create engaging population charts and point plots in R. 'ggpop' allows users to represent population data and points proportionally using customizable icons, facilitating the creation of circular representative population charts as well as any point-plots. |
| Authors: | Jorge A. Roa-Contreras [aut, cre] (ORCID: <https://orcid.org/0000-0002-3972-9793>), Ralitza Soultanova [aut] (ORCID: <https://orcid.org/0009-0000-9324-5653>), Fernando Alarid-Escudero [aut] (ORCID: <https://orcid.org/0000-0001-5076-1172>), Carlos Pineda-Antunez [aut] (ORCID: <https://orcid.org/0000-0002-8352-7080>) |
| Maintainer: | Jorge A. Roa-Contreras <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.9.0 |
| Built: | 2026-10-29 12:48:41 UTC |
| Source: | https://github.com/jurjoroa/ggpop |
ggpop is a ggplot2 extension for creating icon-based population charts
and pictogram plots. Use geom_pop() and geom_icon_point() to visualize
proportion and population data with 2,000+ Font Awesome icons.
geom_pop() – proportional icon grids
geom_icon_point() – icon scatter plots
process_data() – prepare count data for plotting
fa_icons() – search Font Awesome icon names
theme_pop(), theme_pop_dark(), theme_pop_minimal() – built-in themes
Converts count data to one row per icon. group_var and sum_var are
unquoted; high_group_var takes a character string for faceted charts.
df_plot <- process_data(
data = data.frame(sex = c("Female", "Male"), n = c(55, 45)),
group_var = sex,
sum_var = n,
sample_size = 20
)
Draws icon grids. Add an icon column, map icon and color in aes().
Do not map x or y.
ggplot() + geom_pop(data = df_plot, aes(icon = icon, color = type), size = 2) + scale_color_manual(values = c(Female = "#C0392B", Male = "#2980B9")) + theme_pop()
Drop-in replacement for geom_point() using Font Awesome icons.
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_icon_point(icon = "seedling", size = 1)
Search the bundled Font Awesome icon list by keyword.
fa_icons(query = "person")
Three built-in themes optimized for icon charts:
theme_pop(), theme_pop_dark(), theme_pop_minimal().
Maintainer: Jorge A. Roa-Contreras [email protected] (ORCID)
Authors:
Ralitza Soultanova [email protected] (ORCID)
Fernando Alarid-Escudero [email protected] (ORCID)
Carlos Pineda-Antunez [email protected] (ORCID)
Useful links:
library(ggplot2) library(dplyr) ## ------------------------------------------------------- ## geom_pop(): population icon grid ## ------------------------------------------------------- df_plot <- process_data( data = data.frame(sex = c("Female", "Male"), n = c(55, 45)), group_var = sex, sum_var = n, sample_size = 20 ) %>% mutate(icon = ifelse(type == "Female", "person-dress", "person")) ggplot() + geom_pop(data = df_plot, aes(icon = icon, color = type), size = 2) + scale_color_manual(values = c(Female = "#C0392B", Male = "#2980B9")) + theme_pop() + labs(title = "Population by sex", color = NULL) ## ------------------------------------------------------- ## geom_icon_point(): icon scatter plot ## ------------------------------------------------------- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_icon_point(icon = "seedling", size = 1) + scale_color_manual(values = c( setosa = "#43A047", versicolor = "#1E88E5", virginica = "#E53935" )) + labs(title = "Iris dataset", x = "Sepal Length", y = "Petal Length")library(ggplot2) library(dplyr) ## ------------------------------------------------------- ## geom_pop(): population icon grid ## ------------------------------------------------------- df_plot <- process_data( data = data.frame(sex = c("Female", "Male"), n = c(55, 45)), group_var = sex, sum_var = n, sample_size = 20 ) %>% mutate(icon = ifelse(type == "Female", "person-dress", "person")) ggplot() + geom_pop(data = df_plot, aes(icon = icon, color = type), size = 2) + scale_color_manual(values = c(Female = "#C0392B", Male = "#2980B9")) + theme_pop() + labs(title = "Population by sex", color = NULL) ## ------------------------------------------------------- ## geom_icon_point(): icon scatter plot ## ------------------------------------------------------- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_icon_point(icon = "seedling", size = 1) + scale_color_manual(values = c( setosa = "#43A047", versicolor = "#1E88E5", virginica = "#E53935" )) + labs(title = "Iris dataset", x = "Sepal Length", y = "Petal Length")
Retrieves Font Awesome icon names, optionally filtered by a search query or category. Results can be returned as a plain character vector or as a tibble with category classification.
fa_icons( query = NULL, category = NULL, regex = FALSE, classify = TRUE, include_unclassified = TRUE, class_map = NULL, primary_only = TRUE, as_vector = FALSE )fa_icons( query = NULL, category = NULL, regex = FALSE, classify = TRUE, include_unclassified = TRUE, class_map = NULL, primary_only = TRUE, as_vector = FALSE )
query |
Character string. Filter icons whose names contain |
category |
Character vector. One or more category names to filter by.
Run |
regex |
Logical. When |
classify |
Logical. When |
include_unclassified |
Logical. When |
class_map |
A named list mapping category names to regex patterns.
Defaults to the internal |
primary_only |
Logical. When |
as_vector |
Logical. When |
When as_vector = TRUE, a sorted character vector of icon names.
Otherwise a tibble with columns:
Icon name (character).
Primary category the icon belongs to, or NA
when unclassified (character).
All matching categories (list-column of character
vectors). Only present when primary_only = FALSE.
# All icons as a classified tibble fa_icons() # Quick lookup -- plain sorted vector head(fa_icons(as_vector = TRUE), 10) # Search for icons whose name contains "heart" fa_icons(query = "heart") # Filter by category fa_icons(category = "animals") # Regex search -- all icons starting with "arrow" fa_icons(query = "^arrow", regex = TRUE)# All icons as a classified tibble fa_icons() # Quick lookup -- plain sorted vector head(fa_icons(as_vector = TRUE), 10) # Search for icons whose name contains "heart" fa_icons(query = "heart") # Filter by category fa_icons(category = "animals") # Regex search -- all icons starting with "arrow" fa_icons(query = "^arrow", regex = TRUE)
df_coordinates_final DatasetDownloads and caches the df_coordinates_final dataset if it is not already cached locally.
This function ensures that the dataset is downloaded only once and loaded into memory
without cluttering the global environment. The dataset is stored in a package-specific
cache directory and retrieved efficiently for subsequent uses.
fetch_df_coordinates()fetch_df_coordinates()
The dataset is downloaded from GitHub
The file is cached in a directory specific to the package, which is determined
using R_user_dir. If the dataset is already cached, it will
be loaded directly from the cache instead of downloading again.
A data frame containing the df_coordinates_final dataset.
df <- fetch_df_coordinates() head(df)df <- fetch_df_coordinates() head(df)
Works exactly like geom_point(), but renders Font Awesome icons instead of dots. Pass any data with x and y variables - no special formatting required.
geom_icon_point( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, icon = NULL, size = 1, dpi = 50, legend_icons = TRUE, stroke_width = NULL, icon_path = NULL, ... )geom_icon_point( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, icon = NULL, size = 1, dpi = 50, legend_icons = TRUE, stroke_width = NULL, icon_path = NULL, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
logical, whether remove NA values |
show.legend |
Logical. Should this layer be included in the legends?
|
inherit.aes |
If |
icon |
Default icon (default: NULL). Accepts a Font Awesome name, a
bundled ggpop marker name (e.g. |
size |
Default icon size (default: 1). |
dpi |
Icon resolution (default: 50). |
legend_icons |
Show icons in legend (default: TRUE). |
stroke_width |
Numeric. Width of the icon outline/stroke. |
icon_path |
Optional path to a folder of your own SVG icons, referenced
by file name (without |
... |
additional parameters |
A ggplot layer.
geom_icon_point uses standard ggplot2 scatter plot aesthetics:
x - Numeric variable for x-axis
y - Numeric variable for y-axis
icon - Font Awesome icon name (optional, column or mapped)
color/colour - Color grouping
alpha - Transparency
size - Icon size
library(ggplot2) data <- data.frame( x = rnorm(20), y = rnorm(20), category = sample(c("A", "B", "C"), 20, replace = TRUE), icon = sample(c("heart", "star", "circle"), 20, replace = TRUE) ) # Map icon to a column ggplot(data, aes(x = x, y = y, icon = icon, color = category)) + geom_icon_point() # Use a fixed icon ggplot(data, aes(x = x, y = y, color = category)) + geom_icon_point(icon = "star")library(ggplot2) data <- data.frame( x = rnorm(20), y = rnorm(20), category = sample(c("A", "B", "C"), 20, replace = TRUE), icon = sample(c("heart", "star", "circle"), 20, replace = TRUE) ) # Map icon to a column ggplot(data, aes(x = x, y = y, icon = icon, color = category)) + geom_icon_point() # Use a fixed icon ggplot(data, aes(x = x, y = y, color = category)) + geom_icon_point(icon = "star")
Draws a circular representative population chart based on group proportions, where each point (person) represents a fixed number of individuals. Each person is rendered as a Font Awesome icon.
geom_pop( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, icon = "ggmale", group_var = NULL, sample_size = NULL, arrange = FALSE, seed = NULL, sum_var = NULL, facet = NULL, size = 1, dpi = 50, legend_icons = TRUE, stroke_width = NULL, icon_path = NULL, ... )geom_pop( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, icon = "ggmale", group_var = NULL, sample_size = NULL, arrange = FALSE, seed = NULL, sum_var = NULL, facet = NULL, size = 1, dpi = 50, legend_icons = TRUE, stroke_width = NULL, icon_path = NULL, ... )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
position |
A position adjustment to use on the data for this layer. This
can be used in various ways, including to prevent overplotting and
improving the display. The
|
na.rm |
logical, whether remove NA values |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
icon |
Default icon used when no |
group_var |
(Deprecated) Use |
sample_size |
The total number of individuals (points) to draw. |
arrange |
Logical; if TRUE, output data is arranged by group. |
seed |
Optional numeric seed used only when |
sum_var |
Optional variable to sum over instead of counting. |
facet |
Optional faceting variable. If provided, final plot must be faceted
with ggplot2 (use |
size |
Icon size. If mapped in |
dpi |
Height (in pixels) of the rendered PNG when using
|
legend_icons |
Logical; if TRUE, legend displays the selected icons. |
stroke_width |
Numeric. Width of the icon outline in pixels (single value). |
icon_path |
Optional path to a folder of your own SVG icons, referenced
by file name (without |
... |
additional parameters |
A ggplot layer that renders a circular population chart with icons.
geom_pop understands the following aesthetics:
icon: Font Awesome icon name (mapped column)
group: grouping variable for raw data mode
color/colour: icon color
alpha: transparency (must be mapped)
size: icon size (mapped or fixed)
geom_icon_point, process_data,
geom_image
library(ggplot2) df <- data.frame( sex = rep(c("F", "M"), each = 10), icon = rep(c("female", "male"), each = 10) ) ggplot() + geom_pop( data = df, aes(icon = icon, group = sex, color = sex), size = 3, dpi = 80 )library(ggplot2) df <- data.frame( sex = rep(c("F", "M"), each = 10), icon = rep(c("female", "male"), each = 10) ) ggplot() + geom_pop( data = df, aes(icon = icon, group = sex, color = sex), size = 3, dpi = 80 )
Returns the bundled ggpop marker names and, if an icon directory is given,
the names of the user SVGs found there. These names (plus any Font Awesome
name) are valid values for the icon aesthetic of geom_pop() and
geom_icon_point().
ggpop_markers(icon_path = getOption("ggpop.icon_path"))ggpop_markers(icon_path = getOption("ggpop.icon_path"))
icon_path |
Optional path to a folder of user SVG icons. Defaults to
|
A list with element bundled (character vector of marker names)
and, when icon_path resolves to a directory, user.
ggpop_markers()ggpop_markers()
Derives the icon row/column positions from the unique combinations in
df, returning a plain data frame ready to rbind() with
group and symbol rows before passing to legend_canvas.
icon_grid(df, icon, label, row, col, label_fn = NULL, section = "grid")icon_grid(df, icon, label, row, col, label_fn = NULL, section = "grid")
df |
Data frame used in the ggplot call. |
icon |
Column name holding icon names. |
label |
Column name holding cell labels. |
row |
Column whose unique values define grid rows. |
col |
Column whose unique values define grid columns. |
label_fn |
Optional function applied to label values before display. |
section |
Value for the |
A data frame with columns section, type,
label, color, icon, row, col.
Rows are sorted by row then column (factor level order respected).
# df_icons <- icon_grid( # df, icon = "icon", label = "AgeLabel", # row = "StartAge", col = "StopAge", # label_fn = function(x) gsub(" ", "", x) # )# df_icons <- icon_grid( # df, icon = "icon", label = "AgeLabel", # row = "StartAge", col = "StopAge", # label_fn = function(x) gsub(" ", "", x) # )
Adds a column of symbol + label entries to an existing ggplot canvas
(typically the output of marker_legend) using the same
ggplot2::annotate() approach. The coordinate system is shared with
the base plot, so positions integrate seamlessly with the rest of the legend.
Four entry types are supported:
A filled rectangle (colour bands, modality tiles).
A horizontal segment (frontier or trend lines).
A bold "*" glyph rendered as text.
A label with no visible key symbol.
A fourth entry kind, icon, exists in legend_canvas's
df_legend vocabulary but is not a key_legend() type - icon
rows are rendered separately via marker_legend.
Two y-placement modes:
y_start = NULL (default) - first entry is placed at the
section-title row (row_spacing * title_frac), matching the
position where marker_legend() puts section headers. Use this
when entry\,1 is both the title and its own symbol (e.g. the
"Efficient frontier" line entry).
y_start = 0 - first entry at row\,1, second at
-row_spacing, etc. Use this with a text-only title.
key_legend( entries, x = 0, y_start = NULL, title = NULL, title_frac = 0.85, row_spacing = 1, key_width = 1.2, label_gap = 0.3, label_size = 2.8, label_color = "black", label_inside = FALSE, label_fontface = "plain", title_color = NULL, swatch_height = 0.45, point_size = 1.6 )key_legend( entries, x = 0, y_start = NULL, title = NULL, title_frac = 0.85, row_spacing = 1, key_width = 1.2, label_gap = 0.3, label_size = 2.8, label_color = "black", label_inside = FALSE, label_fontface = "plain", title_color = NULL, swatch_height = 0.45, point_size = 1.6 )
entries |
A data frame with columns |
x |
Left edge of the key-symbol column in plot coordinates. |
y_start |
Y coordinate of the first entry. |
title |
Optional text-only section title drawn at
|
title_frac |
Y-fraction used for the section-title row
(default |
row_spacing |
Vertical distance between rows. Match the
|
key_width |
Horizontal width of the key symbol area. |
label_gap |
Gap between the key symbol and the label text. |
label_size |
Text size (passed to |
label_color |
Colour of label text. |
label_inside |
When |
label_fontface |
Font face for the title and entry labels (default
|
title_color |
Colour of the section title. Inherits
|
swatch_height |
Height of swatch rectangles as a fraction of
|
point_size |
Size multiplier for point glyphs relative to
|
A ggpop_key_legend object. Add it to any ggplot
with + to inject the annotate layers onto that canvas. Print it
(or use it standalone) to render a self-contained legend panel.
library(ggplot2) s <- 0.78 rs <- 0.34 * s ef <- data.frame( type = c("line", "swatch", "point"), label = c("Efficient frontier", "Grey zone", "Near efficient"), color = c("black", "grey50", "black"), stringsAsFactors = FALSE ) # Standalone panel: print(key_legend(ef, row_spacing = rs, key_width = 0.22)) # Added to a marker_legend canvas: # p <- marker_legend(age_entries, ...) + # key_legend(ef, x = 1.10, row_spacing = rs, key_width = 0.22)library(ggplot2) s <- 0.78 rs <- 0.34 * s ef <- data.frame( type = c("line", "swatch", "point"), label = c("Efficient frontier", "Grey zone", "Near efficient"), color = c("black", "grey50", "black"), stringsAsFactors = FALSE ) # Standalone panel: print(key_legend(ef, row_spacing = rs, key_width = 0.22)) # Added to a marker_legend canvas: # p <- marker_legend(age_entries, ...) + # key_legend(ef, x = 1.10, row_spacing = rs, key_width = 0.22)
The new row is appended to the symbol section without changing spec.
Render the returned specification to measure its border after the new row.
legend_add_key( spec, label, type = "line", colour = "black", linetype = "solid", linewidth = 0.8, pch = NA )legend_add_key( spec, label, type = "line", colour = "black", linetype = "solid", linewidth = 0.8, pch = NA )
spec |
A |
label |
Text beside the symbol. |
type |
One of |
colour |
Colour of the symbol. |
linetype, linewidth
|
Line style and width for a line row. |
pch |
Character or numeric point glyph for a point row. |
A new ggpop_legend_spec object.
legend_spec(), legend_subset(), legend_render()
grid <- data.frame(row = 1, col = 1, icon = "square-inset", label = "A") spec <- legend_spec(grid, symbols = c(Frontier = "line")) legend_add_key(spec, "Capacity", type = "line", linetype = "dashed")grid <- data.frame(row = 1, col = 1, icon = "square-inset", label = "A") spec <- legend_spec(grid, symbols = c(Frontier = "line")) legend_add_key(spec, "Capacity", type = "line", linetype = "dashed")
legend_canvas positions its sections from nominal geometry,
but text labels overflow their anchor points by an amount that depends on
the font, the label strings, and the final output size - none of which are
known when the layers are built. A border drawn from that nominal geometry
therefore clips the labels. legend_box() sidesteps this by rendering
the legend once at the intended output size, measuring the true pixel extent
of the drawn content, mapping it back to data coordinates, and adding a
border rectangle around it.
Because it renders once to measure, the border reflects exactly what will be drawn - long labels, mixed fonts, any content - with no per-figure hand tuning.
legend_box( plot, width, height, padding = c(0.02, 0.12), colour = "black", linewidth = 0.7, fill = NA, threshold = 150, dpi = 150 )legend_box( plot, width, height, padding = c(0.02, 0.12), colour = "black", linewidth = 0.7, fill = NA, threshold = 150, dpi = 150 )
plot |
A |
width, height
|
Physical size in inches of the region the legend will be
drawn in for the final export - pass the same values used there so the
measured text width matches. For a |
padding |
Length-2 numeric: gap between content and border, as
fractions of the measured content width and height
(default |
colour |
Border colour (default |
linewidth |
Border line width (default |
fill |
Border fill (default |
threshold |
Colour-channel cutoff (0-255) for detecting content; a
pixel counts as content when its darkest red, green or blue channel is
below this value (default |
dpi |
Resolution of the internal measurement render (default
|
plot with a border ggplot2::annotate("rect", ...)
layer added.
Renders a legend from a data frame that combines icon grid rows (built with
icon_grid), colour tile rows, and typed-symbol rows via
rbind(). Layout parameters determine where each section is
positioned. The scale parameter multiplies all size and spacing
values so the data frame can be written in round numbers.
legend_canvas( df_legend, grid_section = "grid", grid_title = NULL, group_section = NULL, group_title = NULL, group_width = NULL, group_gap = 0.08, group_label_size = NA_real_, group_label_color = "white", group_swatch_height = 0.44, symbol_section = NULL, symbol_right_gap = 0.3, symbol_key_width = 0.2, symbol_label_gap = NULL, col_spacing, row_spacing, label_gap, marker_size, label_size, label_fontface = "plain", scale = 1, label_scale = 1, dpi = 300, xlim = NULL, ylim = NULL, x_margin = c(1, 1), align = NULL, y_margin = c(1.1, 1.1), valign = NULL, clip = "off" )legend_canvas( df_legend, grid_section = "grid", grid_title = NULL, group_section = NULL, group_title = NULL, group_width = NULL, group_gap = 0.08, group_label_size = NA_real_, group_label_color = "white", group_swatch_height = 0.44, symbol_section = NULL, symbol_right_gap = 0.3, symbol_key_width = 0.2, symbol_label_gap = NULL, col_spacing, row_spacing, label_gap, marker_size, label_size, label_fontface = "plain", scale = 1, label_scale = 1, dpi = 300, xlim = NULL, ylim = NULL, x_margin = c(1, 1), align = NULL, y_margin = c(1.1, 1.1), valign = NULL, clip = "off" )
df_legend |
Data frame with columns
|
grid_section |
Value of |
grid_title |
Title drawn above the icon grid ( |
group_section |
Value of |
group_title |
Title drawn above the colour tiles. Inherits
|
group_width |
Width of the colour tile section (scaled by
|
group_gap |
Gap between the tile right edge and |
group_label_size |
Label size inside tiles; inherits |
group_label_color |
Label colour inside tiles (default |
group_swatch_height |
Tile height as fraction of |
symbol_section |
Value of |
symbol_right_gap |
Gap between icon grid right edge and symbol section
(default |
symbol_key_width |
Width of the key symbol area (default |
symbol_label_gap |
Gap between key symbol and label; inherits
|
col_spacing |
Horizontal distance between icon grid columns. |
row_spacing |
Vertical distance between rows. |
label_gap |
Default gap between key symbol and label. |
marker_size |
Icon size for the grid. |
label_size |
Default label text size. |
label_fontface |
Font face for every title and label in the legend
(grid title, group title/tile labels, symbol title/labels) - default
|
scale |
Multiplier applied to every length and size:
|
label_scale |
Extra multiplier applied on top of |
dpi |
Icon render resolution (default |
xlim |
Length-2 numeric; x limits of the canvas. Auto-computed
when |
ylim |
Length-2 numeric; y limits of the canvas. Auto-computed
when |
x_margin |
Length-2 numeric: left/right padding added to auto x range
(default |
align |
Optional convenience for biasing |
y_margin |
Length-2 numeric: top/bottom padding as multiples of
(scaled) |
valign |
Optional convenience for biasing |
clip |
Passed to |
A ggplot ready to save or pass to legend_strip.
icon_grid, key_legend,
legend_strip
Opinionated wrapper over legend_canvas + legend_box
for the recurring three-section legend (an icon grid, a block of colour
tiles, and a small typed-symbol key). You supply the content
(df_legend) and the strip size; the wrapper applies a fixed
proportion ladder (legend_ratios) driven by three base sizes,
centres the content in the strip, and fits a border to the rendered result.
Every layout length is a multiple of one module; every text size is a
multiple of one text base; icons use one marker size. A
label_size column on the symbol-section rows is read as multiples of
text.
The base sizes are calibrated for a base_width-inch strip and are
scaled by width / base_width, so a legend keeps identical proportions
at any output width - pass your width and the text, markers, and
layout all follow. The group and symbol blocks may hold more entries than
the grid has rows; the border grows to enclose whichever section runs
deepest.
The group colour swatches are rectangles in data coordinates, so shrinking
content_range to make sparse content fill a wide strip stretches them
into banners while the fixed-size icons stay put. legend_composite()
warns (class "ggpop_swatch_aspect_warning") when the rendered swatch
aspect ratio gets banner-like, pointing you to raise content_range or
reduce width - keep sparse legends compact rather than stretched.
legend_composite( df_legend, width, height, grid_title = NULL, group_title = NULL, grid_section = "grid", group_section = "group", symbol_section = "symbol", module = 0.44294, text = 7.756, marker = 4.562, content_range = 7.21636, base_width = 27, swatch_height = 0.8, fontface = "plain", border = "#231F20", border_padding = c(0.018, 0.09), ratios = legend_ratios(), dpi = 300 )legend_composite( df_legend, width, height, grid_title = NULL, group_title = NULL, grid_section = "grid", group_section = "group", symbol_section = "symbol", module = 0.44294, text = 7.756, marker = 4.562, content_range = 7.21636, base_width = 27, swatch_height = 0.8, fontface = "plain", border = "#231F20", border_padding = c(0.018, 0.09), ratios = legend_ratios(), dpi = 300 )
df_legend |
Legend content, as for |
width, height
|
Physical size (inches) of the strip the legend will fill
(passed to |
grid_title, group_title
|
Section titles ( |
grid_section, group_section, symbol_section
|
|
module |
Layout module: one row / one column pitch, in legend units. |
text |
Base text size (ggplot mm) - the primary label size. |
marker |
Icon marker size ( |
content_range |
Total data-x range the strip maps to; larger renders a smaller legend. The content is centred within it. |
base_width |
Output width (inches) at which the base sizes
( |
swatch_height |
Colour-tile height as a fraction of a row. |
fontface |
Font face for all titles and labels. |
border |
Border colour ( |
border_padding |
Length-2 |
ratios |
Proportion ladder; defaults to |
dpi |
Icon render resolution. |
A ggplot with a fitted border, ready for
legend_strip.
legend_canvas, legend_box,
legend_ratios, legend_strip
legend_composite
Returns the fixed layout proportions used by legend_composite
as a named list. Lengths are multiples of the layout module (see the
module argument there); the two *_label entries are multiples
of the text base (text there). Override individual entries and pass
the result back via legend_composite(ratios = ...).
legend_ratios()legend_ratios()
A named list of proportions.
Render a composite legend specification
legend_render(spec, width, height)legend_render(spec, width, height)
spec |
A |
width, height
|
Physical legend size in inches. |
A ggplot with a border fitted to the final content.
grid <- data.frame(row = 1, col = 1, icon = "square-inset", label = "A") spec <- legend_spec(grid, keys = c(Group = "#1090F3")) legend_render(spec, width = 7, height = 1.5)grid <- data.frame(row = 1, col = 1, icon = "square-inset", label = "A") spec <- legend_spec(grid, keys = c(Group = "#1090F3")) legend_render(spec, width = 7, height = 1.5)
Stores icon-grid rows, named colour keys, and typed symbol rows as content.
legend_render() lays out the content and fits its border at the requested
physical size. The specification can later be reused for related legends.
legend_spec( grid, keys = NULL, symbols = NULL, key_columns = 1L, grid_title = NULL, group_title = NULL, symbol_title = NULL, group_key_width = 0.2375, group_label_gap = 0.1187, group_gap = 0.591, key_column_gap = 0.317, group_title_nudge = 0, swatch_height = 0.56, xlim = NULL, label_size = 3.634, label_fontface = "plain", marker_size = 3.717, dpi = 120, ratios = legend_ratios(), border = "#231F20", border_padding = c(0.018, 0.09) )legend_spec( grid, keys = NULL, symbols = NULL, key_columns = 1L, grid_title = NULL, group_title = NULL, symbol_title = NULL, group_key_width = 0.2375, group_label_gap = 0.1187, group_gap = 0.591, key_column_gap = 0.317, group_title_nudge = 0, swatch_height = 0.56, xlim = NULL, label_size = 3.634, label_fontface = "plain", marker_size = 3.717, dpi = 120, ratios = legend_ratios(), border = "#231F20", border_padding = c(0.018, 0.09) )
grid |
A data frame from |
keys |
Named character vector of colour values. Names are key labels. |
symbols |
Named character vector of |
key_columns |
Number of colour-key columns, filled from top to bottom. |
grid_title, group_title
|
Optional titles for the icon grid and colour keys. |
symbol_title |
Optional title for the symbol column. With no title the first symbol row sits on the title line, so it doubles as the column heading. With a title, the heading takes that line and the symbols start on the same rows as the icon grid and colour keys. |
group_key_width, group_label_gap, swatch_height
|
Dimensions of the
external colour keys in grid coordinates. |
group_gap |
Gap from the rightmost colour-key label to the icon grid. |
key_column_gap |
Gap between colour-key columns. |
group_title_nudge |
Horizontal adjustment to the centred group title. |
xlim |
Optional fixed horizontal canvas limits. |
label_size |
Text size in millimetres. |
label_fontface |
Font face for every title and label: |
marker_size |
Icon size passed to |
dpi |
Icon rendering resolution. |
ratios |
Layout proportions from |
border, border_padding
|
Border colour and padding passed to
|
A ggpop_legend_spec object to pass to legend_render().
legend_canvas(), legend_composite()
grid <- data.frame( row = c(1, 2), col = c(1, 1), icon = c("square-inset", "circle-solid"), label = c("A", "B") ) spec <- legend_spec(grid, keys = c(Group1 = "#1090F3", Group2 = "#DF4601"), symbols = c(Frontier = "line"), key_columns = 2) legend_render(spec, width = 7, height = 1.5)grid <- data.frame( row = c(1, 2), col = c(1, 1), icon = c("square-inset", "circle-solid"), label = c("A", "B") ) spec <- legend_spec(grid, keys = c(Group1 = "#1090F3", Group2 = "#DF4601"), symbols = c(Frontier = "line"), key_columns = 2) legend_render(spec, width = 7, height = 1.5)
Returns a ggpop_legend_strip object. When added to a ggplot
with +, produces a ggpop_composite that stacks the main plot
above the strip at the specified physical height. The composite works with
ggplot2::ggsave() and print(). The main plot (the object
legend_strip() is added to) may itself be a patchwork object
(e.g. several panels combined with +/plot_layout)
this requires the patchwork package to be installed.
legend_strip(strip_plot, height)legend_strip(strip_plot, height)
strip_plot |
A |
height |
Height of the strip in inches. |
A ggpop_legend_strip object; add it to a ggplot with
+.
# p_legend <- marker_legend(entries, ...) + key_legend(...) # p_full <- p_scatter + legend_strip(p_legend, height = 1.326) # ggplot2::ggsave("out.png", p_full, width = 10.5, height = 8.826, dpi = 150)# p_legend <- marker_legend(entries, ...) + key_legend(...) # p_full <- p_scatter + legend_strip(p_legend, height = 1.326) # ggplot2::ggsave("out.png", p_full, width = 10.5, height = 8.826, dpi = 150)
Creates a new specification without changing spec. Grid rows are
renumbered from one so the selected grid stays compact. Render the result
with legend_render() after selecting its final content.
legend_subset( spec, grid_rows = NULL, keys = NULL, symbols = NULL, key_columns = NULL, group_gap = spec$group_gap, group_title_nudge = spec$group_title_nudge, xlim = spec$xlim )legend_subset( spec, grid_rows = NULL, keys = NULL, symbols = NULL, key_columns = NULL, group_gap = spec$group_gap, group_title_nudge = spec$group_title_nudge, xlim = spec$xlim )
spec |
A |
grid_rows |
Grid row positions to keep, in display order. |
keys |
Names of colour keys to keep, in display order. |
symbols |
Symbol labels to keep, in display order. |
key_columns |
Number of colour-key columns in the result. When |
group_gap, group_title_nudge, xlim
|
Optional layout overrides. Each
inherits from |
A new ggpop_legend_spec object.
legend_spec(), legend_add_key(), legend_render()
grid <- data.frame(row = c(1, 2), col = c(1, 1), icon = c("square-inset", "circle-solid"), label = c("A", "B")) spec <- legend_spec(grid, keys = c(One = "#1090F3", Two = "#DF4601")) legend_subset(spec, grid_rows = 1, keys = "One")grid <- data.frame(row = c(1, 2), col = c(1, 1), icon = c("square-inset", "circle-solid"), label = c("A", "B")) spec <- legend_spec(grid, keys = c(One = "#1090F3", Two = "#DF4601")) legend_subset(spec, grid_rows = 1, keys = "One")
A marker scheme maps one value to a shape family and another to a variant.
Regular markers combine the two names with a hyphen. One-time values map
directly to a marker, while none uses the scheme's fallback marker.
marker_encode(start, stop, once = FALSE, none = FALSE, scheme = "sda2028")marker_encode(start, stop, once = FALSE, none = FALSE, scheme = "sda2028")
start, stop
|
Vectors of values used to select shape family and variant.
Numeric, character, and factor values are accepted. Values may be missing
on rows selected by |
once, none
|
Logical vectors marking one-time and no-screening rows.
Scalars are recycled. The two flags cannot both be |
scheme |
|
The built-in "sda2028" scheme uses screening start ages 45/50/55 for
square/circle/diamond and stop ages 70/75/80/85 for
inset/hollow/cross/solid. One-time screening uses solid markers at ages
45/50/55, "plus-bold" at 60, and "triangle-down" at 65.
A character vector of icon names, one per input row.
ggpop_markers(), geom_icon_point()
marker_encode(c(45, 50, NA), c(70, 85, NA), none = c(FALSE, FALSE, TRUE)) marker_encode(c(45, 60), c(NA, NA), once = TRUE) other <- list( start = c(A = "square"), stop = c(B = "hollow"), once = c(A = "square-solid"), none = "circle-solid" ) marker_encode("A", "B", scheme = other)marker_encode(c(45, 50, NA), c(70, 85, NA), none = c(FALSE, FALSE, TRUE)) marker_encode(c(45, 60), c(NA, NA), once = TRUE) other <- list( start = c(A = "square"), stop = c(B = "hollow"), once = c(A = "square-solid"), none = "circle-solid" ) marker_encode("A", "B", scheme = other)
For an ordinary legend keyed to your plot data you do not need this
function - map an aesthetic and let ggplot2 build the legend natively:
geom_icon_point(..., legend_icons = TRUE) + scale_legend_icon().
Use marker_legend() only for a standalone composite legend that
ggplot2's guide system cannot express - a multi-column grouped legend
decoupled from any plot, often combined with extra annotations and exported
at fixed pixel dimensions (for example the screening-strategy
Legend_*.png figures).
marker_legend( entries, layout = c("column", "grid"), ncol = 1, title = NULL, marker_size = 3, label_size = 2.8, dpi = 300, icon_path = NULL, col_spacing = 10, row_spacing = 1, label_gap = 0.6, label_colour = "black", label_fontface = "plain", default_color = "black" )marker_legend( entries, layout = c("column", "grid"), ncol = 1, title = NULL, marker_size = 3, label_size = 2.8, dpi = 300, icon_path = NULL, col_spacing = 10, row_spacing = 1, label_gap = 0.6, label_colour = "black", label_fontface = "plain", default_color = "black" )
entries |
A data frame of legend rows. Must contain an |
layout |
Legend arrangement. |
ncol |
Number of columns for |
title |
Optional bold title drawn centred above the legend. |
marker_size |
Icon size passed to |
label_size |
Text size for the labels. |
dpi |
Icon rendering resolution passed to |
icon_path |
Optional folder of user |
col_spacing |
Horizontal distance between columns. |
row_spacing |
Vertical distance between rows. |
label_gap |
Horizontal gap between a marker and its label. |
label_colour |
Text colour for the labels (default: |
label_fontface |
Font face for the labels (default: |
default_color |
Marker colour used for rows with no |
Lays out icon + label entries into a self-contained ggplot object.
Each entry is drawn with geom_icon_point, so any icon source is
accepted - Font Awesome names, bundled ggpop markers, or user-supplied
.svg paths (see ggpop_markers) - and the three may be
mixed in a single legend. The result is a plain ggplot you can extend
with further ggplot2::annotate() layers (frontier segments, colour
bands, asterisks) and export at any size with ggplot2::ggsave().
A ggplot object with theme_void() applied.
geom_icon_point, ggpop_markers
# For a normal data-driven legend, prefer the native path instead: # geom_icon_point(aes(icon = icon, colour = group), legend_icons = TRUE) + # scale_legend_icon() # marker_legend() is for a STANDALONE composite legend - here two semantic # colour-columns, the kind ggplot2 guides cannot produce in one figure. df_legend <- data.frame( column = c(1, 1, 2, 2), icon = c("square-inset", "circle-solid", "square-hollow", "diamond-cross"), label = c("Start 45y", "Start 50y", "Stop 75y", "Stop 80y"), colour = c("#FF1493", "#FF1493", "#006400", "#006400"), stringsAsFactors = FALSE ) marker_legend(df_legend, col_spacing = 12)# For a normal data-driven legend, prefer the native path instead: # geom_icon_point(aes(icon = icon, colour = group), legend_icons = TRUE) + # scale_legend_icon() # marker_legend() is for a STANDALONE composite legend - here two semantic # colour-columns, the kind ggplot2 guides cannot produce in one figure. df_legend <- data.frame( column = c(1, 1, 2, 2), icon = c("square-inset", "circle-solid", "square-hollow", "diamond-cross"), label = c("Start 45y", "Start 50y", "Stop 75y", "Stop 80y"), colour = c("#FF1493", "#FF1493", "#006400", "#006400"), stringsAsFactors = FALSE ) marker_legend(df_legend, col_spacing = 12)
The process_data function processes a dataset to calculate group proportions and generates a sampled dataset based on specified parameters. This processed data is suitable for creating visual representations, such as population charts, where each sample represents a group with associated counts and proportions.
process_data( data, high_group_var = NULL, group_var, sum_var = NULL, sample_size = 100 )process_data( data, high_group_var = NULL, group_var, sum_var = NULL, sample_size = 100 )
data |
A data frame containing the population data to be processed. |
high_group_var |
Character vector, optional. The variables used to group individuals hierarchically. This should be a categorical variable. If provided, the function samples individuals within each group defined by these variables. |
group_var |
Quosure. The variable used to group individuals in the dataset. This should be a categorical variable. |
sum_var |
Quosure, optional. The variable to sum over within each group. If |
sample_size |
Integer. The total number of individuals to sample based on group proportions. Must be a positive integer. |
A tibble (data frame) with the following columns:
The sampled group type.
The group identifier.
The count of individuals in the group.
The proportion of the group relative to the total population.
A convenience function to set appropriate legend key sizes for icon-based legends. This is equivalent to using theme(legend.key.size = ...) but provides sensible defaults for population icon plots.
scale_legend_icon( size = 10, unit = "mm", spacing = 0.2, size_multiplier = 2, ... )scale_legend_icon( size = 10, unit = "mm", spacing = 0.2, size_multiplier = 2, ... )
size |
Numeric. Legend key size in specified units (default 10). |
unit |
Character. Unit for legend key sizing (default "mm"). |
spacing |
Numeric. Spacing between legend items as fraction of size (default 0.2). |
size_multiplier |
Numeric. Multiplier to apply to the size for spacing calculations (default 2). |
... |
Additional theme arguments. |
A ggplot2 theme object that can be added to a plot.
library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(df, aes(icon = icon, color = type)) + geom_pop() + scale_legend_icon(size = 20)library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(df, aes(icon = icon, color = type)) + geom_pop() + scale_legend_icon(size = 20)
A minimal theme optimized for icon-based population plots. Similar to
theme_void() but with automatic legend key sizing, appropriate margins,
and sensible defaults for population visualizations.
theme_pop( base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, legend_icon_size = NULL, legend_spacing = NULL, plot_margin = NULL, legend_position = "right" )theme_pop( base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, legend_icon_size = NULL, legend_spacing = NULL, plot_margin = NULL, legend_position = "right" )
base_size |
Base font size in points (default: 11). |
base_family |
Base font family (default: ""). |
base_line_size |
Base size for line elements (default: base_size/22). |
base_rect_size |
Base size for rect elements (default: base_size/22). |
legend_icon_size |
Size of legend icons in cm. If NULL (default), automatically calculated as base_size/20 for proportional sizing. |
legend_spacing |
Spacing between legend items in cm (default: 0.3 * legend_icon_size). |
plot_margin |
Plot margins. Default: margin(5.5, 5.5, 5.5, 5.5, "pt"). Can be a single numeric (applied to all sides) or margin() object. |
legend_position |
Position of legend: "none", "left", "right", "bottom", "top" (default: "right"). |
A ggplot2 theme object.
library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(data = df, aes(icon = icon, color = type)) + geom_pop(size = 1) + theme_pop()library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(data = df, aes(icon = icon, color = type)) + geom_pop(size = 1) + theme_pop()
A dark variant of theme_pop() with white text on black background. Perfect for presentations or dark-mode visualizations.
theme_pop_dark( base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, legend_icon_size = NULL, legend_spacing = NULL, plot_margin = NULL, legend_position = "right", bg_color = "black", text_color = "white" )theme_pop_dark( base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22, legend_icon_size = NULL, legend_spacing = NULL, plot_margin = NULL, legend_position = "right", bg_color = "black", text_color = "white" )
base_size |
Base font size in points (default: 11). |
base_family |
Base font family (default: ""). |
base_line_size |
Base size for line elements (default: base_size/22). |
base_rect_size |
Base size for rect elements (default: base_size/22). |
legend_icon_size |
Size of legend icons in cm. If NULL (default), automatically calculated as base_size/20 for proportional sizing. |
legend_spacing |
Spacing between legend items in cm (default: 0.3 * legend_icon_size). |
plot_margin |
Plot margins. Default: margin(5.5, 5.5, 5.5, 5.5, "pt"). Can be a single numeric (applied to all sides) or margin() object. |
legend_position |
Position of legend: "none", "left", "right", "bottom", "top" (default: "right"). |
bg_color |
Background color (default: "black"). |
text_color |
Text color (default: "white"). |
A ggplot2 theme object.
library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(data = df, aes(icon = icon, color = type)) + geom_pop(size = 1) + theme_pop_dark(base_size = 40)library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(data = df, aes(icon = icon, color = type)) + geom_pop(size = 1) + theme_pop_dark(base_size = 40)
An ultra-minimal variant with no margins or legend, perfect for icon arrays without annotations.
theme_pop_minimal(base_size = 11, base_family = "")theme_pop_minimal(base_size = 11, base_family = "")
base_size |
Base font size in points (default: 11). |
base_family |
Base font family (default: ""). |
A ggplot2 theme object.
library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(data = df, aes(icon = icon, color = type)) + geom_pop(size = 1) + theme_pop_minimal()library(ggplot2) df <- data.frame( type = rep(c("A", "B"), each = 10), icon = rep(c("circle", "square"), each = 10) ) ggplot(data = df, aes(icon = icon, color = type)) + geom_pop(size = 1) + theme_pop_minimal()