% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stat-function-3d.R
\name{stat_function_3d}
\alias{stat_function_3d}
\alias{geom_function_3d}
\title{3D surface from a function}
\usage{
stat_function_3d(
  mapping = NULL,
  data = ensure_nonempty_data,
  geom = "surface_3d",
  position = "identity",
  ...,
  fun = NULL,
  xlim = NULL,
  ylim = NULL,
  grid = "rectangle",
  n = 40,
  direction = "x",
  trim = TRUE,
  cull_backfaces = FALSE,
  light = NULL,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

geom_function_3d(
  mapping = NULL,
  data = ensure_nonempty_data,
  stat = "function_3d",
  position = "identity",
  ...,
  fun = NULL,
  xlim = NULL,
  ylim = NULL,
  grid = "rectangle",
  n = 40,
  direction = "x",
  trim = TRUE,
  cull_backfaces = FALSE,
  light = NULL,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link[=aes]{aes()}}. By default, fill
is mapped to \code{after_stat(z)}.}

\item{data}{Ignored; this stat generates its own data.}

\item{geom}{Geom to use for rendering. Defaults to \code{"surface_3d"} for mesh
surfaces. Use \code{"ridgeline_3d"} for ridgeline rendering.}

\item{position}{Position adjustment, defaults to "identity".}

\item{...}{Other arguments passed to the layer.}

\item{fun}{Function to evaluate. Must accept (x, y) and return numeric z values.}

\item{xlim, ylim}{Length-2 numeric vectors giving the x and y ranges over which to
plot the function.}

\item{grid, n, direction, trim}{Parameters determining the geometry, resolution, and
orientation of the surface grid. See \link{grid_generation} for details.}

\item{cull_backfaces}{Logical indicating whether to remove back-facing polygons
from rendering. This is primarily for performance optimization but may be useful
for aesthetic reasons in some situations. Backfaces are determined using
screen-space winding order after 3D transformation. Defaults vary by geometry
type: FALSE for open surface-type geometries, TRUE for solid objects (hulls,
voxels, etc. where backfaces are generally hidden unless frontfaces are transparent
or explicitly disabled).}

\item{light}{A lighting specification object created by \code{\link[=light]{light()}}, \code{light("none")}
or the string \code{"none"} to disable lighting, or \code{NULL} to inherit plot-level
lighting. Set plot-level lighting by adding \code{\link[=light]{light()}} to the plot, and
layer-specific lighting via the \code{light} argument of \verb{geom_*_3d()} functions,
which takes precedence.}

\item{na.rm}{If \code{FALSE}, missing values are removed.}

\item{show.legend}{Logical indicating whether this layer should be included in legends.}

\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics.}

\item{stat}{Statistical transformation to use on the data. Defaults to \code{"surface_3d"}.}
}
\value{
A \code{Layer} object that can be added to a ggplot.
}
\description{
Evaluates a function f(x,y) = z over a regular grid and renders the result
as a 3D surface or ridgeline plot.
}
\section{Computed variables}{

\describe{
\item{x, y, z}{Grid coordinates and function values}
\item{dzdx, dzdy}{Partial derivatives at each point}
\item{slope}{Gradient magnitude: sqrt(dzdx^2 + dzdy^2)}
\item{aspect}{Direction of steepest slope: atan2(dzdy, dzdx)}
}
}

\examples{
# Basic function surface
ggplot() +
  geom_function_3d(fun = function(x, y) sin(x) * cos(y),
                   xlim = c(-pi, pi), ylim = c(-pi, pi)) +
  coord_3d()

\donttest{

# Fill by slope
ggplot() +
  geom_function_3d(fun = function(x, y) x^2 + y^2,
                   xlim = c(-2, 2), ylim = c(-2, 2),
                   aes(fill = after_stat(slope)),
                   grid = "equilateral") +
  scale_fill_viridis_c() +
  coord_3d()

# As ridgelines
ggplot() +
  stat_function_3d(fun = function(x, y) dnorm(x) * dnorm(y) * 10,
                   xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5), n = c(15, 30),
                   geom = "ridgeline_3d", base = 0, light = "none",
                   fill = "black", color = "white") +
  coord_3d()

}

}
\seealso{
\code{\link[=geom_surface_3d]{geom_surface_3d()}}, \code{\link[=geom_ridgeline_3d]{geom_ridgeline_3d()}}, \code{\link[=coord_3d]{coord_3d()}}
}
