Skip to content

Make add_ggplot an S3 method #3815

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ S3method("[<-",uneval)
S3method("[[",ggproto)
S3method("[[<-",uneval)
S3method(.DollarNames,ggproto)
S3method(add_ggplot,default)
S3method(as.list,ggproto)
S3method(autolayer,default)
S3method(autoplot,default)
Expand Down Expand Up @@ -246,6 +247,7 @@ export(StatSummaryBin)
export(StatSummaryHex)
export(StatUnique)
export(StatYdensity)
export(add_ggplot)
export(aes)
export(aes_)
export(aes_all)
Expand Down
30 changes: 24 additions & 6 deletions R/plot-construction.r
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,32 @@
#' @export
"%+%" <- `+.gg`

add_ggplot <- function(p, object, objectname) {
if (is.null(object)) return(p)
#' Add ggplot-like objects to custom objects
#'
#' This generic allows you to add your own methods for adding objects to
#' a ggplot-like with [+.gg].
#'
#' @param plot The ggplot-like object to add `object` to
#' @param object An object to add to the plot
#' @param object_name The name of the object to add
#'
#' @return A modified ggplot object
#'
#' @keywords internal
#' @export
add_ggplot <- function(plot, object, object_name) {
UseMethod("ggplot_add")
}
#' @export
add_ggplot.default <- function(plot, object, object_name) {
if (is.null(object)) return(plot)

p <- plot_clone(p)
p <- ggplot_add(object, p, objectname)
set_last_plot(p)
p
plot <- plot_clone(plot)
plot <- ggplot_add(object, plot, object_name)
set_last_plot(plot)
plot
}

#' Add custom objects to ggplot
#'
#' This generic allows you to add your own methods for adding custom objects to
Expand Down
23 changes: 23 additions & 0 deletions man/add_ggplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.