stat_functions()
computes values from functions and returns new data
containing numeric vectors for x
and y
. As function definitions
are passed through data
this statistic follows the grammar of graphics in
its behaviour.
Usage
stat_functions(
mapping = NULL,
data = NULL,
n = 101,
geom = "line",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
Arguments
- mapping
The aesthetic mapping, usually constructed with
aes
oraes_
. Only needs to be set at the layer level if you are overriding the plot defaults.- data
A layer specific dataset. Useful if the function curve is to be overlaid on other layers.
- n
integer Number of points to interpolate along the x axis.
- geom
The geometric object to use display the data
- position
The position adjustment to use on this layer
- na.rm
a logical indicating whether
NA
values should be stripped before the computation proceeds.- show.legend
logical. Should this layer be included in the legends?
NA
, the default, includes it if any aesthetics are mapped.FALSE
never includes, andTRUE
always includes.- inherit.aes
If
FALSE
, overrides the default aesthetics, rather than combining with them.- ...
other arguments passed on to
layer
. This can include aesthetics whose values you want to set, not map. Seelayer
for more details.
Details
This statistic can be used to plot values computed by functions. As
it follows the grammar of graphics, grouping and facets are supported. In
this it differs from geom_function
which behaves
like a plot annotation.
Aesthetics xmin
and xmax
should be mapped to numeric values
defining the range of the vector to be created and passed as argument to
the function to compute the y
values, and returned as x
in
data. n
is the length of this x
vector.
Computed variables
Data frame with n
rows or a multiple of
this, one for each
row in data
.
- x
numeric vector
- y
numeric vactor
- idx
integer vector, with values corresponding to rows in the input
data
, i.e., for each function
As shown in one example below geom_debug
can be
used to print the computed values returned by any statistic. The output
shown includes also values mapped to aesthetics.
Examples
# one function
df1 <- data.frame(min = 0, max = pi, fun = I(list(sin)))
ggplot(df1, aes(xmin = min, xmax = max, y = fun)) +
stat_functions()
ggplot(df1, aes(xmin = min, xmax = max, y = fun)) +
stat_functions(geom = "point", n = 20)
# two functions
df2 <- data.frame(min = -pi, max = pi,
fun = I(list(sin, cos)), name = c("sin", "cos"))
# each function must be in a separate group for correct plotting of lines
ggplot(df2, aes(xmin = min, xmax = max, y = fun, group = after_stat(idx))) +
stat_functions()
ggplot(df2, aes(xmin = min, xmax = max, y = fun, colour = name)) +
stat_functions()
ggplot(df2, aes(xmin = min, xmax = max, y = fun)) +
stat_functions() +
facet_grid(~ name)
# two curves with same function
df3 <- data.frame(min = c(-pi, 0),
max = c(0,pi),
fun = I(list(sin, sin)),
name = c("negative", "positive"))
ggplot(df3, aes(xmin = min, xmax = max, y = fun, colour = name)) +
stat_functions()
# We use geom_debug() to see the computed values
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed) {
library(gginnards)
ggplot(df1, aes(xmin = min, xmax = max, y = fun)) +
stat_functions(geom = "debug")
}
#> [1] "PANEL 1; group(s) -1; 'draw_function()' input 'data' (head):"
#> x idx y PANEL group xmin xmax
#> 1 0.00000000 1 0.00000000 1 -1 0 3.141593
#> 2 0.03141593 1 0.03141076 1 -1 0 3.141593
#> 3 0.06283185 1 0.06279052 1 -1 0 3.141593
#> 4 0.09424778 1 0.09410831 1 -1 0 3.141593
#> 5 0.12566371 1 0.12533323 1 -1 0 3.141593
#> 6 0.15707963 1 0.15643447 1 -1 0 3.141593