|
1 | 1 | #' Compute function for each x value
|
2 | 2 | #'
|
3 |
| -#' This stat makes it easy to superimpose a function on top of an existing |
4 |
| -#' plot. The function is called with a grid of evenly spaced values along |
5 |
| -#' the x axis, and the results are drawn (by default) with a line. |
| 3 | +#' This stat makes it easy to superimpose a function on top of an existing plot. |
| 4 | +#' The function is called with a grid of evenly spaced values along the x axis, |
| 5 | +#' and the results are drawn (by default) with a line. |
6 | 6 | #'
|
7 | 7 | #' @eval rd_aesthetics("stat", "function")
|
8 |
| -#' @param fun function to use or formula. Must be vectorised. |
9 |
| -#' @param n number of points to interpolate along |
10 |
| -#' @param args list of additional arguments to pass to `fun` |
| 8 | +#' @param fun Function to use. Either 1) an anonymous function in the base or |
| 9 | +#' rlang formula syntax (see \code{\link[rlang:as_function]{as_function()}}) |
| 10 | +#' or 2) a quoted or character name referencing a function; see examples. Must |
| 11 | +#' be vectorised. |
| 12 | +#' @param n Number of points to interpolate along |
| 13 | +#' @param args List of additional arguments to pass to `fun` |
11 | 14 | #' @param xlim Optionally, restrict the range of the function to this range.
|
12 | 15 | #' @inheritParams layer
|
13 | 16 | #' @inheritParams geom_point
|
|
16 | 19 | #' \item{x}{x's along a grid}
|
17 | 20 | #' \item{y}{value of function evaluated at corresponding x}
|
18 | 21 | #' }
|
| 22 | +#' @seealso \code{\link[rlang:as_function]{as_function()}} |
19 | 23 | #' @export
|
20 | 24 | #' @examples
|
| 25 | +#' |
| 26 | +#' # stat_function is useful for overlaying functions |
21 | 27 | #' set.seed(1492)
|
22 |
| -#' df <- data.frame( |
23 |
| -#' x = rnorm(100) |
24 |
| -#' ) |
25 |
| -#' x <- df$x |
26 |
| -#' base <- ggplot(df, aes(x)) + geom_density() |
27 |
| -#' base + stat_function(fun = dnorm, colour = "red") |
28 |
| -#' base + stat_function(fun = dnorm, colour = "red", args = list(mean = 3)) |
| 28 | +#' base <- ggplot(data.frame(x = rnorm(100)), aes(x)) |
| 29 | +#' base + geom_density() |
| 30 | +#' base + geom_density() + stat_function(fun = dnorm, colour = "red") |
29 | 31 | #'
|
30 |
| -#' # Plot functions without data |
31 |
| -#' # Examples adapted from Kohske Takahashi |
| 32 | +#' # To plot functions without data, specify range of x-axis |
| 33 | +#' base <- ggplot(data.frame(x = c(-5, 5)), aes(x)) |
| 34 | +#' base + stat_function(fun = dnorm) |
| 35 | +#' base + stat_function(fun = dnorm, args = list(mean = 2, sd = .5)) |
32 | 36 | #'
|
33 |
| -#' # Specify range of x-axis |
34 |
| -#' ggplot(data.frame(x = c(0, 2)), aes(x)) + |
35 |
| -#' stat_function(fun = exp, geom = "line") |
| 37 | +#' # The underlying mechanics evaluate the function at discrete points |
| 38 | +#' # and connect the points with lines |
| 39 | +#' base <- ggplot(data.frame(x = c(-5, 5)), aes(x)) |
| 40 | +#' base + stat_function(fun = dnorm) |
| 41 | +#' base + stat_function(fun = dnorm, geom = "path") # same |
| 42 | +#' base + stat_function(fun = dnorm, geom = "point") |
| 43 | +#' base + stat_function(fun = dnorm, geom = "point", n = 20) |
| 44 | +#' base + stat_function(fun = dnorm, n = 20) |
36 | 45 | #'
|
37 |
| -#' # Plot a normal curve |
38 |
| -#' ggplot(data.frame(x = c(-5, 5)), aes(x)) + stat_function(fun = dnorm) |
| 46 | +#' # Two functions on the same plot |
| 47 | +#' base + |
| 48 | +#' stat_function(fun = dnorm, colour = "red") + |
| 49 | +#' stat_function(fun = dt, colour = "blue", args = list(df = 1)) |
39 | 50 | #'
|
40 |
| -#' # To specify a different mean or sd, use the args parameter to supply new values |
41 |
| -#' ggplot(data.frame(x = c(-5, 5)), aes(x)) + |
42 |
| -#' stat_function(fun = dnorm, args = list(mean = 2, sd = .5)) |
| 51 | +#' # Using a custom anonymous function |
| 52 | +#' base + stat_function(fun = function(.x) .5*exp(-abs(.x))) |
| 53 | +#' base + stat_function(fun = ~ .5*exp(-abs(.x))) |
43 | 54 | #'
|
44 |
| -#' # Two functions on the same plot |
45 |
| -#' f <- ggplot(data.frame(x = c(0, 10)), aes(x)) |
46 |
| -#' f + stat_function(fun = sin, colour = "red") + |
47 |
| -#' stat_function(fun = cos, colour = "blue") |
| 55 | +#' # Using a custom named function |
| 56 | +#' f <- function(.x) .5*exp(-abs(.x)) |
| 57 | +#' base + stat_function(fun = f) |
| 58 | +#' base + stat_function(fun = "f") |
48 | 59 | #'
|
49 |
| -#' # Using a custom function |
50 |
| -#' test <- function(x) {x ^ 2 + x + 20} |
51 |
| -#' f + stat_function(fun = test) |
52 |
| -#' f + stat_function(fun = ~ .x^2 + .x + 20) |
53 | 60 | stat_function <- function(mapping = NULL, data = NULL,
|
54 | 61 | geom = "path", position = "identity",
|
55 | 62 | ...,
|
|
0 commit comments