Skip to content

Commit eb64858

Browse files
committed
Improve + documentation
1 parent a8d62d3 commit eb64858

File tree

3 files changed

+64
-69
lines changed

3 files changed

+64
-69
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ There were a number of tweaks legend themes:
159159
stricter for aesthetics (previously there was no message), and less
160160
strict for parameters (previously this threw an error) (#1585).
161161

162+
* `+.gg` now works for lists that include data frames.
163+
162164
* `annotation_x()` now works in the absense of global data (#1655)
163165

164166
* `geom_*(show.legend = FALSE)` now works for `guide_colorbar`

R/plot-construction.r

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,61 @@
22
#'
33
#' This operator allows you to add objects to a ggplot or theme object.
44
#'
5-
#' If the first object is an object of class \code{ggplot}, you can add
6-
#' the following types of objects, and it will return a modified ggplot
7-
#' object.
5+
#' @section Adding on to a ggplot object:
6+
#' You can add any of the following types of objects:
87
#'
98
#' \itemize{
10-
#' \item \code{data.frame}: replace current data.frame
11-
#' (must use \code{\%+\%})
129
#' \item \code{uneval}: replace current aesthetics
1310
#' \item \code{layer}: add new layer
1411
#' \item \code{theme}: update plot theme
1512
#' \item \code{scale}: replace current scale
1613
#' \item \code{coord}: override current coordinate system
1714
#' \item \code{facet}: override current coordinate faceting
15+
#' \item \code{list}: a list of any of the above.
1816
#' }
1917
#'
20-
#' If the first object is an object of class \code{theme}, you can add
21-
#' another theme object. This will return a modified theme object.
18+
#' To replace the current default data frame, you must use \code{\%+\%},
19+
#' due to S3 method precedence issues.
2220
#'
23-
#' For theme objects, the \code{+} operator and the \code{\%+replace\%}
24-
#' can be used to modify elements in themes.
21+
#' @section Adding on to a theme:
2522
#'
26-
#' The \code{+} operator updates the elements of e1 that differ from
27-
#' elements specified (not NULL) in e2.
28-
#' Thus this operator can be used to incrementally add or modify attributes
29-
#' of a ggplot theme.
23+
#' \code{+} and \code{\%+replace\%} can be used to modify elements in themes.
3024
#'
31-
#' In contrast, the \code{\%+replace\%} operator replaces the
32-
#' entire element; any element of a theme not specified in e2 will not be
33-
#' present in the resulting theme (i.e. NULL).
34-
#' Thus this operator can be used to overwrite an entire theme.
25+
#' \code{+} updates the elements of e1 that differ from elements specified (not
26+
#' NULL) in e2. Thus this operator can be used to incrementally add or modify
27+
#' attributes of a ggplot theme.
3528
#'
29+
#' In contrast, \code{\%+replace\%} replaces the entire element; any element of
30+
#' a theme not specified in e2 will not be present in the resulting theme (i.e.
31+
#' NULL). Thus this operator can be used to overwrite an entire theme.
32+
#'
33+
#' @param e1 An object of class \code{\link{ggplot}} or a \code{\link{theme}}.
34+
#' @param e2 A component, or list of components to add to \code{e1}.
35+
#' @seealso \code{\link{theme}}
36+
#' @export
37+
#' @method + gg
38+
#' @rdname gg-add
3639
#' @examples
37-
#' ### Adding objects to a ggplot object
38-
#' p <- ggplot(mtcars, aes(wt, mpg, colour = disp)) +
39-
#' geom_point()
40+
#' # Adding on to a plot -----------------------------------------------
41+
#'
42+
#' base <- ggplot(mpg, aes(displ, hwy)) + geom_point()
43+
#' base + geom_smooth()
44+
#'
45+
#' # To override the data, you must use %+%
46+
#' base %+% subset(mpg, fl == "p")
4047
#'
41-
#' p
42-
#' p + coord_cartesian(ylim = c(0, 40))
43-
#' p + scale_colour_continuous(breaks = c(100, 300))
44-
#' p + guides(colour = "colourbar")
48+
#' # Alternatively, you can add multiple components with a list.
49+
#' # This can be useful to return from a list.
50+
#' base + list(subset(mpg, fl == "p"), geom_smooth())
4551
#'
46-
#' # Use a different data frame
47-
#' m <- mtcars[1:10, ]
48-
#' p %+% m
52+
#' # Adding on to a theme ----------------------------------------------
4953
#'
50-
#' ### Adding objects to a theme object
5154
#' # Compare these results of adding theme objects to other theme objects
5255
#' add_el <- theme_grey() + theme(text = element_text(family = "Times"))
5356
#' rep_el <- theme_grey() %+replace% theme(text = element_text(family = "Times"))
5457
#'
5558
#' add_el$text
5659
#' rep_el$text
57-
#'
58-
#' @param e1 An object of class \code{ggplot} or \code{theme}
59-
#' @param e2 A component to add to \code{e1}
60-
#' @export
61-
#' @seealso \code{\link{theme}}
62-
#' @method + gg
63-
#' @rdname gg-add
6460
"+.gg" <- function(e1, e2) {
6561
# Get the name of what was passed in as e2, and pass along so that it
6662
# can be displayed in error messages
@@ -75,7 +71,6 @@
7571
#' @export
7672
"%+%" <- `+.gg`
7773

78-
7974
add_ggplot <- function(p, object, objectname) {
8075
if (is.null(object)) return(p)
8176

@@ -106,7 +101,7 @@ add_ggplot <- function(p, object, objectname) {
106101
p
107102
} else if (is.list(object)) {
108103
for (o in object) {
109-
p <- p + o
104+
p <- p %+% o
110105
}
111106
} else if (is.layer(object)) {
112107
p$layers <- append(p$layers, object)

man/gg-add.Rd

Lines changed: 30 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)