Skip to contents

Generates stacked bar charts to visualize value compositions across multiple dimensions. The function supports stacked and unstacked presentations for decomposition analysis.

Usage

stack_plot(
  data,
  filter_var = NULL,
  x_axis_from,
  stack_value_from,
  split_by = NULL,
  panel_var = "Experiment",
  variable_col = "Variable",
  unit_col = "Unit",
  desc_col = "Description",
  invert_pane = FALSE,
  separate_figure = FALSE,
  unstack_plot = FALSE,
  output_path = NULL,
  export_picture = TRUE,
  export_as_pdf = FALSE,
  export_config = NULL,
  var_name_by_description = FALSE,
  add_var_info = FALSE,
  show_total = TRUE,
  top_impact = NULL,
  plot_style_config = NULL
)

Arguments

data

A data frame or a list of data frames containing GTAP results.

filter_var

Vector or data frame. If a vector, filters the values in `x_axis_from`. If a data frame, filters `value_col` based on matching `variable_col` values.

x_axis_from

Character. Column name for x-axis categories (e.g., "REG", "Sector").

stack_value_from

Character. Column containing stack component categories (e.g., "COMM" for commodities).

split_by

Character or vector. Column name(s) for data splitting (e.g., "COMM", "REG", "ACTS"). Set to NULL for no splitting, suitable for macro-level analysis.

panel_var

Character. Column for panel facets (default: "Experiment").

variable_col

Character. Column containing variable identifiers (default: "Variable").

unit_col

Character. Column containing unit information (default: "Unit").

desc_col

Character. Column containing variable descriptions (default: "Description").

invert_pane

Logical. If TRUE, creates horizontal bars instead of vertical ones (default: FALSE).

separate_figure

Logical. If TRUE, generates separate figures per panel value (default: FALSE).

unstack_plot

Logical. If TRUE, creates separate bar plots for each `x_axis_from` value instead of stacked bars (default: FALSE).

output_path

Character. Directory path for saving output files. If NULL, plots are only returned in R.

export_picture

Logical. If TRUE, exports plots as image files (default: TRUE).

export_as_pdf

Logical or "merged". If TRUE, exports separate PDFs; if "merged", creates a multi-page PDF; if FALSE, skips PDF (default: FALSE).

export_config

List. Export settings, including:

  • `width`: Output file width (in inches).

  • `height`: Output file height (in inches).

  • Additional settings—see `?get_export_config`.

var_name_by_description

Logical. If TRUE, uses descriptions instead of variable codes in titles (default: FALSE).

add_var_info

Logical. If TRUE, adds the variable code in parentheses after the description (default: FALSE).

show_total

Logical. If TRUE, displays total values above stacked bars (default: TRUE).

top_impact

Numeric or NULL. If specified, shows only the top N impactful values; NULL shows all values.

plot_style_config

List. Custom plot styles—see `?get_plot_style_config`.

Value

A ggplot2 object for a single plot or a list of ggplot2 objects for multiple plots.

Author

Pattawee Puangchit

Examples

# \donttest{
# Input Path:
input_path <- system.file("extdata/in", package = "GTAPViz")
                                
# GTAP Macro Variables from 2 .sl4 Files named (EXP1, EXP2)
# Note: No need to add .sl4 to the experiment name 
gtap_data <- auto_gtap_data(experiment = c("EXP1", "EXP2"),
                            input_path = input_path, subtotal_level = FALSE,
                            process_sl4_vars = NULL, process_har_vars = NULL,
                            mapping_info = "GTAPv7", plot_data = TRUE)
#> All 2 requested experiment files are found with both SL4 and HAR data.
#> Mapping method used: GTAPv7 
#> Processing GTAP Macro Data
#> Processing SL4 Data
#> Processing QXS Bilateral Trade Data
#> Processing HAR Data
#> 
#> Summary of Processing:
#> GTAP Macro Data processed successfully
#> SL4 Data processed successfully
#> HAR Data processed successfully
#> 
#> GTAP data processing completed successfully!
                            
stack_plot(data = har.plot.data[["A"]],
           x_axis_from = "REG",
           stack_value_from = "COLUMN",
           split_by = FALSE,
           
           show_total = TRUE,
           unstack_plot = FALSE,
           
           var_name_by_description = TRUE,       
           
           invert_pane = FALSE,           
           separate_figure = FALSE,

           export_picture = TRUE,
           export_as_pdf = "merged",
           export_config = list(
             width = 28,
             height = 15
           ),
           output_path = "/your/folder/path",
           
           plot_style_config = list(
             color_tone = "gtap",
             panel_rows = 2,
             panel_cols = NULL,
             show_legend = TRUE,
             show_axis_titles_on_all_facets = FALSE
           ))
#> >>> Starting plot export process: 1 plot(s) with dimensions (widthxheight): 28.0 x 15.0 inches
#> >>> DPI: 300
#> Warning: cannot create dir '/your', reason 'Permission denied'
#> Error in grDevices::pdf(file = pdf_path, width = export_config$width,     height = export_config$height, useDingbats = FALSE, title = pdf_file_name): cannot open file '/your/folder/path/Stack_plot_1.pdf'
# }