Skip to content

Adding Layout slot to ggplot object #5576

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

Merged
merged 12 commits into from
Dec 15, 2023
Merged
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ggplot2 (development version)

* The ggplot object now contains `$layout` which points to the `Layout` ggproto
object and will be used by the `ggplot_build.ggplot` method. This was exposed
so that package developers may extend the behavior of the `Layout` ggproto object
without needing to develop an entirely new `ggplot_build` method (@jtlandis, #5077).

* `coord_polar()` can have free scales in facets (@teunbrand, #2815).

* The new argument `axes` in `facet_grid()` and `facet_wrap()` controls the
Expand Down
7 changes: 5 additions & 2 deletions R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
# This includes managing the parameters for the facet and the coord
# so that we don't modify the ggproto object in place.

create_layout <- function(facet = FacetNull, coord = CoordCartesian) {
ggproto(NULL, Layout, facet = facet, coord = coord)
create_layout <- function(facet, coord, layout = NULL) {
layout <- layout %||% Layout
check_inherits(layout, "Layout")
ggproto(NULL, layout, facet = facet, coord = coord)
}

#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
Expand Down
2 changes: 1 addition & 1 deletion R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ggplot_build.ggplot <- function(plot) {

# Initialise panels, add extra data for margins & missing faceting
# variables, and add on a PANEL variable to data
layout <- create_layout(plot$facet, plot$coordinates)
layout <- create_layout(plot$facet, plot$coordinates, plot$layout)
data <- layout$setup(data, plot$data, plot$plot_env)

# Compute aesthetics to produce data with generalised variable names
Expand Down
3 changes: 2 additions & 1 deletion R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ ggplot.default <- function(data = NULL, mapping = aes(), ...,
theme = list(),
coordinates = coord_cartesian(default = TRUE),
facet = facet_null(),
plot_env = environment
plot_env = environment,
layout = ggproto(NULL, Layout)
), class = c("gg", "ggplot"))

p$labels <- make_labels(mapping)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-facet-layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ c <- data_frame(b = 3)
empty <- data_frame()

panel_layout <- function(facet, data) {
layout <- create_layout(facet)
layout <- create_layout(facet = facet, coord = CoordCartesian)
layout$setup(data)
layout$layout
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-facet-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ df_b <- unique(df["b"])
df_c <- unique(data_frame(c = 1))

panel_map_one <- function(facet, data, plot_data = data) {
layout <- create_layout(facet)
layout <- create_layout(facet = facet, coord = CoordCartesian)
layout$setup(list(data), plot_data)[[1]]
}

Expand Down