Skip to content

One limit #908

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

Merged
merged 5 commits into from
Feb 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
ggplot2 0.9.3.1.99
----------------------------------------------------------------

* Allow specifying only one of the limits in a scale and use the automatic
calculation of the other limit by passing NA to to the limit function (xlim
or ylim). (Fixes #557) Thanks Jim Hester.

* `stat_smooth()` checks for `method = "auto"` and `method = "glm"` in
a safer way.

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

MINOR FEATURES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this heading and put your news item at the top?


* Allow specifying only one of the limits in a scale and use the automatic
calculation of the other limit by passing NA to to the limit function (xlim
or ylim). (Fixes #557) Thanks Jim Hester.

ggplot2 0.9.3.1
----------------------------------------------------------------
Expand Down
11 changes: 8 additions & 3 deletions R/limits.r
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' Convenience functions to set the limits of the x and y axis.
#'
#' Observations not in this range will be dropped completely and
#' not passed to any other layers.
#' not passed to any other layers. If a NA value is substituted for one of the
#' limits that limit is automatically calculated.
#'
#' @param ... if numeric, will create a continuous scale, if factor or
#' character, will create a discrete scale.
Expand All @@ -16,12 +17,16 @@
#' xlim(c(10, 20))
#' xlim("a", "b", "c")
#' qplot(mpg, wt, data=mtcars) + xlim(15, 20)
#' # with automatic lower limit
#' qplot(mpg, wt, data=mtcars) + xlim(NA, 20)
#'
#' # ylim
#' ylim(15, 20)
#' ylim(c(10, 20))
#' ylim("a", "b", "c")
#' qplot(mpg, wt, data=mtcars) + ylim(15, 20)
#' qplot(mpg, wt, data=mtcars) + ylim(0, 4)
#' # with automatic upper limit
#' qplot(mpg, wt, data=mtcars) + ylim(0, NA)
xlim <- function(...) {
limits(c(...), "x")
}
Expand All @@ -47,7 +52,7 @@ limits <- function(lims, var) UseMethod("limits")
#' @export
limits.numeric <- function(lims, var) {
stopifnot(length(lims) == 2)
if (lims[1] > lims[2]) {
if (!any(is.na(lims)) && lims[1] > lims[2]) {
trans <- "reverse"
} else {
trans <- "identity"
Expand Down
9 changes: 8 additions & 1 deletion R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,16 @@ scale_limits <- function(scale) {
}


# if scale contains a NULL, use the default scale range
# if scale contains a NA, use the default range for that axis, otherwise
# use the user defined limit for that axis
#' @export
scale_limits.default <- function(scale) {
scale$limits %||% scale$range$range
if(!is.null(scale$limits)) {
ifelse(!is.na(scale$limits), scale$limits, scale$range$range)
} else {
scale$range$range
}
}

# @kohske
Expand Down
9 changes: 7 additions & 2 deletions man/xylim.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ ylim(...)
}
\description{
Observations not in this range will be dropped completely and
not passed to any other layers.
not passed to any other layers. If a NA value is substituted for one of the
limits that limit is automatically calculated.
}
\examples{
# xlim
Expand All @@ -23,12 +24,16 @@ xlim(20, 15)
xlim(c(10, 20))
xlim("a", "b", "c")
qplot(mpg, wt, data=mtcars) + xlim(15, 20)
# with automatic lower limit
qplot(mpg, wt, data=mtcars) + xlim(NA, 20)

# ylim
ylim(15, 20)
ylim(c(10, 20))
ylim("a", "b", "c")
qplot(mpg, wt, data=mtcars) + ylim(15, 20)
qplot(mpg, wt, data=mtcars) + ylim(0, 4)
# with automatic upper limit
qplot(mpg, wt, data=mtcars) + ylim(0, NA)
}
\seealso{
For changing x or y axis limits \strong{without} dropping data
Expand Down