ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Error when using axis_logticks with pseudo_log transformation

Open bkohrn opened this issue 1 year ago • 1 comments

I just updated to the latest versions of R and tidyverse (R 4.4.1, ggplot2 3.5.1), and decided to test out the new axis_logticks with a pseudo_log plot that I find myself doing fairly frequently. However, when I tried to run it, I ended up with an error message:

Error in seq.default(start, end, by = 1) : wrong sign in 'by' argument

Minimal example:

tibble("Y_val" = c(0, 1e-6, 1e-5, 1e-4),
       "X_val" = c(10, 20, 30, 40)) %>% 
  ggplot(
    aes(
      x = X_val,
      y = Y_val
    )
  ) + 
  geom_point() + 
  scale_y_continuous(
    transform = pseudo_log_trans(sigma = 1e-7, base = 10),
    guide = "axis_logticks"
  ) 

bkohrn avatar Sep 25 '24 19:09 bkohrn

Thanks for the report! This situation should be handled more gracefully by ggplot and thus requires a fix. Essentially, because the limits include 0, the axis assumed 0.1 is the smallest number to label via the negative.small argument. However this is still bigger than your largest number, so it gets the order wrong. In the meantime, you can set the negative.small argument to a smaller number, for which I recommend a number smaller than your smallest non-zero data.

library(ggplot2)

data.frame(
  "Y_val" = c(0, 1e-6, 1e-5, 1e-4),
  "X_val" = c(10, 20, 30, 40)
) |>
  ggplot(
    aes(
      x = X_val,
      y = Y_val
    )
  ) + 
  geom_point() + 
  scale_y_continuous(
    transform = scales::pseudo_log_trans(sigma = 1e-7, base = 10),
    guide = guide_axis_logticks(negative.small = 1e-7)
  ) 

Created on 2024-09-25 with reprex v2.1.1

teunbrand avatar Sep 25 '24 20:09 teunbrand