Skip to content

Commit

Permalink
bug fix: coloring for numeric columns
Browse files Browse the repository at this point in the history
 - While updating the vignette, I noticed that the size_by and color_by arguments occasionally didnt line up for numeric columns. Numeric columns need to be sorted first so the right colors are assigned. This was previously "tested" using the character 'run' column, though was more observable when testing against objective function values (which dont necessarily appear in ascending/descending order)
  • Loading branch information
barrettk committed Oct 4, 2024
1 parent ca905f7 commit 6d11e25
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions R/model-tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,19 @@ color_tree_by <- function(tree_data, color_by = "run"){
n_levels <- dplyr::n_distinct(vals)

# To preview color palette: scales::show_col(pal_bbr)
if(inherits(vals, "logical")){
if(inherits(vals, "numeric")){
# Gradient coloring (sorted): get colors for unique values (excluding NA)
sorted_vals <- sort(unique(vals))
pal_bbr <- scales::pal_gradient_n(bbr_cols)(seq(0, 1, length.out = n_levels))

# Assign colors based on sorted values
color_mapping <- setNames(pal_bbr, sorted_vals)
tree_data$col <- ifelse(
tree_data$col %in% sorted_vals,
color_mapping[as.character(tree_data$col)],
tree_data$col
)
}else if(inherits(vals, "logical")){
# Ensure both TRUE and FALSE colors are extracted, even if only one occurs
pal_bbr <- scales::pal_gradient_n(bbr_cols)(c(0,1))
# Explicitly set FALSE to white and TRUE to red
Expand All @@ -693,7 +705,7 @@ color_tree_by <- function(tree_data, color_by = "run"){
)
)
}else{
# Gradient coloring: get colors for unique values (excluding NA)
# Gradient coloring; doesn't need to be sorted
pal_bbr <- scales::pal_gradient_n(bbr_cols)(seq(0, 1, length.out = n_levels))
tree_data$col <- factor(tree_data$col)
levels(tree_data$col) <- c(node_colors, pal_bbr)
Expand Down

0 comments on commit 6d11e25

Please sign in to comment.