Skip to content

Commit 8d9f428

Browse files
authored
Centralise documentation for common layer arguments (#6730)
* collect universal arguments in separate Rd * redirect `@inheritParams` * canonise `orientation` arg * replace `rd_orientation()` with `@inheritSection` * canonise line params * canonise `arrow` param * move docs to `docs_layer.R`
1 parent 3112348 commit 8d9f428

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2590
-2639
lines changed

R/annotation.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#' @param geom name of geom to use for annotation
1919
#' @param x,y,xmin,ymin,xmax,ymax,xend,yend positioning aesthetics -
2020
#' you must specify at least one of these.
21-
#' @inheritParams layer
22-
#' @inheritParams geom_point
21+
#' @inheritParams shared_layer_parameters
2322
#' @seealso
2423
#' The `r link_book("custom annotations section", "annotations#sec-custom-annotations")`
2524
#' @export

R/docs_layer.R

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,146 @@
1+
# Shared parameters -------------------------------------------------------
2+
3+
#' @name shared_layer_parameters
4+
#' @title Shared layer parameters
5+
#' @description
6+
#' This is a central place for describing typical layer parameters.
7+
#' It prevents cluttered definitions all over the place.
8+
#'
9+
#' @param mapping
10+
#' Set of aesthetic mappings created by [aes()]. If specified and `inherit.aes =
11+
#' TRUE` (the default), it is combined with the default mapping at the top level
12+
#' of the plot. You must supply `mapping` if there is no plot mapping.
13+
#'
14+
#' @param data
15+
#' The data to be displayed in this layer. There are three options:
16+
#' * `NULL` (default): the data is inherited from the plot data as specified
17+
#' in the call to [ggplot()].
18+
#' * A `data.frame`, or other object, will override the plot data. All objects
19+
#' will be fortified to produce a data frame. See [fortify()] for which
20+
#' variables will be created.
21+
#' * A `function` will be called with a single argument, the plot data. The return
22+
#' value must be a `data.frame`, and will be used as the layer data. A
23+
#' `function` can be created from a `formula` (e.g. `~ head(.x, 10)`).
24+
#'
25+
#' @param geom
26+
#' The geometric object to use to display the data for this layer. When using a
27+
#' `stat_*()` function to construct a layer, the `geom` argument can be used to
28+
#' override the default coupling between stats and geoms. The `geom` argument
29+
#' accepts the following:
30+
#' * A `Geom` ggproto subclass, for example `GeomPoint`.
31+
#' * A string naming the geom. To give the geom as a string, strip the
32+
#' function name of the `geom_` prefix. For example, to use `geom_point()`,
33+
#' give the geom as `"point"`.
34+
#' * For more information and other ways to specify the geom, see the
35+
#' [layer geom][layer_geoms] documentation.
36+
#'
37+
#' @param stat
38+
#' The statistical transformation to use on the data for this layer. When using
39+
#' a `geom_*()` function to construct a layer, the `stat` argument can be used
40+
#' to override the default coupling between geoms and stats. The `stat` argument
41+
#' accepts the following:
42+
#' * A `Stat` ggproto subclass, for example `StatCount`.
43+
#' * A string naming the stat. To give the stat as a string, strip the
44+
#' function name of the `stat_` prefix. For example, to use `stat_count()`,
45+
#' give the stat as `"count"`.
46+
#' * For more information and other ways to specify the stat, see the
47+
#' [layer stat][layer_stats] documentation.
48+
#'
49+
#' @param position
50+
#' A position adjustment to use on the data for this layer. This can be used in
51+
#' various ways, including to prevent overplotting and improving the display.
52+
#' The `position` argument accepts the following:
53+
#' * The result of calling a position function, such as `position_jitter()`.
54+
#' This method allows for passing extra arguments to the position.
55+
#' * A string naming the position adjustment. To give the position as a
56+
#' string, strip the function name of the `position_` prefix. For example, to
57+
#' use `position_jitter()`, give the position as `"jitter"`.
58+
#' * For more information and other ways to specify the position, see the
59+
#' [layer position][layer_positions] documentation.
60+
#'
61+
#' @param na.rm
62+
#' If `FALSE`, the default, missing values are removed with a warning. If
63+
#' `TRUE`, missing values are silently removed.
64+
#'
65+
#' @param show.legend
66+
#' Logical. Should this layer be included in the legends? `NA`, the default,
67+
#' includes if any aesthetics are mapped. `FALSE` never includes, and `TRUE`
68+
#' always includes. It can also be a named logical vector to finely select the
69+
#' aesthetics to display. To include legend keys for all levels, even when no
70+
#' data exists, use `TRUE`. If `NA`, all levels are shown in legend, but
71+
#' unobserved levels are omitted.
72+
#'
73+
#' @param inherit.aes
74+
#' If `FALSE`, overrides the default aesthetics, rather than combining with
75+
#' them. This is most useful for helper functions that define both data and
76+
#' aesthetics and shouldn't inherit behaviour from the default plot
77+
#' specification, e.g. [annotation_borders()].
78+
#'
79+
#' @param ...
80+
#' Other arguments passed on to [layer()]'s `params` argument. These arguments
81+
#' broadly fall into one of 4 categories below. Notably, further arguments to
82+
#' the `position` argument, or aesthetics that are required can
83+
#' *not* be passed through `...`. Unknown arguments that are not part of the 4
84+
#' categories below are ignored.
85+
#' * Static aesthetics that are not mapped to a scale, but are at a fixed
86+
#' value and apply to the layer as a whole. For example, `colour = "red"` or
87+
#' `linewidth = 3`. The geom's documentation has an **Aesthetics** section
88+
#' that lists the available options. The 'required' aesthetics cannot be
89+
#' passed on to the `params`. Please note that while passing unmapped
90+
#' aesthetics as vectors is technically possible, the order and required
91+
#' length is not guaranteed to be parallel to the input data.
92+
#' * When constructing a layer using
93+
#' a `stat_*()` function, the `...` argument can be used to pass on parameters
94+
#' to the `geom` part of the layer. An example of this is
95+
#' `stat_density(geom = "area", outline.type = "both")`. The geom's
96+
#' documentation lists which parameters it can accept.
97+
#' * Inversely, when constructing a layer using a
98+
#' `geom_*()` function, the `...` argument can be used to pass on parameters
99+
#' to the `stat` part of the layer. An example of this is
100+
#' `geom_area(stat = "density", adjust = 0.5)`. The stat's documentation lists
101+
#' which parameters it can accept.
102+
#' * The `key_glyph` argument of [`layer()`] may also be passed on through
103+
#' `...`. This can be one of the functions described as
104+
#' [key glyphs][draw_key], to change the display of the layer in the legend.
105+
#'
106+
#' @param lineend
107+
#' Line end style, one of `"round"`, `"butt"` or `"square"`.
108+
#'
109+
#' @param linejoin
110+
#' Line join style, one of `"round"`, `"mitre"` or `"bevel"`.
111+
#'
112+
#' @param linemitre
113+
#' Line mitre limit, a number greater than 1.
114+
#'
115+
#' @param arrow
116+
#' Arrow specification. Can be created by [grid::arrow()] or `NULL` to not draw
117+
#' an arrow.
118+
#'
119+
#' @param arrow.fill
120+
#' Fill colour to use for closed arrowheads. `NULL` means use `colour`
121+
#' aesthetic.
122+
#'
123+
#' @param orientation
124+
#' The orientation of the layer. The default (`NA`) automatically determines the
125+
#' orientation from the aesthetic mapping. In the rare event that this fails it
126+
#' can be given explicitly by setting `orientation` to either `"x"` or `"y"`.
127+
#' See the *Orientation* section for more detail.
128+
#'
129+
#' @section Orientation:
130+
#' This geom treats each axis differently and, thus, can have two
131+
#' orientations. Often the orientation is easy to deduce from a combination of
132+
#' the given mappings and the types of positional scales in use. Thus, ggplot2
133+
#' will by default try to guess which orientation the layer should have. Under
134+
#' rare circumstances, the orientation is ambiguous and guessing may fail. In
135+
#' that case the orientation can be specified directly using the `orientation`
136+
#' parameter, which can be either `"x"` or `"y"`. The value gives the axis that
137+
#' the geom should run along, `"x"` being the default orientation you would
138+
#' expect for the geom.
139+
#'
140+
#' @keywords internal
141+
#' @aliases NULL
142+
NULL
143+
1144
# Geoms -------------------------------------------------------------------
2145

3146
#' @title

R/geom-abline.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ NULL
3131
#'
3232
#' @seealso See [geom_segment()] for a more general approach to
3333
#' adding straight line segments to a plot.
34-
#' @inheritParams layer
35-
#' @inheritParams geom_point
34+
#' @inheritParams shared_layer_parameters
3635
#' @param mapping Set of aesthetic mappings created by [aes()].
3736
#' @param xintercept,yintercept,slope,intercept Parameters that control the
3837
#' position of the line. If these are set, `data`, `mapping` and

R/geom-bar.R

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ GeomBar <- ggproto(
6666
#' [position_fill()] shows relative proportions at each `x` by stacking the
6767
#' bars and then standardising each bar to have the same height.
6868
#'
69-
#' @eval rd_orientation()
69+
#' @inheritSection shared_layer_parameters Orientation
7070
#'
7171
#' @aesthetics GeomBar
7272
#' @aesthetics GeomCol
@@ -76,12 +76,7 @@ GeomBar <- ggproto(
7676
#' [position_dodge()] and [position_dodge2()] for creating side-by-side
7777
#' bar charts.
7878
#' @export
79-
#' @inheritParams layer
80-
#' @inheritParams geom_point
81-
#' @param orientation The orientation of the layer. The default (`NA`)
82-
#' automatically determines the orientation from the aesthetic mapping. In the
83-
#' rare event that this fails it can be given explicitly by setting `orientation`
84-
#' to either `"x"` or `"y"`. See the *Orientation* section for more detail.
79+
#' @inheritParams shared_layer_parameters
8580
#' @param just Adjustment for column placement. Set to `0.5` by default, meaning
8681
#' that columns will be centered about axis breaks. Set to `0` or `1` to place
8782
#' columns to the left/right of axis breaks. Note that this argument may have
@@ -90,8 +85,6 @@ GeomBar <- ggproto(
9085
#' @param geom,stat Override the default connection between `geom_bar()` and
9186
#' `stat_count()`. For more information about overriding these connections,
9287
#' see how the [stat][layer_stats] and [geom][layer_geoms] arguments work.
93-
#' @param lineend Line end style (round, butt, square).
94-
#' @param linejoin Line join style (round, mitre, bevel).
9588
#' @examples
9689
#' # geom_bar is designed to make it easy to create bar charts that show
9790
#' # counts (or sums of weights)

R/geom-bin2d.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ GeomBin2d <- ggproto("GeomBin2d", GeomTile)
1717
#' @aesthetics GeomBin2d
1818
#'
1919
#' @export
20-
#' @inheritParams layer
21-
#' @inheritParams geom_point
20+
#' @inheritParams shared_layer_parameters
2221
#' @param geom,stat Use to override the default connection between
2322
#' `geom_bin_2d()` and `stat_bin_2d()`. For more information about overriding
2423
#' these connections, see how the [stat][layer_stats] and [geom][layer_geoms]
2524
#' arguments work.
26-
#' @param lineend Line end style (round, butt, square).
27-
#' @param linejoin Line join style (round, mitre, bevel).
2825
#' @seealso [stat_bin_hex()] for hexagonal binning
2926
#' @examples
3027
#' d <- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)

R/geom-blank.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#' more details.
66
#'
77
#' @export
8-
#' @inheritParams layer
9-
#' @inheritParams geom_point
8+
#' @inheritParams shared_layer_parameters
109
#' @examples
1110
#' ggplot(mtcars, aes(wt, mpg))
1211
#' # Nothing to see here!

R/geom-boxplot.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#' It visualises five summary statistics (the median, two hinges
55
#' and two whiskers), and all "outlying" points individually.
66
#'
7-
#' @eval rd_orientation()
7+
#' @inheritSection shared_layer_parameters Orientation
88
#'
99
#' @section Summary statistics:
1010
#' The lower and upper hinges correspond to the first and third quartiles
@@ -29,8 +29,7 @@
2929
#' @seealso [geom_quantile()] for continuous `x`,
3030
#' [geom_violin()] for a richer display of the distribution, and
3131
#' [geom_jitter()] for a useful technique for small data.
32-
#' @inheritParams layer
33-
#' @inheritParams geom_bar
32+
#' @inheritParams shared_layer_parameters
3433
#' @param geom,stat Use to override the default connection between
3534
#' `geom_boxplot()` and `stat_boxplot()`. For more information about
3635
#' overriding these connections, see how the [stat][layer_stats] and

R/geom-contour.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ GeomContourFilled <- ggproto("GeomContourFilled", GeomPolygon)
3636
#'
3737
#' @aesthetics GeomContour
3838
#' @aesthetics GeomContourFilled
39-
#' @inheritParams layer
40-
#' @inheritParams geom_point
41-
#' @inheritParams geom_path
39+
#' @inheritParams shared_layer_parameters
4240
#' @param binwidth The width of the contour bins. Overridden by `bins`.
4341
#' @param bins Number of contour bins. Overridden by `breaks`.
4442
#' @param breaks One of:

R/geom-count.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ NULL
1313
#' these connections, see how the [stat][layer_stats] and [geom][layer_geoms]
1414
#' arguments work.
1515
#' @seealso For continuous `x` and `y`, use [geom_bin_2d()].
16-
#' @inheritParams layer
17-
#' @inheritParams geom_point
16+
#' @inheritParams shared_layer_parameters
1817
#' @export
1918
#' @examples
2019
#' ggplot(mpg, aes(cty, hwy)) +

R/geom-density.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ GeomDensity <- ggproto(
2121
#' the histogram. This is a useful alternative to the histogram for continuous
2222
#' data that comes from an underlying smooth distribution.
2323
#'
24-
#' @eval rd_orientation()
24+
#' @inheritSection shared_layer_parameters Orientation
2525
#' @aesthetics GeomDensity
2626
#' @seealso See [geom_histogram()], [geom_freqpoly()] for
2727
#' other methods of displaying continuous distribution.
2828
#' See [geom_violin()] for a compact density display.
29-
#' @inheritParams layer
30-
#' @inheritParams geom_bar
29+
#' @inheritParams shared_layer_parameters
3130
#' @inheritParams geom_ribbon
3231
#' @param geom,stat Use to override the default connection between
3332
#' `geom_density()` and `stat_density()`. For more information about

0 commit comments

Comments
 (0)