2424# ' @param e1 An object of class [ggplot()] or a [theme()].
2525# ' @param e2 A plot component, as described below.
2626# ' @seealso [theme()]
27- # ' @export
28- # ' @method + gg
2927# ' @rdname gg-add
3028# ' @examples
3129# ' base <-
3937# ' # Alternatively, you can add multiple components with a list.
4038# ' # This can be useful to return from a function.
4139# ' base + list(subset(mpg, fl == "p"), geom_smooth())
42- " +.gg " <- function (e1 , e2 ) {
40+ add_gg <- function (e1 , e2 ) {
4341 if (missing(e2 )) {
4442 cli :: cli_abort(c(
4543 " Cannot use {.code +} with a single argument." ,
5250 e2name <- deparse(substitute(e2 ))
5351
5452 if (is.theme(e1 )) add_theme(e1 , e2 , e2name )
53+ # The `add_ggplot()` branch here is for backward compatibility with R < 4.3.0
54+ else if (is.ggplot(e1 )) add_ggplot(e1 , e2 , e2name )
5555 else if (is.ggproto(e1 )) {
5656 cli :: cli_abort(c(
5757 " Cannot add {.cls ggproto} objects together." ,
6060 }
6161}
6262
63+ if (getRversion() < " 4.3.0" ) {
64+ S7 :: method(`+` , list (class_S3_gg , S7 :: class_any )) <- add_gg
65+ }
66+
6367S7 :: method(`+` , list (class_ggplot , S7 :: class_any )) <- function (e1 , e2 ) {
6468 e2name <- deparse(substitute(e2 , env = caller_env(2 )))
6569 add_ggplot(e1 , e2 , e2name )
@@ -73,7 +77,13 @@ S7::method(`+`, list(class_theme, S7::class_any)) <- function(e1, e2) {
7377
7478# ' @rdname gg-add
7579# ' @export
76- " %+%" <- function (e1 , e2 ) e1 + e2
80+ " %+%" <- function (e1 , e2 ) {
81+ if (getRversion() < " 4.3.0" ) {
82+ add_gg(e1 , e2 )
83+ } else {
84+ `+`(e1 , e2 )
85+ }
86+ }
7787
7888add_ggplot <- function (p , object , objectname ) {
7989 if (is.null(object )) return (p )
0 commit comments