Skip to content

Commit

Permalink
Add ggplot_build() method for <ggplot_built> class (#5803)
Browse files Browse the repository at this point in the history
* Add `ggplot_build.ggplot_built` method

* small simplification in `get_guide_data()`

* add news bullet
  • Loading branch information
teunbrand committed May 20, 2024
1 parent f7cf2af commit e8cb599
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ S3method(ggplot_add,list)
S3method(ggplot_add,theme)
S3method(ggplot_add,uneval)
S3method(ggplot_build,ggplot)
S3method(ggplot_build,ggplot_built)
S3method(ggplot_gtable,ggplot_built)
S3method(grid.draw,absoluteGrob)
S3method(grid.draw,ggplot)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
alignment is not necessary (#5788).
* `position_stack()` skips computation when all `x` values are unique and
therefore stacking is not necessary (#5788).
* A new `ggplot_build()` S3 method for <ggplot_built> classes was added, which
returns input unaltered (@teunbrand, #5800).

# ggplot2 3.5.1

Expand Down
4 changes: 1 addition & 3 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,7 @@ get_guide_data <- function(plot = get_last_plot(), aesthetic, panel = 1L) {
check_string(aesthetic, allow_empty = FALSE)
aesthetic <- standardise_aes_names(aesthetic)

if (!inherits(plot, "ggplot_built")) {
plot <- ggplot_build(plot)
}
plot <- ggplot_build(plot)

if (!aesthetic %in% c("x", "y", "x.sec", "y.sec", "theta", "r")) {
# Non position guides: check if aesthetic in colnames of key
Expand Down
6 changes: 6 additions & 0 deletions R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ ggplot_build <- function(plot) {
UseMethod('ggplot_build')
}

#' @export
ggplot_build.ggplot_built <- function(plot) {
# This is a no-op
plot
}

#' @export
ggplot_build.ggplot <- function(plot) {
plot <- plot_clone(plot)
Expand Down

0 comments on commit e8cb599

Please sign in to comment.