| Title: | Visualise Changes in Rankings with 'ggplot2' |
|---|---|
| Description: | Calculates, inspects, tabulates, and visualises changes in rankings across two to four ordered states. Creates 'ggplot2'-based rank-transition charts that retain categories entering or leaving a selected top-rank boundary. Supports ranks calculated from numeric values as well as authoritative ranks supplied without values. |
| Authors: | Rubeshkumar Polani [aut, cre] (ORCID: <https://orcid.org/0000-0002-0418-7592>), Think Denominator [cph] |
| Maintainer: | Rubeshkumar Polani <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-09-19 11:41:15 UTC |
| Source: | https://github.com/thinkdenominator/ggrank |
Creates a ggplot2 chart that combines ranked tables with connecting lines.
Categories entering or exiting top_n remain visible outside the boundary.
ggrank( data, category, period, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, top_n = 10, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), show_transitions = c("boundary", "top_only", "all"), colour_by = c("auto", "group", "movement", "none"), palette = NULL, legend_title = NULL, legend_labels = NULL, show_legend = TRUE, category_header = "Category", value_header = "Value", label_wrap = 28, category_width = 2.4, value_width = 1.55, state_gap = 1.15, base_size = 11, title = NULL, subtitle = NULL, check_rank = TRUE )ggrank( data, category, period, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, top_n = 10, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), show_transitions = c("boundary", "top_only", "all"), colour_by = c("auto", "group", "movement", "none"), palette = NULL, legend_title = NULL, legend_labels = NULL, show_legend = TRUE, category_header = "Category", value_header = "Value", label_wrap = 28, category_width = 2.4, value_width = 1.55, state_gap = 1.15, base_size = 11, title = NULL, subtitle = NULL, check_rank = TRUE )
data |
A data frame with one row per category and period. |
category, period
|
Unquoted columns identifying the category and ordered state. |
value |
Optional unquoted numeric ranking-value column. It may be
omitted when an authoritative |
rank |
Optional unquoted column containing precomputed ranks. |
label |
Optional unquoted display-label column. By default |
group |
Optional unquoted category-group column. |
periods |
Optional vector selecting and ordering two to four states. |
top_n |
Rank threshold to retain in each state. All categories tied at
the boundary are included, so the result may contain more than |
direction |
Whether large ( |
ties |
Ranking method: |
show_transitions |
|
colour_by |
Colour categories by |
palette |
Optional named colour vector. |
legend_title |
Optional legend title. |
legend_labels |
Optional named labels corresponding to every displayed group or movement value. |
show_legend |
Show the colour legend. |
category_header, value_header
|
Headers shown over the two box columns. |
label_wrap |
Approximate number of characters per category-label line. |
category_width, value_width
|
Relative widths of the category and value
boxes. Increase |
state_gap |
Horizontal space reserved for connectors between states. |
base_size |
Base text size passed to |
title, subtitle
|
Optional plot title and subtitle. |
check_rank |
When |
With colour_by = "auto", supplying group uses group colours; otherwise
movement colours are used. Movement classification prioritises entry to and
exit from the selected top-N boundary, followed by positive (riser),
negative (faller), or zero (stable) rank change. Palette and legend-label
names must match the displayed group or movement values exactly. For charts
with three or four periods, movement colour summarises the net change between
the first and last displayed period; use ggrank_change() to inspect each
adjacent transition.
A ggplot object.
ggrank(ggrank_causes, cause, year, rate, rank = rank, label = display_value, group = cause_group, periods = c(1990, 2021), top_n = 10 )ggrank(ggrank_causes, cause, year, rate, rank = rank, label = display_value, group = cause_group, periods = c(1990, 2021), top_n = 10 )
Opens a local Shiny application for creating a rank-transition chart, inspecting its comparison table, and visualising its largest rank changes. Users can start with the synthetic teaching data or upload a CSV file. The guided workflow supports either numeric values that need ranking or an existing rank column with no marks, rates, scores, or values.
ggrank_app(..., launch.browser = NULL)ggrank_app(..., launch.browser = NULL)
... |
Additional arguments passed to |
launch.browser |
Logical or a function passed to |
The app is a companion to the code-first workflow. It shows and downloads reusable R code for the selected analysis.
Invisibly returns the value from shiny::runApp().
if (interactive()) { ggrank_app() }if (interactive()) { ggrank_app() }
A synthetic teaching dataset with cause rankings and uncertainty intervals. Values do not represent published Global Burden of Disease estimates. This dataset was generated specifically for package examples and contains no downloaded IHME or Global Burden of Disease data. The package is independent and is not affiliated with or endorsed by IHME.
ggrank_causesggrank_causes
A data frame with 36 rows and 8 variables: year, cause,
cause_group, rank, rate, lower, upper, and display_value.
Synthetic data created for ggrank.
Creates a diverging bar chart answering "Who moved the most?" Positive
values rose towards rank one; negative values fell away from rank one. Raw
data are processed by ggrank_table(), preserving one ranking engine.
ggrank_change( data, category = NULL, period = NULL, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, top = 15, top_n = 10, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), check_rank = TRUE, comparison = c("latest", "all"), from = NULL, to = NULL, show_stable = FALSE, change_label = c("change", "ranks", "none"), label_wrap = 30, palette = c(riser = "#0072B2", faller = "#D55E00", stable = "#667085"), legend_title = NULL, legend_labels = NULL, show_legend = TRUE, base_size = 11, title = NULL, subtitle = NULL )ggrank_change( data, category = NULL, period = NULL, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, top = 15, top_n = 10, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), check_rank = TRUE, comparison = c("latest", "all"), from = NULL, to = NULL, show_stable = FALSE, change_label = c("change", "ranks", "none"), label_wrap = 30, palette = c(riser = "#0072B2", faller = "#D55E00", stable = "#667085"), legend_title = NULL, legend_labels = NULL, show_legend = TRUE, base_size = 11, title = NULL, subtitle = NULL )
data |
Raw data or a rank-change table returned by |
category, period
|
Unquoted columns used with raw |
value |
Optional numeric value column. It may be omitted when |
rank, label, group
|
Optional unquoted columns used with raw |
periods |
Optional vector selecting and ordering two to four periods. |
top |
Maximum categories displayed per comparison, selected by
|
top_n |
Top-rank boundary used to classify entrants and exits with raw
data. It does not control the number of bars; |
direction, ties, check_rank
|
Ranking options passed to |
comparison |
Display the latest consecutive comparison (default) or
all comparisons. Ignored when |
from, to
|
Optional explicit comparison. Both must be supplied and the pair must already exist in the rank-change table. |
show_stable |
Include unchanged categories. The default excludes them. |
change_label |
Show signed change ( |
label_wrap |
Approximate characters per category-label line. |
palette |
Named colours for |
legend_title |
Optional legend title. |
legend_labels |
Optional named labels for displayed movements. |
show_legend |
Show the movement legend. |
base_size |
Base text size. |
title, subtitle
|
Optional text overriding informative defaults. Use
|
A ggplot object whose data retain the underlying table status.
ggrank_change(ggrank_products, product, year, sales, top = 5) changes <- ggrank_table( ggrank_products, product, year, sales, top_n = 5, show_transitions = "all" ) ggrank_change(changes, top = 5, comparison = "all")ggrank_change(ggrank_products, product, year, sales, top = 5) changes <- ggrank_table( ggrank_products, product, year, sales, top_n = 5, show_transitions = "all" ) ggrank_change(changes, top = 5, comparison = "all")
Calculates ranks within each state before any top-N display filtering is
applied. This helper is optional: ggrank() performs the same ranking
automatically. Use it when you want to inspect, teach, export, or reuse the
calculated ranks.
ggrank_data( data, category, period, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), check_rank = TRUE )ggrank_data( data, category, period, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), check_rank = TRUE )
data |
A data frame with one row per category and period. |
category, period
|
Unquoted columns identifying the category and state. |
value |
Optional unquoted numeric ranking-value column. It may be
omitted when an authoritative |
rank |
Optional unquoted column containing authoritative precomputed ranks. When supplied, these ranks are preserved. |
label |
Optional unquoted display-label column. |
group |
Optional unquoted category-group column. |
periods |
Optional vector selecting and ordering states. Unlike
|
direction |
Whether large ( |
ties |
Ranking method: |
check_rank |
When |
Ranking uses the exact numeric value; formatting supplied through label
never changes the rank. By default, equal values receive the same competition
rank (1, 2, 3, 3, 5). Tied categories receive separate alphabetical display
positions so their plot boxes do not overlap.
A data frame containing category, period, value, rank,
display_position, label, and group.
ggrank_data(ggrank_products, product, year, sales) tied <- data.frame( year = rep(c(2020, 2025), each = 4), organism = rep(LETTERS[1:4], 2), rate = c(5, 4, 3, 3, 6, 4, 4, 2) ) ggrank_data(tied, organism, year, rate) ranks_only <- data.frame( year = rep(c(2024, 2025), each = 3), student = rep(c("A", "B", "C"), 2), rank = c(1, 2, 3, 2, 1, 3) ) ggrank_data(ranks_only, student, year, rank = rank)ggrank_data(ggrank_products, product, year, sales) tied <- data.frame( year = rep(c(2020, 2025), each = 4), organism = rep(LETTERS[1:4], 2), rate = c(5, 4, 3, 3, 6, 4, 4, 2) ) ggrank_data(tied, organism, year, rate) ranks_only <- data.frame( year = rep(c(2024, 2025), each = 3), student = rep(c("A", "B", "C"), 2), rank = c(1, 2, 3, 2, 1, 3) ) ggrank_data(ranks_only, student, year, rank = rank)
A general-purpose teaching dataset of product sales across three years. Every product name, ranking, and sales value is synthetic and was generated specifically for package teaching examples.
ggrank_productsggrank_products
A data frame with 24 rows and 4 variables: year, product,
category, and sales.
Synthetic data created for ggrank.
Produces the analytical companion to ggrank(). Each row compares a
category between two adjacent selected states. A two-state comparison has
one row per category; three or four states produce successive transition
rows such as 1990 to 2010 and 2010 to 2021.
ggrank_table( data, category, period, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, top_n = 10, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), show_transitions = c("boundary", "top_only", "all"), check_rank = TRUE )ggrank_table( data, category, period, value = NULL, rank = NULL, label = NULL, group = NULL, periods = NULL, top_n = 10, direction = c("descending", "ascending"), ties = c("min", "dense", "first"), show_transitions = c("boundary", "top_only", "all"), check_rank = TRUE )
data |
A data frame with one row per category and period. |
category, period
|
Unquoted columns identifying the category and ordered state. |
value |
Optional unquoted numeric ranking-value column. It may be
omitted when an authoritative |
rank |
Optional unquoted column containing precomputed ranks. |
label |
Optional unquoted display-label column. By default |
group |
Optional unquoted category-group column. |
periods |
Optional vector selecting and ordering two to four states. |
top_n |
Rank threshold to retain in each state. All categories tied at
the boundary are included, so the result may contain more than |
direction |
Whether large ( |
ties |
Ranking method: |
show_transitions |
|
check_rank |
When |
Rank change is calculated as rank_from - rank_to: positive values rose
towards rank one, negative values fell, and zero is stable. status is
"entrant" when a category crosses from outside to inside top_n, and
"exit" for the reverse. These boundary statuses take precedence over
"riser" and "faller". "new" and "absent" indicate that a category
exists on only one side; "missing" identifies a supplied non-finite value.
A data frame with category, transition states, ranks, rank change,
values, value change, labels, group, missing-value indicators, and movement
status. Positive
rank_change means that a category rose in the ranking.
ggrank_table( ggrank_products, product, year, sales, periods = c(2022, 2024), top_n = 5 )ggrank_table( ggrank_products, product, year, sales, periods = c(2022, 2024), top_n = 5 )
A minimal theme for rank-transition charts
theme_ggrank(base_size = 11, base_family = "")theme_ggrank(base_size = 11, base_family = "")
base_size |
Base font size. |
base_family |
Base font family. |
A complete ggplot2 theme.
theme_ggrank()theme_ggrank()