Skip to content

Adjust for S7 in ggplot2 #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method("$",patchwork)
S3method("$<-",patchwork)
S3method("&","ggplot2::gg")
S3method("&",gg)
S3method("*","ggplot2::gg")
S3method("*",gg)
S3method("-",ggplot)
S3method("/",ggplot)
Expand All @@ -25,16 +29,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)
Expand Down Expand Up @@ -73,12 +67,19 @@ S3method(simplify_gt,gtable_patchwork)
S3method(simplify_gt,inset_table)
S3method(simplify_gt,patchgrob)
S3method(str,patchwork)
export(add_ggplot)
export(add_grob)
export(add_gt_tbl)
export(add_plot_annotation)
export(add_plot_layout)
export(align_patches)
export(align_plots)
export(area)
export(build_fixed_dim_ggplot)
export(free)
export(get_dim)
export(get_max_dim)
export(gtable_fixed_dim_build)
export(guide_area)
export(inset_element)
export(patchGrob)
Expand Down
15 changes: 3 additions & 12 deletions R/add_plot.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
#' @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)
}

Expand Down
6 changes: 6 additions & 0 deletions R/arithmetic.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ NULL
}
e1 + e2
}

#' @export
"&.ggplot2::gg" <- `&.gg`

#' @export
"*.ggplot2::gg" <- `*.gg`
2 changes: 1 addition & 1 deletion R/plot_annotation.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ default_annotation <- plot_annotation(
)
#' @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
Expand Down
2 changes: 1 addition & 1 deletion R/plot_layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ default_layout <- plot_layout(
)
#' @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
Expand Down
4 changes: 2 additions & 2 deletions R/plot_multipage.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ set_dim.patchwork <- function(plot, dim) {
}
#' @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)
Expand Down
17 changes: 17 additions & 0 deletions R/plot_patchwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ names.patchwork <- function(x) NULL
x
}
#' @export
`$.patchwork` <- function(x, i) {
if (i == "patches") {
attr(x, "patches")
} else {
NextMethod()
}
}
#' @export
`$<-.patchwork` <- function(x, i, value) {
if (i == "patches") {
attr(x, "patches") <- value
x
} else {
NextMethod()
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is absolutely lazyness on my part, because I wasn't keen on finding every instance of where the patches object is accessed.

#' @export
as.list.patchwork <- function(x, ...) {
get_patches(x)$plots
}
Expand Down
40 changes: 40 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

})
Loading