Skip to content

Commit ae1abff

Browse files
committed
Add an xlim parameter to stat_function.
Fixes #1528
1 parent 6ff3daf commit ae1abff

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 2.0.0.9000
22

3+
* `stat_function()` gains an `xlim` parameter (#1528).
4+
35
* `stat_summary()` preserves sorted x order which avoids artefacts when
46
display results with `geom_smooth()` (#1520).
57

R/stat-function.r

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' @param fun function to use
77
#' @param n number of points to interpolate along
88
#' @param args list of additional arguments to pass to \code{fun}
9+
#' @param xlim Optionally, restrict the range of the function to this range.
910
#' @param na.rm If \code{FALSE} (the default), removes missing values with
1011
#' a warning. If \code{TRUE} silently removes missing values.
1112
#' @inheritParams stat_identity
@@ -51,6 +52,7 @@ stat_function <- function(mapping = NULL, data = NULL,
5152
geom = "path", position = "identity",
5253
...,
5354
fun,
55+
xlim = NULL,
5456
n = 101,
5557
args = list(),
5658
na.rm = FALSE,
@@ -69,6 +71,7 @@ stat_function <- function(mapping = NULL, data = NULL,
6971
n = n,
7072
args = args,
7173
na.rm = na.rm,
74+
xlim = xlim,
7275
...
7376
)
7477
)
@@ -81,8 +84,8 @@ stat_function <- function(mapping = NULL, data = NULL,
8184
StatFunction <- ggproto("StatFunction", Stat,
8285
default_aes = aes(y = ..y..),
8386

84-
compute_group = function(data, scales, fun, n = 101, args = list()) {
85-
range <- scales$x$dimension()
87+
compute_group = function(data, scales, fun, xlim = NULL, n = 101, args = list()) {
88+
range <- xlim %||% scales$x$dimension()
8689
xseq <- seq(range[1], range[2], length.out = n)
8790

8891
if (scales$x$is_discrete()) {

man/stat_function.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)