Skip to content

Discrete scales cannot handle continuous values #3463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yutannihilation opened this issue Jul 30, 2019 · 3 comments · Fixed by #3487
Closed

Discrete scales cannot handle continuous values #3463

yutannihilation opened this issue Jul 30, 2019 · 3 comments · Fixed by #3487

Comments

@yutannihilation
Copy link
Member

It seems discrete scales cannot handle continuous values with the current master (the CRAN version works).

When I ran tests on my local, I saw some warnings around here:

test_that("discrete ranges also encompass continuous values", {
df <- data_frame(x1 = c("a", "b", "c"), x2 = c(0, 2, 4), y = 1:3)
base <- ggplot(df, aes(y = y)) + scale_x_discrete()
x_range <- function(x) {
layer_scales(x)$x$dimension()
}
expect_equal(x_range(base + geom_point(aes(x1))), c(1, 3))
expect_equal(x_range(base + geom_point(aes(x2))), c(0, 4))

#> Warning in structure(in_domain, pos = match(in_domain, breaks)): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
#>   Consider 'structure(list(), *)' instead.

For a simple example:

library(ggplot2)

df <- data.frame(x = c(0, 2, 4), y = 1:3, stringsAsFactors = FALSE)

ggplot(df, aes(x, y)) +
  geom_point() +
  scale_x_discrete()
#> Warning in structure(in_domain, pos = match(in_domain, breaks)): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
#>   Consider 'structure(list(), *)' instead.

#> Warning in structure(in_domain, pos = match(in_domain, breaks)): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
#>   Consider 'structure(list(), *)' instead.
#> Error in UseMethod("rescale"): no applicable method for 'rescale' applied to an object of class "list"

Created on 2019-07-30 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.1 (2019-07-05)
#>  os       macOS High Sierra 10.13.6   
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language en                          
#>  collate  ja_JP.UTF-8                 
#>  ctype    ja_JP.UTF-8                 
#>  tz       Asia/Tokyo                  
#>  date     2019-07-30                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib
#>  assertthat    0.2.1      2019-03-21 [1]
#>  backports     1.1.4      2019-04-10 [1]
#>  callr         3.3.1      2019-07-18 [1]
#>  cli           1.1.0      2019-03-19 [1]
#>  colorspace    1.4-1      2019-03-18 [1]
#>  crayon        1.3.4      2017-09-16 [1]
#>  curl          4.0        2019-07-22 [1]
#>  desc          1.2.0      2018-05-01 [1]
#>  devtools      2.1.0      2019-07-06 [1]
#>  digest        0.6.20     2019-07-04 [1]
#>  dplyr         0.8.3      2019-07-04 [1]
#>  evaluate      0.14       2019-05-28 [1]
#>  fs            1.3.1      2019-05-06 [1]
#>  ggplot2     * 3.2.0.9000 2019-07-25 [1]
#>  glue          1.3.1      2019-03-12 [1]
#>  gtable        0.3.0      2019-03-25 [1]
#>  highr         0.8        2019-03-20 [1]
#>  htmltools     0.3.6      2017-04-28 [1]
#>  httr          1.4.0      2018-12-11 [1]
#>  knitr         1.23       2019-05-18 [1]
#>  labeling      0.3        2014-08-23 [1]
#>  lazyeval      0.2.2      2019-03-15 [1]
#>  magrittr      1.5        2014-11-22 [1]
#>  memoise       1.1.0      2017-04-21 [1]
#>  mime          0.7        2019-06-11 [1]
#>  munsell       0.5.0      2018-06-12 [1]
#>  pillar        1.4.2      2019-06-29 [1]
#>  pkgbuild      1.0.3      2019-03-20 [1]
#>  pkgconfig     2.0.2      2018-08-16 [1]
#>  pkgload       1.0.2      2018-10-29 [1]
#>  prettyunits   1.0.2      2015-07-13 [1]
#>  processx      3.4.1      2019-07-18 [1]
#>  ps            1.3.0      2018-12-21 [1]
#>  purrr         0.3.2      2019-03-15 [1]
#>  R6            2.4.0      2019-02-14 [1]
#>  Rcpp          1.0.2      2019-07-25 [1]
#>  remotes       2.1.0      2019-06-24 [1]
#>  rlang         0.4.0.9000 2019-07-30 [1]
#>  rmarkdown     1.14       2019-07-12 [1]
#>  rprojroot     1.3-2      2018-01-03 [1]
#>  scales        1.0.0      2018-08-09 [1]
#>  sessioninfo   1.1.1      2018-11-05 [1]
#>  stringi       1.4.3      2019-03-12 [1]
#>  stringr       1.4.0      2019-02-10 [1]
#>  testthat      2.2.1      2019-07-25 [1]
#>  tibble        2.1.3      2019-06-06 [1]
#>  tidyselect    0.2.5      2018-10-11 [1]
#>  usethis       1.5.1      2019-07-04 [1]
#>  withr         2.1.2      2018-03-15 [1]
#>  xfun          0.8        2019-06-25 [1]
#>  xml2          1.2.1      2019-07-29 [1]
#>  yaml          2.2.0      2018-07-25 [1]
#>  source                            
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  Github (tidyverse/ggplot2@141c775)
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  Github (r-lib/rlang@415df7d)      
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.1)                    
#>  CRAN (R 3.6.0)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
@yutannihilation
Copy link
Member Author

It seems the change was introduced by #3426.

@yutannihilation
Copy link
Member Author

self$range$range can be NULL because train() is bypassed when is.discrete() returns FALSE.

train = function(self, x) {
if (is.discrete(x)) {
self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
} else {
self$range_c$train(x)
}
},

So, to fix this minimally, we can change the line 91 to self$range$range %||% integer():

get_limits = function(self) {
if (self$is_empty()) {
c(0, 1)
} else if (!is.null(self$limits) & !is.function(self$limits)){
self$limits
} else if (is.null(self$limits)) {
self$range$range
} else if (is.function(self$limits)) {
self$limits(self$range$range)
} else {
integer(0)
}
},

@lock
Copy link

lock bot commented Feb 22, 2020

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Feb 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant