Skip to content

Commit

Permalink
Change ggplot_build and ggplot_gtable to generics (tidyverse#2370)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 authored and hadley committed Jan 2, 2018
1 parent a637c67 commit 152910a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ S3method(ggplot_add,labels)
S3method(ggplot_add,list)
S3method(ggplot_add,theme)
S3method(ggplot_add,uneval)
S3method(ggplot_build,ggplot)
S3method(ggplot_gtable,ggplot_built)
S3method(grid.draw,absoluteGrob)
S3method(grid.draw,ggplot)
S3method(grobHeight,absoluteGrob)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ up correct aspect ratio, and draws a graticule.
`panel_params` (#1311). These are parameters of the Coord that vary from
panel to panel.

* `ggplot_build` and `ggplot_gtable` are now generics so ggplot-subclasses can
define additional behavior during the build stage.

## Minor bug fixes and improvements

### Facetting
Expand Down
10 changes: 9 additions & 1 deletion R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#' @keywords internal
#' @export
ggplot_build <- function(plot) {
UseMethod('ggplot_build')
}

#' @export
ggplot_build.ggplot <- function(plot) {
plot <- plot_clone(plot)
if (length(plot$layers) == 0) {
plot <- plot + geom_blank()
Expand Down Expand Up @@ -141,8 +146,11 @@ layer_grob <- function(plot, i = 1L) {
#' @param data plot data generated by [ggplot_build()]
#' @export
ggplot_gtable <- function(data) {
stopifnot(inherits(data, "ggplot_built"))
UseMethod('ggplot_gtable')
}

#' @export
ggplot_gtable.ggplot_built <- function(data) {
plot <- data$plot
layout <- data$layout
data <- data$data
Expand Down

0 comments on commit 152910a

Please sign in to comment.