Skip to content

Commit 52baf83

Browse files
committed
Merge pull request #908 from jimhester/one_limit
Allow NA in limits
2 parents 9b8dc05 + f35ea8a commit 52baf83

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
ggplot2 0.9.3.1.99
22
----------------------------------------------------------------
33

4+
* Allow specifying only one of the limits in a scale and use the automatic
5+
calculation of the other limit by passing NA to to the limit function (xlim
6+
or ylim). (Fixes #557) Thanks Jim Hester.
7+
48
* `stat_smooth()` checks for `method = "auto"` and `method = "glm"` in
59
a safer way.
610

@@ -13,6 +17,11 @@ ggplot2 0.9.3.1.99
1317
* Add `"none"` to documentation of `theme()` for parameter `legend.position`.
1418
(@krlmlr, #829)
1519

20+
MINOR FEATURES
21+
22+
* Allow specifying only one of the limits in a scale and use the automatic
23+
calculation of the other limit by passing NA to to the limit function (xlim
24+
or ylim). (Fixes #557) Thanks Jim Hester.
1625

1726
ggplot2 0.9.3.1
1827
----------------------------------------------------------------

R/limits.r

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#' Convenience functions to set the limits of the x and y axis.
22
#'
33
#' Observations not in this range will be dropped completely and
4-
#' not passed to any other layers.
4+
#' not passed to any other layers. If a NA value is substituted for one of the
5+
#' limits that limit is automatically calculated.
56
#'
67
#' @param ... if numeric, will create a continuous scale, if factor or
78
#' character, will create a discrete scale.
@@ -16,12 +17,16 @@
1617
#' xlim(c(10, 20))
1718
#' xlim("a", "b", "c")
1819
#' qplot(mpg, wt, data=mtcars) + xlim(15, 20)
20+
#' # with automatic lower limit
21+
#' qplot(mpg, wt, data=mtcars) + xlim(NA, 20)
1922
#'
2023
#' # ylim
2124
#' ylim(15, 20)
2225
#' ylim(c(10, 20))
2326
#' ylim("a", "b", "c")
24-
#' qplot(mpg, wt, data=mtcars) + ylim(15, 20)
27+
#' qplot(mpg, wt, data=mtcars) + ylim(0, 4)
28+
#' # with automatic upper limit
29+
#' qplot(mpg, wt, data=mtcars) + ylim(0, NA)
2530
xlim <- function(...) {
2631
limits(c(...), "x")
2732
}
@@ -47,7 +52,7 @@ limits <- function(lims, var) UseMethod("limits")
4752
#' @export
4853
limits.numeric <- function(lims, var) {
4954
stopifnot(length(lims) == 2)
50-
if (lims[1] > lims[2]) {
55+
if (!any(is.na(lims)) && lims[1] > lims[2]) {
5156
trans <- "reverse"
5257
} else {
5358
trans <- "identity"

R/scale-.r

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,16 @@ scale_limits <- function(scale) {
296296
}
297297

298298

299+
# if scale contains a NULL, use the default scale range
300+
# if scale contains a NA, use the default range for that axis, otherwise
301+
# use the user defined limit for that axis
299302
#' @export
300303
scale_limits.default <- function(scale) {
301-
scale$limits %||% scale$range$range
304+
if(!is.null(scale$limits)) {
305+
ifelse(!is.na(scale$limits), scale$limits, scale$range$range)
306+
} else {
307+
scale$range$range
308+
}
302309
}
303310

304311
# @kohske

man/xylim.Rd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ ylim(...)
1414
}
1515
\description{
1616
Observations not in this range will be dropped completely and
17-
not passed to any other layers.
17+
not passed to any other layers. If a NA value is substituted for one of the
18+
limits that limit is automatically calculated.
1819
}
1920
\examples{
2021
# xlim
@@ -23,12 +24,16 @@ xlim(20, 15)
2324
xlim(c(10, 20))
2425
xlim("a", "b", "c")
2526
qplot(mpg, wt, data=mtcars) + xlim(15, 20)
27+
# with automatic lower limit
28+
qplot(mpg, wt, data=mtcars) + xlim(NA, 20)
2629

2730
# ylim
2831
ylim(15, 20)
2932
ylim(c(10, 20))
3033
ylim("a", "b", "c")
31-
qplot(mpg, wt, data=mtcars) + ylim(15, 20)
34+
qplot(mpg, wt, data=mtcars) + ylim(0, 4)
35+
# with automatic upper limit
36+
qplot(mpg, wt, data=mtcars) + ylim(0, NA)
3237
}
3338
\seealso{
3439
For changing x or y axis limits \strong{without} dropping data

0 commit comments

Comments
 (0)