stat_functions: Draw functions as curves

View source: R/stat-functions.R

stat_functionsR Documentation

Draw functions as curves

Description

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 or aes_. 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, and TRUE 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. See layer 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.

Value

A plot layer instance.

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")

}


ggpp documentation built on Nov. 8, 2023, 1:10 a.m.