Skip to content

Commit

Permalink
Merge pull request #1503 from rstudio/nanoplots-constant-val-fix
Browse files Browse the repository at this point in the history
Fix issue where constant-value nanoplots show jittered values
  • Loading branch information
rich-iannone authored Dec 3, 2023
2 parents 36fc9f4 + 8ff9e13 commit 57bad1e
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions R/utils_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ generate_nanoplot <- function(
y_scale_max <- get_extreme_value(y_vals, stat = "max")
y_scale_min <- get_extreme_value(y_vals, stat = "min")

if (y_scale_min == y_scale_max && is.null(expand_y)) {

# Expand the `y` scale, centering around the `y_scale_min` value
expand_y_dist <- (y_scale_min / 10) * 2
expand_y <- c(y_scale_min - expand_y_dist, y_scale_min + expand_y_dist)
}

# Ensure that a reference line or reference area isn't shown if NULL or
# any of its directives is NA
if (
Expand Down Expand Up @@ -1104,19 +1111,25 @@ generate_nanoplot <- function(
"</rect>"
)

if (rlang::is_integerish(y_scale_max) && rlang::is_integerish(y_scale_min)) {
y_axis_guide_vals_integerlike <- TRUE
} else {
y_axis_guide_vals_integerlike <- FALSE
}

y_value_max_label <-
format_number_compactly(
y_scale_max,
currency = currency,
as_integer = y_vals_integerlike,
as_integer = y_axis_guide_vals_integerlike,
fn = y_axis_fmt_fn
)

y_value_min_label <-
format_number_compactly(
y_scale_min,
currency = currency,
as_integer = y_vals_integerlike,
as_integer = y_axis_guide_vals_integerlike,
fn = y_axis_fmt_fn
)

Expand Down Expand Up @@ -1463,10 +1476,6 @@ get_extreme_value <- function(..., stat = c("max", "min")) {

value_list_vec <- unlist(value_list)

if (length(unique(value_list_vec)) == 1) {
value_list_vec <- jitter(value_list_vec, amount = 1 / 100000)
}

if (stat == "max") {
extreme_val <- max(value_list_vec, na.rm = TRUE)
} else {
Expand Down

2 comments on commit 57bad1e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.