EVOLUTION-MANAGER
Edit File: geom_function.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Draw a function as a continuous curve</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for geom_function {ggplot2}"><tr><td>geom_function {ggplot2}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Draw a function as a continuous curve</h2> <h3>Description</h3> <p>Computes and draws a function as a continuous curve. This makes it easy to superimpose a function on top of an existing plot. The function is called with a grid of evenly spaced values along the x axis, and the results are drawn (by default) with a line. </p> <h3>Usage</h3> <pre> geom_function( mapping = NULL, data = NULL, stat = "function", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) stat_function( mapping = NULL, data = NULL, geom = "function", position = "identity", ..., fun, xlim = NULL, n = 101, args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>mapping</code></td> <td> <p>Set of aesthetic mappings created by <code><a href="aes.html">aes()</a></code> or <code><a href="aes_.html">aes_()</a></code>. If specified and <code>inherit.aes = TRUE</code> (the default), it is combined with the default mapping at the top level of the plot. You must supply <code>mapping</code> if there is no plot mapping.</p> </td></tr> <tr valign="top"><td><code>data</code></td> <td> <p>Ignored by <code>stat_function()</code>, do not use.</p> </td></tr> <tr valign="top"><td><code>stat</code></td> <td> <p>The statistical transformation to use on the data for this layer, as a string.</p> </td></tr> <tr valign="top"><td><code>position</code></td> <td> <p>Position adjustment, either as a string, or the result of a call to a position adjustment function.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Other arguments passed on to <code><a href="layer.html">layer()</a></code>. These are often aesthetics, used to set an aesthetic to a fixed value, like <code>colour = "red"</code> or <code>size = 3</code>. They may also be parameters to the paired geom/stat.</p> </td></tr> <tr valign="top"><td><code>na.rm</code></td> <td> <p>If <code>FALSE</code>, the default, missing values are removed with a warning. If <code>TRUE</code>, missing values are silently removed.</p> </td></tr> <tr valign="top"><td><code>show.legend</code></td> <td> <p>logical. Should this layer be included in the legends? <code>NA</code>, the default, includes if any aesthetics are mapped. <code>FALSE</code> never includes, and <code>TRUE</code> always includes. It can also be a named logical vector to finely select the aesthetics to display.</p> </td></tr> <tr valign="top"><td><code>inherit.aes</code></td> <td> <p>If <code>FALSE</code>, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. <code><a href="borders.html">borders()</a></code>.</p> </td></tr> <tr valign="top"><td><code>geom</code></td> <td> <p>The geometric object to use display the data</p> </td></tr> <tr valign="top"><td><code>fun</code></td> <td> <p>Function to use. Either 1) an anonymous function in the base or rlang formula syntax (see <code><a href="../../rlang/html/as_function.html">rlang::as_function()</a></code>) or 2) a quoted or character name referencing a function; see examples. Must be vectorised.</p> </td></tr> <tr valign="top"><td><code>xlim</code></td> <td> <p>Optionally, restrict the range of the function to this range.</p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p>Number of points to interpolate along the x axis.</p> </td></tr> <tr valign="top"><td><code>args</code></td> <td> <p>List of additional arguments passed on to the function defined by <code>fun</code>.</p> </td></tr> </table> <h3>Aesthetics</h3> <p><code>geom_function()</code> understands the following aesthetics (required aesthetics are in bold): </p> <ul> <li> <p><strong><code>x</code></strong> </p> </li> <li> <p><strong><code>y</code></strong> </p> </li> <li> <p><code>alpha</code> </p> </li> <li> <p><code>colour</code> </p> </li> <li> <p><code>group</code> </p> </li> <li> <p><code>linetype</code> </p> </li> <li> <p><code>size</code> </p> </li></ul> <p>Learn more about setting these aesthetics in <code>vignette("ggplot2-specs")</code>. </p> <h3>Computed variables</h3> <p><code>stat_function()</code> computes the following variables: </p> <dl> <dt>x</dt><dd><p>x values along a grid</p> </dd> <dt>y</dt><dd><p>value of the function evaluated at corresponding x</p> </dd> </dl> <h3>See Also</h3> <p><code><a href="../../rlang/html/as_function.html">rlang::as_function()</a></code> </p> <h3>Examples</h3> <pre> # geom_function() is useful for overlaying functions set.seed(1492) ggplot(data.frame(x = rnorm(100)), aes(x)) + geom_density() + geom_function(fun = dnorm, colour = "red") # To plot functions without data, specify range of x-axis base <- ggplot() + xlim(-5, 5) base + geom_function(fun = dnorm) base + geom_function(fun = dnorm, args = list(mean = 2, sd = .5)) # The underlying mechanics evaluate the function at discrete points # and connect the points with lines base + stat_function(fun = dnorm, geom = "point") base + stat_function(fun = dnorm, geom = "point", n = 20) base + geom_function(fun = dnorm, n = 20) # Two functions on the same plot base + geom_function(aes(colour = "normal"), fun = dnorm) + geom_function(aes(colour = "t, df = 1"), fun = dt, args = list(df = 1)) # Using a custom anonymous function base + geom_function(fun = function(x) 0.5*exp(-abs(x))) base + geom_function(fun = ~ 0.5*exp(-abs(.x))) # Using a custom named function f <- function(x) 0.5*exp(-abs(x)) base + geom_function(fun = f) </pre> <hr /><div style="text-align: center;">[Package <em>ggplot2</em> version 3.3.2 <a href="00Index.html">Index</a>]</div> </body></html>