Skip to content

NA limits in coords #2907

Closed
Closed

Description

@eliocamp

Setting NA in limits of coords in a similar fashion to scales ends up in an error.

library(ggplot2)

ggplot(cars, aes(speed, dist)) +
   geom_point() +
   coord_cartesian(ylim = c(25, NA))  # emulating scale_y_continuous(limits = c(25, NA))
#> Error in if (zero_range(range)) zero_width else diff(range): missing value where TRUE/FALSE needed

Created on 2018-09-21 by the reprex package (v0.2.0).

I've tinker a bit and found that the error is in this line:

range <- range(scale$transform(limits))

Adding this:
limits <- ifelse(is.na(limits), scale$get_limits(), limits)
solves the issue in this case.

library(ggplot2)

scale_range <- function(scale, limits = NULL, expand = TRUE) {
   expansion <- if (expand) ggplot2:::expand_default(scale) else c(0, 0)
   
   if (is.null(limits)) {
      scale$dimension(expansion)
   } else {
      limits <- ifelse(is.na(limits), scale$get_limits(), limits)
      range <- range(scale$transform(limits))
      scales::expand_range(range, expansion[1], expansion[2])
   } 
}

assignInNamespace("scale_range", scale_range, ns = "ggplot2")

ggplot(cars, aes(speed, dist)) +
   geom_point() +
   coord_cartesian(ylim = c(25, NA))  

Created on 2018-09-21 by the reprex package (v0.2.0).

I'm not versed enough on how ggplot2 handles coords to know if it's a robust fix across different coords, though.

Activity

  1. atusy commented on Nov 23, 2018

    @atusy
    Contributor

    There's another inconsistency among coord_cartesian() and scale_*_continuous1().
    Limits for coord_cartesian can be length larger than 3 due to range() in scale_range() unlike limits for scale_*_continuous().

    Thus, in ifelse(is.na(limits), scale$get_limits(), limits) by @eliocamp, scale$get_limits() will be re-cycled to have same length with limits.
    It may result in unexpected behavior.

    library(ggplot2)
    
    scale_range <- function(scale, limits = NULL, expand = TRUE) {
       expansion <- if (expand) ggplot2:::expand_default(scale) else c(0, 0)
       
       if (is.null(limits)) {
          scale$dimension(expansion)
       } else {
          limits <- ifelse(is.na(limits), scale$get_limits(), limits)
          range <- range(scale$transform(limits))
          scales::expand_range(range, expansion[1], expansion[2])
       } 
    }
    
    assignInNamespace("scale_range", scale_range, ns = "ggplot2")
    
    ggplot(cars, aes(speed, dist)) +
       geom_point() +
       coord_cartesian(ylim = c(25, NA, NA))

    image

    Created on 2018-11-23 by the reprex package (v0.2.1)

    I think limits argument should only accept

    - a finite value
    - a numeric vector of length 2 with or without NA
    

    Is there any reason that coord_cartesian needs to accept limits with length larger than 2?

  2. added
    feature
    on Apr 11, 2019
  3. hadley commented on Jun 18, 2019

    @hadley
    Member

    I just tried to do this too!

  4. lock commented on Dec 28, 2019

    @lock

    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/

  5. locked and limited conversation to collaborators on Dec 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coord 🗺️feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions