Skip to contents

Sorts data for plotting using flexible options for column ordering and value-based sorting. Works with data frames, lists of data frames, or nested data structures.

Usage

sort_plot_data(
  data,
  sort_columns = NULL,
  sort_by_value_desc = NULL,
  convert_to_factor = TRUE
)

Arguments

data

A data frame or list structure containing data to be sorted.

sort_columns

Named list. Specifies columns to sort by and their ordering. Each element should be a character vector of values in desired order. For example, `list(Region = c("USA", "EU", "CHN")`, `Experiment = c("Base", "Shock1", "Shock2"))`.

sort_by_value_desc

Logical or NULL. Controls sorting by the "Value" column: - NULL (default): Don't sort by value, only use column-based sorting. - TRUE: After column-based sorting, sort by value in descending order. - FALSE: After column-based sorting, sort by value in ascending order.

convert_to_factor

Logical. Whether to convert sorted columns to factors with custom ordering. Default is TRUE, which preserves ordering in GTAP plotting functions.

Value

A data structure with the same form as the input, with all contained data frames sorted.

Author

Pattawee Puangchit

Examples


# Load Sample Data:
sl4_data1 <- HARplus::load_sl4x(system.file("extdata/in", "EXP1.sl4", 
                                package = "GTAPViz"))
# Get Data by Variable Name
sl4_data1 <- HARplus::get_data_by_var(c("qgdp","EV"),sl4_data1)

# Creating Sorting Rule
sorting_specs <- data.frame(REG = c("EastAsia", "SEAsia", "Oceania"))

# Sorting
sort_data <- sort_plot_data(sl4_data1, sort_columns = sorting_specs, 
                            sort_by_value_desc = FALSE)