| Title: | Optimizing Alluvial Plots |
|---|---|
| Description: | Sort k-partite graphs with node order, layer order, and node grouping optimized with a heuristic to (nearly) minimize edge crossings, and render the result as an alluvial plot. Useful for improving visualizations with alluvial plots by "untangling" the graphs. |
| Authors: | Joseph Rich [aut, cre] (ORCID: <https://orcid.org/0000-0003-1400-8479>), Conrad Oakes [aut], Lior Pachter [aut] (ORCID: <https://orcid.org/0000-0002-9164-6231>) |
| Maintainer: | Joseph Rich <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-10-21 11:38:21 UTC |
| Source: | https://github.com/pachterlab/wompwomp |
Computes the WLOMP color-agreement objective
for a coloring produced by get_lode_clusters(): the total
weight of the observations whose blocks in two different layers were given
the same color.
compute_color_agreement( data, cols, mapping, wt = NULL, adjacent_only = FALSE, missing_value = "Missing" )compute_color_agreement( data, cols, mapping, wt = NULL, adjacent_only = FALSE, missing_value = "Missing" )
data |
A data frame or tibble. Must be in one of two formats:
(1) wt == NULL: each row represents an entity and each column a grouping; or
(2) wt != NULL: each row represents a combination of groupings and the column
|
cols |
Character vector. Column names from |
mapping |
List. Output of |
wt |
Optional character. Column name holding the weight of each row. |
adjacent_only |
Logical. If TRUE, sum only over adjacent layer pairs
|
missing_value |
Character. Name of the stratum of missing ( |
Writing for the co-occurrence weight of block
of layer and block of layer , and for
the color assigned to the blocks of layer ,
The sum runs over every unordered pair of layers, matching the graph that
get_lode_clusters() clusters (which also uses every pair, not only adjacent
ones). Set adjacent_only = TRUE to restrict it to the
adjacent pairs, i.e. to the layer pairs that are actually drawn side by side.
is a diagnostic, not something the coloring maximizes:
because colors may repeat within a layer, is maximized
trivially by painting every block the same color. It is reported so a
coloring can be compared against another coloring of the same data.
A list with:
color_agreementThe scalar .
per_pairA data frame with one row per layer pair, giving the
contribution of that pair and the total
co-occurrence weight available to it.
set.seed(429144) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) mapping <- get_lode_clusters(data, cols = c("method1", "method2")) compute_color_agreement(data, cols = c("method1", "method2"), mapping = mapping)set.seed(429144) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) mapping <- get_lode_clusters(data, cols = c("method1", "method2")) compute_color_agreement(data, cols = c("method1", "method2"), mapping = mapping)
Determine overlapping edges of k-partite graph.
compute_crossing_objective( data, cols = NULL, wt = "value", weighted_metric = TRUE, verbose = FALSE )compute_crossing_objective( data, cols = NULL, wt = "value", weighted_metric = TRUE, verbose = FALSE )
data |
A data frame, tibble, or CSV file path. Must be in one of two formats:
(1) wt == NULL: Each row represents an entity, each column represents a grouping, and each entry represents the membership of the entity in that row to the grouping in that column. Must contain at least two columns (two cols).
(2) wt != NULL: Each row represents a combination of groupings, each column from |
cols |
Optional character vector. Vector of column names from |
wt |
Optional character. Column name from |
weighted_metric |
Logical. Determines if the objective is total number of edge crossings (weighted_metric=FALSE) or sum of product of overlapping edge weights (weighted_metric=TRUE). |
verbose |
Logical. If TRUE, will display messages during the function. |
If return_weighted_layer_free_objective is FALSE (default): A list of values, as follows: 'lode_df': A data frame containing the following columns:
alluvium: A specific alluvium/edge.
count: The weight of the alluvium/edge.
x1, x2, ...: Each xi represents the x position of axis/layer i.
y1, y2, ...: Each yi represents the height of a lode in axis/layer i.
stratum1, stratum2, ...: Each stratumi represents the stratum through which the alluvial crosses in axis/layer i.
weight1: The weight of the first alluvium, corresponding to the 'count' column in lode_df.
weight2: The weight of the second alluvium, corresponding to the 'count' column in lode_df.
'output_objective': An integer representing the sum of products of overlapping edge weights.
data <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) data <- sort_to_uncross(data, cols = c("method1", "method2"), method = "tsp") result <- compute_crossing_objective(data, cols = c("method1", "method2"))data <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) data <- sort_to_uncross(data, cols = c("method1", "method2"), method = "tsp") result <- compute_crossing_objective(data, cols = c("method1", "method2"))
Colors a dataframe with the algorithm specified by method.
get_lode_clusters( data, cols, wt = NULL, method = "advanced", resolution = 1, verbose = FALSE, options = NULL )get_lode_clusters( data, cols, wt = NULL, method = "advanced", resolution = 1, verbose = FALSE, options = NULL )
data |
A data frame, tibble, or CSV file path. Must be in one of two formats:
(1) wt == NULL: Each row represents an entity, each column represents a grouping, and each entry represents the membership of the entity in that row to the grouping in that column. Must contain at least two columns (two cols).
(2) wt != NULL: Each row represents a combination of groupings, each column from |
cols |
Character vector. Vector of column names from |
wt |
Optional character. Column name from |
method |
Character. Matching colors methods. Choices are 'advanced' (default), 'none', 'left', 'right', or any value in |
resolution |
Numeric If |
verbose |
Logical. If TRUE, will display messages during the function. |
options |
Additional arguments. See get_lode_clusters_options |
A nested list mapping each stratum to a color. The outer list is indexed by axis names, and the inner lists are indexed by stratum names. Each color is represented by an integer that is mapped to a color by the mapping parameter in scale_fill_manual().
set.seed(429144) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) lapply(data, levels) cluster_mapping <- data |> get_lode_clusters(cols = c(method1, method2))set.seed(429144) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) lapply(data, levels) cluster_mapping <- data |> get_lode_clusters(cols = c(method1, method2))
get_lode_clusters()
Creates a list of control parameters that modify the behavior of
get_lode_clusters(). These options allow fine-grained control over the coloring
algorithm without cluttering the main function interface.
get_lode_clusters_options( method_advanced_option = c("leiden", "louvain"), cutoff = 0.5, preprocess_data = TRUE, missing_value = "Missing", print_params = FALSE )get_lode_clusters_options( method_advanced_option = c("leiden", "louvain"), cutoff = 0.5, preprocess_data = TRUE, missing_value = "Missing", print_params = FALSE )
method_advanced_option |
Character. When
|
cutoff |
Numeric. If using a non-advanced algorithm, similarity score threshold for deciding whether to reuse a color or assign a new one. |
preprocess_data |
Logical. If TRUE (default), preprocess data with
|
missing_value |
Character. Missing ( |
print_params |
Logical. If TRUE, prints parameters during execution. |
A named list of control parameters for use in get_lode_clusters().
data <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) opts <- get_lode_clusters_options(cutoff = 0.3, method_advanced_option = "louvain") mapping <- get_lode_clusters(data = data, cols = c('method1', 'method2'), options = opts)data <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) opts <- get_lode_clusters_options(cutoff = 0.3, method_advanced_option = "louvain") mapping <- get_lode_clusters(data = data, cols = c('method1', 'method2'), options = opts)
Convert color mapping made by get_lode_clusters into list for scale_fill_manual
lode_cluster_pal(data, cols, mapping, color_palette = NULL, per_axis = FALSE)lode_cluster_pal(data, cols, mapping, color_palette = NULL, per_axis = FALSE)
data |
A data frame with columns corresponding to axes, with factors indicating sorting. (eg run through sort_to_uncross) |
cols |
Character vector. Vector of column names from |
mapping |
List. Output from get_lode_clusters. |
color_palette |
Optional named list or vector mapping values in the graphing columns to colors. Overrides default palette. |
per_axis |
Logical. If FALSE (default), returns one color per distinct stratum value across all columns (a flat named vector). If TRUE, returns a named list with one entry per column, each a named vector of colors for that column's levels – so a value appearing in two columns can take a different color in each (as when a method over-splits a group). |
A named vector of colors, or (if per_axis) a list of them. An explicit NA factor level (as sort_to_uncross() returns with missing_value = NA) has no name and gets no entry; ggplot2 scales color it with their na.value.
# Example 1 data <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) cols = c("method1", "method2") clus_df_gather <- sort_to_uncross(data, cols = cols, method = "tsp") color_mapping <- get_lode_clusters(data = clus_df_gather, cols = cols) color_list <- lode_cluster_pal(data = clus_df_gather, cols = cols, mapping = color_mapping)# Example 1 data <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) cols = c("method1", "method2") clus_df_gather <- sort_to_uncross(data, cols = cols, method = "tsp") color_mapping <- get_lode_clusters(data = clus_df_gather, cols = cols) color_list <- lode_cluster_pal(data = clus_df_gather, cols = cols, mapping = color_mapping)
Creates a two-axis alluvial plot to visualize the relationship between two categorical groupings (e.g., cluster assignments from different methods), minimizing crossover and aligning clusters based on agreement.
plot_alluvial( df, graphing_columns = NULL, column1 = NULL, column2 = NULL, column_weights = NULL, sorting_algorithm = "neighbornet", optimize_column_order = TRUE, optimize_column_order_per_cycle = FALSE, alpha = 2, beta = alpha, weight_scalar = 5e+05, matrix_initialization_value = NULL, same_side_matrix_initialization_value = NULL, matrix_initialization_value_column_order = 1e+06, weight_scalar_column_order = 1, column_sorting_metric = "edge_crossing", column_sorting_algorithm = "neighbornet", weighted = TRUE, cycle_start_positions = NULL, fixed_column = NULL, random_initializations = 1, color_boxes = TRUE, color_bands = FALSE, color_list = NULL, color_band_list = NULL, color_band_column = NULL, color_val = NULL, color_band_boundary = FALSE, coloring_algorithm = "advanced", coloring_algorithm_advanced_option = "leiden", resolution = 1, cutoff = 0.5, alluvial_alpha = 0.5, include_labels_in_boxes = TRUE, include_axis_titles = TRUE, include_group_sizes = FALSE, output_plot_path = NULL, output_df_path = NULL, preprocess_data = TRUE, default_sorting = "alphabetical", missing_value = "Missing", box_width = 1/3, text_width = 1/4, min_text = 4, text_size = 14, auto_adjust_text = TRUE, axis_text_size = 2, axis_text_vjust = 0, save_height = 6, save_width = 6, dpi = 300, rasterise_alluvia = FALSE, keep_y_labels = FALSE, keep_x_labels = TRUE, box_line_width = 1, verbose = FALSE, print_params = FALSE, add_legend = FALSE, legend_loc = "right", flip_xy = FALSE )plot_alluvial( df, graphing_columns = NULL, column1 = NULL, column2 = NULL, column_weights = NULL, sorting_algorithm = "neighbornet", optimize_column_order = TRUE, optimize_column_order_per_cycle = FALSE, alpha = 2, beta = alpha, weight_scalar = 5e+05, matrix_initialization_value = NULL, same_side_matrix_initialization_value = NULL, matrix_initialization_value_column_order = 1e+06, weight_scalar_column_order = 1, column_sorting_metric = "edge_crossing", column_sorting_algorithm = "neighbornet", weighted = TRUE, cycle_start_positions = NULL, fixed_column = NULL, random_initializations = 1, color_boxes = TRUE, color_bands = FALSE, color_list = NULL, color_band_list = NULL, color_band_column = NULL, color_val = NULL, color_band_boundary = FALSE, coloring_algorithm = "advanced", coloring_algorithm_advanced_option = "leiden", resolution = 1, cutoff = 0.5, alluvial_alpha = 0.5, include_labels_in_boxes = TRUE, include_axis_titles = TRUE, include_group_sizes = FALSE, output_plot_path = NULL, output_df_path = NULL, preprocess_data = TRUE, default_sorting = "alphabetical", missing_value = "Missing", box_width = 1/3, text_width = 1/4, min_text = 4, text_size = 14, auto_adjust_text = TRUE, axis_text_size = 2, axis_text_vjust = 0, save_height = 6, save_width = 6, dpi = 300, rasterise_alluvia = FALSE, keep_y_labels = FALSE, keep_x_labels = TRUE, box_line_width = 1, verbose = FALSE, print_params = FALSE, add_legend = FALSE, legend_loc = "right", flip_xy = FALSE )
df |
A data frame, tibble, or CSV file path. Must be in one of two formats:
(1) column_weights == NULL: Each row represents an entity, each column represents a grouping, and each entry represents the membership of the entity in that row to the grouping in that column. Must contain at least two columns (two graphing_columns).
(2) column_weights != NULL: Each row represents a combination of groupings, each column from |
graphing_columns |
Optional character vector. Vector of column names from |
column1 |
Optional character. Can be used along with |
column2 |
Optional character. Can be used along with |
column_weights |
Optional character. Column name from |
sorting_algorithm |
Character. Algorithm with which to sort the values in the dataframe (default 'neighbornet'). Can choose from: 'neighbornet', 'tsp', 'greedy', 'barycenter', 'median', 'random', 'none'. 'neighbornet' performs sorting with NeighborNet (Bryant and Moulton, 2004). 'tsp' performs Traveling Salesman Problem solver from the TSP package. 'greedy', 'barycenter' and 'median' sweep across the axes, reordering each against its neighbor: 'greedy' places each block as close as possible to its heaviest neighbor, 'barycenter'/'median' at the weighted mean/median position of its neighbors. All methods accept any number of |
optimize_column_order |
Logical. If TRUE, will optimize the order of |
optimize_column_order_per_cycle |
Logical. If TRUE, will optimize the order of |
alpha |
Positive number (default 2). Ratio between the distance assigned to two blocks in different axes that share no observations and the scale of the |
beta |
Positive number (default |
weight_scalar |
Advanced. Positive number setting the absolute scale of the block distance matrix (the constant |
matrix_initialization_value |
Advanced. Distance between blocks in different axes that share no observations. |
same_side_matrix_initialization_value |
Advanced. Distance between distinct blocks of the same axis. |
matrix_initialization_value_column_order |
Positive integer. Initialized value in distance matrix for optimizing column order. Only applies when |
weight_scalar_column_order |
Positive integer. Scalar with which to loss function after taking their log1p in the distance matrix for optimizing column order. Only applies when |
column_sorting_metric |
Character. Metric to use for determining column order. Options are "edge_crossing" (default) or "ARI". Only applies when |
column_sorting_algorithm |
Character. Algorithm to use for determining column order. Options are "neighbornet" (default) or "tsp". Only applies when |
weighted |
Logical. Weighted objective |
cycle_start_positions |
Set. Cycle start positions to consider. Anything outside this set will be skipped. Only applies when |
fixed_column |
Optional character or integer vector. Names, or positions in |
random_initializations |
Integer. Number of initializations of the stratum positions of the non-fixed axes (the first from the incoming order, the rest random), keeping the one with the fewest crossings. Only applies when |
color_boxes |
Logical. Whether to color the strata/boxes (representing groups). |
color_bands |
Logical. Whether to color the alluvia/edges (connecting the strata). |
color_list |
Optional named list or vector of colors to override default group colors. |
color_band_list |
Optional named list or vector of colors to override default band colors. |
color_band_column |
Optional Character. Which column to use for coloring bands. |
color_val |
Optional named list where the entries are colors and the names correspond to values of the dataframe that should use those colors |
color_band_boundary |
Logical. Whether or not to color boundaries between bands |
coloring_algorithm |
Character. Matching colors methods. Choices are 'advanced' (default), 'none', 'left', 'right', or any value in |
coloring_algorithm_advanced_option |
Character. If |
resolution |
Numeric If |
cutoff |
Numeric If |
alluvial_alpha |
Numeric between 0 and 1. Transparency level for the alluvial bands. |
include_labels_in_boxes |
Logical. Whether to include text labels inside the rectangular group boxes. |
include_axis_titles |
Logical. Whether to display axis titles for column1 and column2. |
include_group_sizes |
Logical. If |
output_plot_path |
Character. File path to save the plot (e.g., "plot.png"). If |
output_df_path |
Optional character. Output path for the output data frame, in CSV format. If |
preprocess_data |
Logical. If TRUE, will preprocess the data with the |
default_sorting |
Character. Default column sorting in prep_for_lodes if integer columns do not exist. Options are 'alphabetical' (default), 'reverse_alphabetical', 'increasing', 'decreasing', 'random'. |
missing_value |
Character. Missing ( |
box_width |
Numeric between 0 and 1. Box width |
text_width |
Numeric between 0 and 1. Text width |
min_text |
Integer greater than 0. Min text |
text_size |
Integer greater than 0. Text size (works whether auto_adjust_text is TRUE or FALSE). |
auto_adjust_text |
Logical. Whether to automatically adjust text size to fit in box. |
axis_text_size |
Integer greater than 0. Axis text size |
axis_text_vjust |
Integer. Axis text vjust |
save_height |
Integer greater than 0. Save height, in inches |
save_width |
Integer greater than 0. Save width, in inches |
dpi |
Integer greater than 0. DPI for |
rasterise_alluvia |
Logical. Whether to rasterize the alluvia if |
keep_y_labels |
Keep y labels |
keep_x_labels |
Keep x labels |
box_line_width |
Box line width |
verbose |
Logical. If TRUE, will display messages during the function. |
print_params |
Logical. If TRUE, will print function params. |
add_legend |
Logical. If TRUE, will generate a legend of the colors of boxes and alluvial |
legend_loc |
Character. Location of legend. Only applies if |
flip_xy |
Logical. Flip x and y (rotate plot 90 degrees). |
A ggplot2 object representing the alluvial plot.
# Example 1: df format 1 df <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) p <- plot_alluvial(df, graphing_columns = c("method1", "method2"), sorting_algorithm = "tsp", column_sorting_algorithm = "tsp", coloring_algorithm = "right" ) # Example 2: df format 2 df <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) clus_df_gather <- df |> dplyr::mutate_if(is.numeric, function(x) factor(x, levels = as.character(sort(unique(x))))) |> dplyr::group_by_all() |> dplyr::count(name = "value") p <- plot_alluvial( clus_df_gather, graphing_columns = c("method1", "method2"), column_weights = "value", sorting_algorithm = "tsp", column_sorting_algorithm = "tsp", coloring_algorithm = "right" )# Example 1: df format 1 df <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) p <- plot_alluvial(df, graphing_columns = c("method1", "method2"), sorting_algorithm = "tsp", column_sorting_algorithm = "tsp", coloring_algorithm = "right" ) # Example 2: df format 2 df <- data.frame(method1 = sample(1:3, 100, TRUE), method2 = sample(1:3, 100, TRUE)) clus_df_gather <- df |> dplyr::mutate_if(is.numeric, function(x) factor(x, levels = as.character(sort(unique(x))))) |> dplyr::group_by_all() |> dplyr::count(name = "value") p <- plot_alluvial( clus_df_gather, graphing_columns = c("method1", "method2"), column_weights = "value", sorting_algorithm = "tsp", column_sorting_algorithm = "tsp", coloring_algorithm = "right" )
plot_alluvial() and its helpers: build an alluvial diagram from a data frame with prep_for_lodes() + sort_to_uncross(), colour it, and render with ggalluvial. (Merged from the former biowomp package.)
Preprocess data (load in, add integer columns, reorder columns to match cols, and group as needed)
prep_for_lodes( data, cols, wt = NULL, default_sorting = "alphabetical", missing_value = "Missing", verbose = FALSE, print_params = FALSE, do_gather_set_data = FALSE, color_band_column = NULL, do_add_int_columns = FALSE )prep_for_lodes( data, cols, wt = NULL, default_sorting = "alphabetical", missing_value = "Missing", verbose = FALSE, print_params = FALSE, do_gather_set_data = FALSE, color_band_column = NULL, do_add_int_columns = FALSE )
data |
A data frame, tibble, or CSV file path. Must be in one of two formats:
(1) wt == NULL: Each row represents an entity, each column represents a grouping, and each entry represents the membership of the entity in that row to the grouping in that column. Must contain at least two columns (two cols).
(2) wt != NULL: Each row represents a combination of groupings, each column from |
cols |
Character vector. Vector of column names from |
wt |
Optional character. Column name from |
default_sorting |
Character. Default column sorting in |
missing_value |
Character or |
verbose |
Logical. If TRUE, will display messages during the function. |
print_params |
Logical. If TRUE, will print function params. |
do_gather_set_data |
Internal flag; not recommended to modify. |
color_band_column |
Internal flag; not recommended to modify. |
do_add_int_columns |
Internal flag; not recommended to modify. |
A data frame where each row represents a combination of groupings, each column from cols represents a grouping, and the column wt ('value' if wt == NULL) represents the number of entities in that combination of groupings. For each column in cols, there will be an additional column col1_int, col2_int, etc. where each column corresponds to a position mapping of groupings in the respective entry of cols - for example, col1_int corresponds to cols[1], col2_int corresponds to cols[2], etc.
set.seed(235488) data <- data.frame( method1 = sample(1:3, 100, TRUE), method2 = sample(4:6, 100, TRUE) ) head(data) lapply(data, unique) # Example 1: data format 1 clus_df_gather <- prep_for_lodes( data, cols = c("method1", "method2") ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels) # Example 2: data format 2 clus_df_gather <- data |> dplyr::mutate_if( is.numeric, function(x) factor(x, levels = as.character(sort(unique(x)))) ) |> dplyr::group_by_all() |> dplyr::count(name = "value") print(clus_df_gather) lapply(clus_df_gather[, 1:2], unique) clus_df_gather <- prep_for_lodes( clus_df_gather, cols = c("method1", "method2"), wt = "value" ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], unique)set.seed(235488) data <- data.frame( method1 = sample(1:3, 100, TRUE), method2 = sample(4:6, 100, TRUE) ) head(data) lapply(data, unique) # Example 1: data format 1 clus_df_gather <- prep_for_lodes( data, cols = c("method1", "method2") ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels) # Example 2: data format 2 clus_df_gather <- data |> dplyr::mutate_if( is.numeric, function(x) factor(x, levels = as.character(sort(unique(x)))) ) |> dplyr::group_by_all() |> dplyr::count(name = "value") print(clus_df_gather) lapply(clus_df_gather[, 1:2], unique) clus_df_gather <- prep_for_lodes( clus_df_gather, cols = c("method1", "method2"), wt = "value" ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], unique)
Sorts a dataframe with the algorithm specified by method.
sort_to_uncross( data, cols, wt = NULL, method = c("neighbornet", "tsp", "greedy", "barycenter", "median", "none", "random"), column_method = c("neighbornet", "tsp", "none", "random"), alpha = 2, beta = alpha, fixed_column = NULL, verbose = FALSE, options = NULL )sort_to_uncross( data, cols, wt = NULL, method = c("neighbornet", "tsp", "greedy", "barycenter", "median", "none", "random"), column_method = c("neighbornet", "tsp", "none", "random"), alpha = 2, beta = alpha, fixed_column = NULL, verbose = FALSE, options = NULL )
data |
A data frame/tibble. Must be in one of two formats:
(1) wt == NULL: Each row represents an entity, each column represents a grouping, and each entry represents the membership of the entity in that row to the grouping in that column. Must contain at least two columns (two cols).
(2) wt != NULL: Each row represents a combination of groupings, each column from |
cols |
Character vector. Vector of column names from |
wt |
Optional character. Column name from |
method |
Character. Algorithm with which to sort the values in the dataframe. Can choose from: 'neighbornet' (default), 'tsp', 'greedy', 'barycenter', 'median', 'random', 'none'. All of them accept any number of |
column_method |
Character. Algorithm to use for determining column order. Options are 'neighbornet' (default), 'tsp', 'random', and 'none'. Only applies when |
alpha |
Positive number (default 2). Ratio |
beta |
Positive number (default |
fixed_column |
Optional character or integer vector. Names, or positions in |
verbose |
Logical. If TRUE, will display messages during the function. |
options |
Additional arguments. See |
A data frame where each row represents an alluvium and each column represents an axis. Each column of cols represents an axis, stored as a factor ordered in ascending order of strata. There is an additional column wt ('value' if NULL) that represents the size of the alluvium in that row. The order of columns represents the recommended order of axes.
# Example 1: data format 1 (uncounted) set.seed(429144) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) head(data) lapply(data, levels) clus_df_gather <- sort_to_uncross( data, cols = c("method1", "method2"), method = "tsp", column_method = "tsp" ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels) # Example 2: data format 2 (counted) set.seed(806949) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) clus_df_gather <- data |> dplyr::mutate_if( is.numeric, function(x) factor(x, levels = as.character(sort(unique(x)))) ) |> dplyr::group_by_all() |> dplyr::count(name = "value") print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels) clus_df_gather <- sort_to_uncross( clus_df_gather, cols = c("method1", "method2"), wt = "value", method = "tsp", column_method = "tsp" ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels)# Example 1: data format 1 (uncounted) set.seed(429144) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) head(data) lapply(data, levels) clus_df_gather <- sort_to_uncross( data, cols = c("method1", "method2"), method = "tsp", column_method = "tsp" ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels) # Example 2: data format 2 (counted) set.seed(806949) data <- data.frame( method1 = factor(LETTERS[sample(1:3, 100, TRUE)]), method2 = factor(LETTERS[27 - sample(1:3, 100, TRUE)]) ) clus_df_gather <- data |> dplyr::mutate_if( is.numeric, function(x) factor(x, levels = as.character(sort(unique(x)))) ) |> dplyr::group_by_all() |> dplyr::count(name = "value") print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels) clus_df_gather <- sort_to_uncross( clus_df_gather, cols = c("method1", "method2"), wt = "value", method = "tsp", column_method = "tsp" ) print(clus_df_gather) lapply(clus_df_gather[, 1:2], levels)
sort_to_uncross()
Creates a list of control parameters that modify the behavior of
sort_to_uncross(). These options allow tuning algorithmic behavior without
cluttering the main function arguments.
sort_to_uncross_options( optimize_column_order_per_cycle = FALSE, weight_scalar = 5e+05, matrix_initialization_value = NULL, same_side_matrix_initialization_value = NULL, matrix_initialization_value_column_order = 1e+06, weight_scalar_column_order = 1, column_metric = c("edge_crossing", "ari"), weighted_metric = TRUE, cycle_start_positions = NULL, random_initializations = 1, preprocess_data = TRUE, default_sorting = c("alphabetical", "reverse_alphabetical", "increasing", "decreasing", "random", "fixed"), missing_value = "Missing", print_params = FALSE, do_compute_alluvial_statistics = FALSE )sort_to_uncross_options( optimize_column_order_per_cycle = FALSE, weight_scalar = 5e+05, matrix_initialization_value = NULL, same_side_matrix_initialization_value = NULL, matrix_initialization_value_column_order = 1e+06, weight_scalar_column_order = 1, column_metric = c("edge_crossing", "ari"), weighted_metric = TRUE, cycle_start_positions = NULL, random_initializations = 1, preprocess_data = TRUE, default_sorting = c("alphabetical", "reverse_alphabetical", "increasing", "decreasing", "random", "fixed"), missing_value = "Missing", print_params = FALSE, do_compute_alluvial_statistics = FALSE )
optimize_column_order_per_cycle |
Logical. If TRUE, will optimize the order of |
weight_scalar |
Advanced. Positive number |
matrix_initialization_value |
Advanced. Positive number |
same_side_matrix_initialization_value |
Advanced. Positive number |
matrix_initialization_value_column_order |
Positive integer. Initialized value in distance matrix for optimizing column order. Only applies when |
weight_scalar_column_order |
Positive integer. Scalar with which to loss function after taking their log1p in the distance matrix for optimizing column order. Only applies when |
column_metric |
Character. Metric to use for determining column order. Options are "edge_crossing" (default) or "ARI". Only applies when |
weighted_metric |
Logical. Determines if the objective is total number of edge crossings (weighted_metric=FALSE) or sum of product of overlapping edge weights (weighted_metric=TRUE). |
cycle_start_positions |
Set. Cycle start positions to consider. Anything outside this set will be skipped. Only applies when |
random_initializations |
Integer. Number of initializations of the stratum positions of the non-fixed axes: the first starts from the incoming order and each further one from a random order, and the one with the lowest crossing objective is kept. Only applies when |
preprocess_data |
Logical. If TRUE, will preprocess the data with the |
default_sorting |
Character. Default column sorting in |
missing_value |
Character or |
print_params |
Logical. If TRUE, will print function params. |
do_compute_alluvial_statistics |
Internal flag; not recommended to modify. |
A named list of control parameters, to be passed into sort_to_uncross()
via the options argument.
data <- data.frame( method1 = LETTERS[sample(1:3, 100, TRUE)], method2 = LETTERS[27 - sample(1:3, 100, TRUE)] ) opts <- sort_to_uncross_options( default_sorting = "reverse_alphabetical", weighted_metric = FALSE ) sort_to_uncross(data = data, cols = c('method1', 'method2'), options = opts)data <- data.frame( method1 = LETTERS[sample(1:3, 100, TRUE)], method2 = LETTERS[27 - sample(1:3, 100, TRUE)] ) opts <- sort_to_uncross_options( default_sorting = "reverse_alphabetical", weighted_metric = FALSE ) sort_to_uncross(data = data, cols = c('method1', 'method2'), options = opts)