Repository navigation
Closed
Description
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 neededCreated 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:
Line 121 in 01155ba
| 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.
Reacted by atusy, Hadley Wickham and Jonathan Zadra

Activity
atusy commentedon Nov 23, 2018
There's another inconsistency among
coord_cartesian()andscale_*_continuous1().Limits for coord_cartesian can be length larger than 3 due to
range()inscale_range()unlike limits forscale_*_continuous().Thus, in
ifelse(is.na(limits), scale$get_limits(), limits)by @eliocamp,scale$get_limits()will be re-cycled to have same length withlimits.It may result in unexpected behavior.
Created on 2018-11-23 by the reprex package (v0.2.1)
I think
limitsargument should only acceptIs there any reason that
coord_cartesianneeds to acceptlimitswith length larger than 2?hadley commentedon Jun 18, 2019
I just tried to do this too!
lock commentedon Dec 28, 2019
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/