View source: R/geom_potential.R
| geom_potential | R Documentation |
geom_potential() adds a raster layer to a ggplot object, visualizing the
potential function derived from a conservative vector field. It computes the
potential numerically over a specified grid and displays it as a heatmap.
geom_potential(
mapping = NULL,
data = NULL,
stat = StatPotential,
position = "identity",
...,
na.rm = FALSE,
inherit.aes = FALSE,
show.legend = NA,
fun,
xlim = NULL,
ylim = NULL,
n = 51,
tol = 1e-06,
verify_conservative = FALSE
)
stat_potential(
mapping = NULL,
data = NULL,
geom = GeomPotential,
position = "identity",
...,
na.rm = FALSE,
inherit.aes = FALSE,
show.legend = NA,
fun,
xlim = NULL,
ylim = NULL,
n = 51,
tol = 1e-06,
verify_conservative = FALSE
)
mapping |
A set of aesthetic mappings created by |
data |
The data to be displayed in this layer. If |
stat |
The statistical transformation to use on the data (default: StatPotential). |
position |
Position adjustment, either as a string or the result of a position adjustment function. |
... |
Other arguments passed to |
na.rm |
Logical. If |
inherit.aes |
Logical. If |
show.legend |
Logical. Should this layer be included in the legends? |
fun |
A function that takes a numeric vector of length 2 ( |
xlim |
Numeric vector of length 2 defining the domain limits on the
x-axis. Defaults to |
ylim |
Numeric vector of length 2 defining the domain limits on the
y-axis. Defaults to |
n |
Integer. Number of grid points along each axis for computing the
potential. Defaults to |
tol |
Numeric. Tolerance for verifying if the vector field is
conservative. Defaults to |
verify_conservative |
Logical. If |
geom |
The geometric object used to render the potential function. Defaults to GeomPotential. |
Note: the potential is only known up to a constant. The point of reference
used is the lower left corner c(xlim[1], ylim[1]), where the potential is
assumed to be 0.
A ggplot2 layer that produces a potential function heatmap.
geom_potential() accepts all aesthetics supported by
GeomRaster. In particular, the key aesthetics include:
fill: The computed potential value at each grid cell, which is mapped to a color scale.
x and y: The coordinates of the grid cell centers. (calculated)
alpha: Controls the transparency of the raster fill.
Additional raster-specific aesthetics (e.g. those controlled by
scale_fill_gradient(), scale_fill_viridis_c(), etc.) can be applied to
modify the appearance of the potential heatmap.
The following variables are computed internally by StatPotential during the potential function calculation:
The scalar potential value computed numerically at each grid point.
It represents the accumulated potential from a reference point (typically the lower
bounds of xlim and ylim) to the given point. This value is mapped to
the fill aesthetic in the raster layer.
The components of the vector field at each grid point.
Only with verify_conservative = TRUE: TRUE where
the field passes the check that it is conservative (its curl is within
tol of zero).
scalar_field <- function(u){
x <- u[1]; y <- u[2]
(x + y)^2 + 4*(x - y)^2 - 8*(.5)^2
}
gradient_field <- function(u) numDeriv::grad(scalar_field, u)
s <- seq(-1, 1, length.out = 51)
expand.grid("x" = s, "y" = s) |>
transform(phi = apply(cbind(x, y), 1, scalar_field)) |>
ggplot(aes(x, y)) + geom_raster(aes(fill = phi))
ggplot() + geom_potential(fun = gradient_field)
ggplot() + geom_potential(fun = gradient_field, verify_conservative = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.