Skip to content

Commit 4c7b76b

Browse files
committed
Improved layer documentation with description of different data arguments + examples of function as data
1 parent ba7501d commit 4c7b76b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

R/layer.r

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
#' Create a new layer
22
#'
3+
#' A layer is a combination of data, stat and geom with a potential position
4+
#' adjustment. Usually layers are created using \code{geom_*} or \code{stat_*}
5+
#' calls but it can also be created directly using the \code{layer} function.
6+
#'
7+
#' @details
8+
#' The data in a layer can be specified in one of three ways:
9+
#'
10+
#' \itemize{
11+
#' \item{If the data argument is \code{NULL} (the default) the data is
12+
#' inherited from the global plot data as specified in the call to
13+
#' \code{\link{ggplot}}.}
14+
#' \item{If the data argument is a function, that function is called with the
15+
#' global data as the only argument and the return value is used as the layer
16+
#' data. The function must return a data.frame.}
17+
#' \item{Any other type of value passed to \code{data} will be passed through
18+
#' \code{\link{fortify}}, and there must thus be a \code{fortify} method
19+
#' defined for the class of the value. Passing a data.frame is a special case
20+
#' of this as \code{fortify.data.frame} returns the data.frame untouched.}
21+
#' }
22+
#'
323
#' @export
424
#' @inheritParams geom_point
525
#' @param geom,stat,position Geom, stat and position adjustment to use in
@@ -16,6 +36,13 @@
1636
#' layer(geom = "point", stat = "identity", position = "identity",
1737
#' params = list(na.rm = FALSE)
1838
#' )
39+
#'
40+
#' # use a function as data to plot a subset of global data
41+
#' ggplot(mpg, aes(displ, hwy)) +
42+
#' layer(geom = "point", stat = "identity", position = "identity",
43+
#' data = head, params = list(na.rm = FALSE)
44+
#' )
45+
#'
1946
layer <- function(geom = NULL, stat = NULL,
2047
data = NULL, mapping = NULL,
2148
position = NULL, params = list(),

0 commit comments

Comments
 (0)