|
2 | 2 | #'
|
3 | 3 | #' This operator allows you to add objects to a ggplot or theme object.
|
4 | 4 | #'
|
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: |
8 | 7 | #'
|
9 | 8 | #' \itemize{
|
10 |
| -#' \item \code{data.frame}: replace current data.frame |
11 |
| -#' (must use \code{\%+\%}) |
12 | 9 | #' \item \code{uneval}: replace current aesthetics
|
13 | 10 | #' \item \code{layer}: add new layer
|
14 | 11 | #' \item \code{theme}: update plot theme
|
15 | 12 | #' \item \code{scale}: replace current scale
|
16 | 13 | #' \item \code{coord}: override current coordinate system
|
17 | 14 | #' \item \code{facet}: override current coordinate faceting
|
| 15 | +#' \item \code{list}: a list of any of the above. |
18 | 16 | #' }
|
19 | 17 | #'
|
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. |
22 | 20 | #'
|
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: |
25 | 22 | #'
|
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. |
30 | 24 | #'
|
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. |
35 | 28 | #'
|
| 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 |
36 | 39 | #' @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") |
40 | 47 | #'
|
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()) |
45 | 51 | #'
|
46 |
| -#' # Use a different data frame |
47 |
| -#' m <- mtcars[1:10, ] |
48 |
| -#' p %+% m |
| 52 | +#' # Adding on to a theme ---------------------------------------------- |
49 | 53 | #'
|
50 |
| -#' ### Adding objects to a theme object |
51 | 54 | #' # Compare these results of adding theme objects to other theme objects
|
52 | 55 | #' add_el <- theme_grey() + theme(text = element_text(family = "Times"))
|
53 | 56 | #' rep_el <- theme_grey() %+replace% theme(text = element_text(family = "Times"))
|
54 | 57 | #'
|
55 | 58 | #' add_el$text
|
56 | 59 | #' 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 |
64 | 60 | "+.gg" <- function(e1, e2) {
|
65 | 61 | # Get the name of what was passed in as e2, and pass along so that it
|
66 | 62 | # can be displayed in error messages
|
|
75 | 71 | #' @export
|
76 | 72 | "%+%" <- `+.gg`
|
77 | 73 |
|
78 |
| - |
79 | 74 | add_ggplot <- function(p, object, objectname) {
|
80 | 75 | if (is.null(object)) return(p)
|
81 | 76 |
|
@@ -106,7 +101,7 @@ add_ggplot <- function(p, object, objectname) {
|
106 | 101 | p
|
107 | 102 | } else if (is.list(object)) {
|
108 | 103 | for (o in object) {
|
109 |
| - p <- p + o |
| 104 | + p <- p %+% o |
110 | 105 | }
|
111 | 106 | } else if (is.layer(object)) {
|
112 | 107 | p$layers <- append(p$layers, object)
|
|
0 commit comments