tidyverse/ggplot2

scale_y_continuous() 'sec.axis' and Warning min(x) max(x)

Closed this issue · 7 comments

In a ggplot I am using a secondary Y-axis to display additionnal labels to my plot.
I use it like this :

scale_y_continuous(
      expand = c(0, 0),
      breaks = seq(0, N.rows, length.out = n.grad),
      labels = function(x) format(x, digits = 2, scientific = TRUE),
      sec.axis = sec_axis(
        trans = ~.,
        breaks = dt.data[right.y.hiden == FALSE]$right.y.label.pos,
        labels = dt.data[right.y.hiden == FALSE]$sample.string))

in sec_axis() breaks and labels are queried in a data.table dt.data:

  • If multiple breaks and labels are selected from the data.table -> no problems.
  • If there is only 1 break and 1 label queried from the data.table, displaying the plot returns this warning (I guess this is the matching translation from the language I use):
Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

The plot returned is fine : it totally matches my expectation.
But I don't understand why these warnings are printed.

So I basically have to questions:

  1. Why those warnings are printed when there is only 1 break & label ?
  2. Is there something I should mention in sec_axis() or elsewhere to get rid of these warnings ?

I don't know if it is reproducible, but from what I experienced, if you give just a single value to breaks and labels in sec_axis() such has:
sec.axis = sec_axis(trans = ~., breaks = 125, labels = "awesome single label")
This should trigger the warnings.

Could you provide a minimal reprex?

Sorry I though what I wrote would be enough to reproduce the issue.
Here is a reproducible example:

library(data.table)
library(ggplot2)

dt.data <- data.table(
  "data.covered" = c(10, 15, 50, 40),
  "sample.amount" = c(1, 2, 3, 4))

ggplot() + theme_gray() +
  theme(legend.title.align = 0.5,
        legend.text = element_text(size = 12),
        legend.position = "bottom") +
  geom_bar(data = dt.data,
           mapping = aes(x = 0, y = data.covered, fill = sample.amount),
           stat = "identity") +
  scale_y_continuous(
    expand = c(0, 0),
    breaks = seq(0, 120, length.out = 15),
    labels = function(x) format(x, digits = 2, scientific = TRUE),
    sec.axis = sec_axis(
      trans = ~.,
      breaks = 35,
      labels = "label test")) +
  scale_x_continuous(expand = c(0, 0))

Executing this chuck of code will produce the warning I describe (and it comes from sec_axis() as I provide only 1 single break and label to it).

EDIT: Here is my sessionInfo()

sessionInfo()

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.7 LTS

Matrix products: default
BLAS:   /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8    LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.3.3     data.table_1.14.0

loaded via a namespace (and not attached):
 [1] magrittr_2.0.1   tidyselect_1.1.0 munsell_0.5.0    colorspace_2.0-0 R6_2.5.0         rlang_0.4.10    
 [7] fansi_0.4.2      dplyr_1.0.4      tools_4.0.2      grid_4.0.2       gtable_0.3.0     utf8_1.2.1      
[13] DBI_1.1.1        withr_2.4.1      ellipsis_0.3.1   digest_0.6.27    assertthat_0.2.1 tibble_3.1.0    
[19] lifecycle_1.0.0  crayon_1.4.1     farver_2.1.0     purrr_0.3.4      vctrs_0.3.6      glue_1.4.2      
[25] labeling_0.4.2   compiler_4.0.2   pillar_1.5.1     generics_0.1.0   scales_1.1.1     pkgconfig_2.0.3 

Thanks. It seems it came from range(old_val_minor_trans) here when there are no minor breaks (i.e. range(numeric(0)) emits the warning). Probablye this should be skipped in the case.

scale$map(old_val_minor_trans, range(old_val_minor_trans)),

I don't understand the context of the line you quote. How should I use it in the reprex ?

It was just a quick note about how to fix this issue.

So contributors for ggplot2 will fix it ? (Sorry I am not so much used to issue process here).

Yes, I wrote it just in case someone might get motivated to fix this. Feel free to contribute :)