Creates bar plots for comparing values across countries, with support for mean or latest values, statistical indicators (range, median), and various visual customizations including color grouping and value labels. The function automatically determines the most appropriate plot type based on the number of indicators and specified parameters.
Usage
wp_plot_bar(
data,
y_axis = NULL,
type = "Mean",
order = "Variable",
show_values = "Bottom",
show_year = 1,
show_range = TRUE,
show_median = TRUE,
color = "Subregion",
filename = NULL,
print = TRUE,
legend = TRUE,
reference = TRUE,
title = NULL,
subtitle = NULL,
subfig_title = TRUE,
verbose = TRUE,
debug = FALSE,
size = NULL,
base_size = 16,
bg = "transparent"
)
Arguments
- data
data.frame that must contain the following columns: - ISO: ISO 3-letter country codes (required) - Date: dates in Date format (required) - Variable: indicator names (required) - Value: numeric values (required) - Country: country names (optional, auto-generated from ISO if missing) - Reference: source citations (required if reference=TRUE)
- y_axis
Single string, TRUE, FALSE, or NULL: - String: y-axis label - TRUE: use Variable name as label - FALSE/NULL: no axis label
- type
Controls which value to display: - "Mean": average over time period (default) - "Last": most recent value Statistical indicators (range, median) adjust accordingly.
- order
Controls bar ordering: - "Variable": order by value (default) - "Alphabetical": order by country name - Numeric: order by nth variable (for multi-indicator plots) - TRUE: same as "Variable" - FALSE: same as "Alphabetical"
- show_values
Controls value label position: - "Bottom": labels below bars - "Top": labels above bars - FALSE: no labels Default is "Bottom".
- show_year
Controls year display in labels: - 0: no year - 1: short year format (YY) - 2: full year format (YYYY) Only applies when type="Last". Default is 1.
- show_range
Logical; whether to show min-max range: - TRUE: adds range indicators - FALSE: no range indicators Default is TRUE.
- show_median
Logical; whether to show median value: - TRUE: adds median markers when type="Mean" - FALSE: no median markers - Ignored when type="Last" Default is TRUE.
- color
Controls bar coloring scheme: For single indicator: - "Subregion": colors by geographical subregion (default) - "Region": colors by continent/major region - "Center-Periphery": colors by economic classification - "Hydrocarbon": colors by hydrocarbon exporter/importer status For multiple indicators: - "Variable": different color per indicator - FALSE/NULL: gray bars
- filename
Character string for saving plots: - Specify name without extension - Plots saved as both PNG and PDF in 'img/' directory - NULL/FALSE for no file output
Logical; controls plot display: - TRUE: displays the plot - FALSE: creates but doesn't display the plot Default is TRUE.
- legend
Logical; controls legend display: - TRUE: includes a legend - FALSE: omits the legend Default is TRUE.
- reference
Logical; controls reference panel: - TRUE: includes reference citations panel (requires Reference column) - FALSE: omits reference panel Default is TRUE.
- title
Character string for main plot title: - NULL/FALSE for no title
- subtitle
Character string for plot subtitle: - NULL/FALSE for no subtitle
- subfig_title
For future use; currently ignored.
- verbose
Logical; controls information messages: - TRUE: prints processing information - FALSE: suppresses information messages Default is TRUE.
- debug
Logical; controls debugging output: - TRUE: prints detailed debugging information - FALSE: suppresses debugging information Default is FALSE.
- size
Numeric or NULL; controls plot dimensions: - 1: small (10x7 inches) - 2: medium (15x7 inches) - 3: large (15x10 inches) - 4: extra large (20x10 inches) - NULL: auto-sizes based on data
- base_size
Numeric; base font size in points: - Controls text size throughout the plot - Must be positive number Default is 16.
- bg
character; controls plot background color: - "transparent": transparent background (default) - Any valid color string: sets background to that color
Details
The function automatically selects the appropriate plotting implementation based on the number of indicators:
Single indicator:
Simple bar plot with optional color grouping
Statistical indicators (range, median)
Flexible value and year labeling
Multiple indicators:
Grouped bar plot
Color-coded by indicator
Consistent statistical indicators across groups
The function includes multiple features for customization:
Automatic axis scaling and formatting
Flexible ordering options
Statistical annotations
Reference panel for data sources
Consistent theme across all variations
See also
wp_plot_series
for time series plots
wp_plot_scatter
for scatter plots
wp_from_iso
for ISO code to country name conversion
Other plotting functions:
wp_plot_scatter()
,
wp_plot_series()
Examples
# Basic bar plot with single indicator
data <- data.frame(
Date = rep(seq(as.Date("2000-01-01"), as.Date("2020-01-01"), by = "year"), 3),
ISO = rep(c("FRA", "DEU", "ITA"), each = 21),
Variable = "GDP Growth",
Value = rnorm(63, 2, 0.5),
Reference = "World Bank"
)
wp_plot_bar(data, y_axis = "Percent")
#> [COUNTRY] Column Country added to data
#> [CONFIG] n_countries = 3 | n_indicators = 1 | plot_type = bar
#> /!\ The following parameters are not used in bar plots and will be ignored: subfig_title, subfig_title
#> [SUBPLOT] call 'in_plot_one_indic_bar'
#> [ARGS] Unused arguments for function 'plot_func': subfig_title, bg
#> [LEGEND] Legend items (display): GDP Growth
#> [LEGEND] plot_type = 'bar' | legend_cols = 1' | legend_height = 1.33333333333333' | n_items = 1
#> [ADD_REF] References: World Bank
# Bar plot with mean values and all statistical indicators
wp_plot_bar(data,
type = "Mean",
show_range = TRUE,
show_median = TRUE,
color = "Region")
#> [COUNTRY] Column Country added to data
#> [CONFIG] n_countries = 3 | n_indicators = 1 | plot_type = bar
#> /!\ The following parameters are not used in bar plots and will be ignored: subfig_title, subfig_title
#> [SUBPLOT] call 'in_plot_one_indic_bar'
#> [ARGS] Unused arguments for function 'plot_func': subfig_title, bg
#> [LEGEND] Legend items (display): GDP Growth
#> [LEGEND] plot_type = 'bar' | legend_cols = 1' | legend_height = 1.33333333333333' | n_items = 1
#> [ADD_REF] References: World Bank
# Latest values with year labels
wp_plot_bar(data,
type = "Last",
show_year = 2,
order = "Alphabetical")
#> [COUNTRY] Column Country added to data
#> [CONFIG] n_countries = 3 | n_indicators = 1 | plot_type = bar
#> /!\ The following parameters are not used in bar plots and will be ignored: subfig_title, subfig_title
#> [SUBPLOT] call 'in_plot_one_indic_bar'
#> [ARGS] Unused arguments for function 'plot_func': subfig_title, bg
#> [LEGEND] Legend items (display): GDP Growth
#> [LEGEND] plot_type = 'bar' | legend_cols = 1' | legend_height = 1.33333333333333' | n_items = 1
#> [ADD_REF] References: World Bank
# Multiple indicators
data_multi <- data.frame(
Date = rep(seq(as.Date("2000-01-01"), as.Date("2020-01-01"), by = "year"), 6),
ISO = rep(rep(c("FRA", "DEU", "ITA"), each = 21), 2),
Variable = rep(c("GDP Growth", "Inflation"), each = 63),
Value = rnorm(126, 2, 0.5),
Reference = "World Bank"
)
wp_plot_bar(data_multi,
order = 1, # Order by first indicator
show_values = "Top")
#> [COUNTRY] Column Country added to data
#> [CONFIG] n_countries = 3 | n_indicators = 2 | plot_type = bar
#> /!\ Setting color to 'Variable' for multi-indicator bar plot
#> /!\ The following parameters are not used in bar plots and will be ignored: subfig_title, subfig_title
#> [SUBPLOT] call 'in_plot_multi_indic_bar'
#> [ARGS] Unused arguments for function 'plot_func': subfig_title, bg, show_year
#> [MAIN] Starting multi-indicator bar plot
#> [DATA] Starting data processing
#> [DATA] Variables found: GDP Growth, Inflation
#> [ORDER] Ordering by variable 1 : GDP Growth
#> [ORDER] Country order: Germany, Italy, France
#> [DATA] Final data rows: 6
#> [ORDER] Final country order: Germany, Italy, France
#> [POSITION] Bar positioning: width = 0.5 space = 0.2 dodge = 0.75
#> [COLORS] Color mapping created
#> [PLOT] Base plot created
#> [PLOT] Plot formatting completed
#> [LEGEND] Legend items (display): GDP Growth, Inflation
#> [LEGEND] plot_type = 'bar' | legend_cols = 2' | legend_height = 1.33333333333333' | n_items = 2
#> [ADD_REF] References: World Bank