Skip to content

Commit 612b6d1

Browse files
committed
update stat-function docs: fun argument, links, and examples
1 parent 0e1a56f commit 612b6d1

File tree

2 files changed

+80
-64
lines changed

2 files changed

+80
-64
lines changed

R/stat-function.r

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#' Compute function for each x value
22
#'
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.
66
#'
77
#' @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`
1114
#' @param xlim Optionally, restrict the range of the function to this range.
1215
#' @inheritParams layer
1316
#' @inheritParams geom_point
@@ -16,40 +19,44 @@
1619
#' \item{x}{x's along a grid}
1720
#' \item{y}{value of function evaluated at corresponding x}
1821
#' }
22+
#' @seealso \code{\link[rlang:as_function]{as_function()}}
1923
#' @export
2024
#' @examples
25+
#'
26+
#' # stat_function is useful for overlaying functions
2127
#' 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")
2931
#'
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))
3236
#'
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)
3645
#'
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))
3950
#'
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)))
4354
#'
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")
4859
#'
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)
5360
stat_function <- function(mapping = NULL, data = NULL,
5461
geom = "path", position = "identity",
5562
...,

man/stat_function.Rd

Lines changed: 42 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)