diff --git a/NAMESPACE b/NAMESPACE index 00434f0..d26a883 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,8 @@ # Generated by roxygen2: do not edit by hand +S3method("&","ggplot2::gg") S3method("&",gg) +S3method("*","ggplot2::gg") S3method("*",gg) S3method("-",ggplot) S3method("/",ggplot) @@ -25,16 +27,6 @@ S3method(as_patchwork,patchwork) S3method(c,patch_area) S3method(get_dim,ggplot) S3method(get_dim,patchwork) -S3method(ggplot_add,formula) -S3method(ggplot_add,ggplot) -S3method(ggplot_add,grob) -S3method(ggplot_add,gt_tbl) -S3method(ggplot_add,nativeRaster) -S3method(ggplot_add,plot_annotation) -S3method(ggplot_add,plot_layout) -S3method(ggplot_add,raster) -S3method(ggplot_build,fixed_dim_ggplot) -S3method(ggplot_gtable,fixed_dim_build) S3method(has_tag,ggplot) S3method(has_tag,guide_area) S3method(has_tag,inset_patch) diff --git a/R/add_plot.R b/R/add_plot.R index c2eabd5..ac03496 100644 --- a/R/add_plot.R +++ b/R/add_plot.R @@ -1,27 +1,15 @@ #' @importFrom ggplot2 ggplot_add -#' @export -ggplot_add.ggplot <- function(object, plot, object_name) { +add_ggplot <- function(object, plot, object_name) { patches <- get_patches(plot) add_patches(object, patches) } #' @importFrom ggplot2 ggplot_add -#' @export -ggplot_add.grob <- function(object, plot, object_name) { +add_grob <- function(object, plot, object_name) { table <- as_patch(object) plot + wrap_elements(full = object) } #' @importFrom ggplot2 ggplot_add -#' @export -ggplot_add.formula <- ggplot_add.grob -#' @importFrom ggplot2 ggplot_add -#' @export -ggplot_add.raster <- ggplot_add.grob -#' @importFrom ggplot2 ggplot_add -#' @export -ggplot_add.nativeRaster <- ggplot_add.grob -#' @importFrom ggplot2 ggplot_add -#' @export -ggplot_add.gt_tbl <- function(object, plot, object_name) { +add_gt_tbl <- function(object, plot, object_name) { plot + wrap_table(object) } diff --git a/R/arithmetic.R b/R/arithmetic.R index b0434b5..593620d 100644 --- a/R/arithmetic.R +++ b/R/arithmetic.R @@ -141,3 +141,9 @@ NULL } e1 + e2 } + +#' @export +"&.ggplot2::gg" <- `&.gg` + +#' @export +"*.ggplot2::gg" <- `*.gg` diff --git a/R/plot_annotation.R b/R/plot_annotation.R index 6f53f2f..5ba7fe6 100644 --- a/R/plot_annotation.R +++ b/R/plot_annotation.R @@ -97,8 +97,7 @@ default_annotation <- plot_annotation( theme = NULL ) #' @importFrom utils modifyList -#' @export -ggplot_add.plot_annotation <- function(object, plot, object_name) { +add_plot_annotation <- function(object, plot, object_name) { plot <- as_patchwork(plot) if (is.null(object$theme)) { plot$patches$annotation$theme <- NULL diff --git a/R/plot_layout.R b/R/plot_layout.R index 26637a4..1e092b0 100644 --- a/R/plot_layout.R +++ b/R/plot_layout.R @@ -314,8 +314,7 @@ default_layout <- plot_layout( axes = 'keep', axis_titles = 'keep' ) #' @importFrom utils modifyList -#' @export -ggplot_add.plot_layout <- function(object, plot, object_name) { +add_plot_layout <- function(object, plot, object_name) { plot <- as_patchwork(plot) do_change <- object[!vapply(object, is_waiver, logical(1))] plot$patches$layout[names(do_change)] <- do_change diff --git a/R/plot_multipage.R b/R/plot_multipage.R index 3db835d..cde1bd5 100644 --- a/R/plot_multipage.R +++ b/R/plot_multipage.R @@ -105,15 +105,13 @@ set_dim.patchwork <- function(plot, dim) { cli_abort('Setting dimensions on patchworks are currently unsupported') } #' @importFrom ggplot2 ggplot_build -#' @export -ggplot_build.fixed_dim_ggplot <- function(plot) { +build_fixed_dim_ggplot <- function(plot) { plot <- NextMethod() class(plot) <- c('fixed_dim_build', class(plot)) plot } #' @importFrom ggplot2 ggplot_gtable -#' @export -ggplot_gtable.fixed_dim_build <- function(data) { +gtable_fixed_dim_build <- function(data) { dim <- data$plot$fixed_dimensions table <- NextMethod() table <- add_strips(table) diff --git a/R/zzz.R b/R/zzz.R index f6ea651..972e3c2 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -56,4 +56,44 @@ on_load({ if ("unitType" %in% getNamespaceExports("grid")) { unitType <- grid::unitType } + if ("class_ggplot" %in% getNamespaceExports("ggplot2")) { + + # Class declarations + class_ggplot <- get("class_ggplot", envir = asNamespace("ggplot2")) + grob_like <- S7::new_union( + S7::new_S3_class("grob"), + S7::new_S3_class("formula"), + S7::new_S3_class("nativeRaster") + ) + + # Add methods + S7::method(ggplot_add, list(class_ggplot, class_ggplot)) <- add_ggplot + S7::method(ggplot_add, list(grob_like, class_ggplot)) <- add_grob + S7::method(ggplot_add, list(S7::new_S3_class("gt_tbl"), class_ggplot)) <- + add_gt_tbl + S7::method(ggplot_add, list(S7::new_S3_class("plot_layout"), class_ggplot)) <- + add_plot_layout + S7::method(ggplot_add, list(S7::new_S3_class("plot_annotation"), class_ggplot)) <- + add_plot_annotation + + # Build/gtable methods + S7::method(ggplot_build, S7::new_S3_class("fixed_dim_ggplot")) <- + build_fixed_dim_ggplot + S7::method(ggplot_gtable, S7::new_S3_class("fixed_dim_build")) <- + gtable_fixed_dim_build + } else { + # Add methods + register_s3_method("ggplot2", "ggplot_add", "ggplot", add_ggplot) + register_s3_method("ggplot2", "ggplot_add", "grob", add_grob) + register_s3_method("ggplot2", "ggplot_add", "formula", add_grob) + register_s3_method("ggplot2", "ggplot_add", "nativeRaster", add_grob) + register_s3_method("ggplot2", "ggplot_add", "gt_tbl", add_gt_tbl) + register_s3_method("ggplot2", "ggplot_add", "plot_layout", add_plot_layout) + register_s3_method("ggplot2", "ggplot_add", "plot_annotation", add_plot_annotation) + + # Build/gtable methods + register_s3_method("ggplot2", "ggplot_build", "fixed_dim_ggplot", build_fixed_dim_ggplot) + register_s3_method("ggplot2", "ggplot_gtable", "fxied_dim_build", gtable_fixed_dim_build) + } + })