Skip to content

Commit f512174

Browse files
jtlandisteunbrand
andauthored
Adding Layout slot to ggplot object (#5576)
* as an S3 generic for class * tests required to pass tests * reverting changes according to @teunbrand 's request * It is probably important to include a test that whatever makes it through `plot$layout` is actually a Layout ggproto object * adding layout slot * Typos: forgot to remove `plot` from body of create_layout * Update R/layout.R teunbrand's suggestion for checking Layout type Co-authored-by: Teun van den Brand <[email protected]> * adding News bullet * This is likely a safer implementation - previously, if someone were to modify the it would affect all future ggplot objects and any still in memory. * typo, wrote layer, meant to say Layout * clearer word choice --------- Co-authored-by: Teun van den Brand <[email protected]>
1 parent acf3641 commit f512174

File tree

6 files changed

+15
-6
lines changed

6 files changed

+15
-6
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ggplot2 (development version)
22

3+
* The ggplot object now contains `$layout` which points to the `Layout` ggproto
4+
object and will be used by the `ggplot_build.ggplot` method. This was exposed
5+
so that package developers may extend the behavior of the `Layout` ggproto object
6+
without needing to develop an entirely new `ggplot_build` method (@jtlandis, #5077).
7+
38
* `coord_polar()` can have free scales in facets (@teunbrand, #2815).
49

510
* The new argument `axes` in `facet_grid()` and `facet_wrap()` controls the

R/layout.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
# This includes managing the parameters for the facet and the coord
77
# so that we don't modify the ggproto object in place.
88

9-
create_layout <- function(facet = FacetNull, coord = CoordCartesian) {
10-
ggproto(NULL, Layout, facet = facet, coord = coord)
9+
create_layout <- function(facet, coord, layout = NULL) {
10+
layout <- layout %||% Layout
11+
check_inherits(layout, "Layout")
12+
ggproto(NULL, layout, facet = facet, coord = coord)
1113
}
14+
1215
#' @rdname ggplot2-ggproto
1316
#' @format NULL
1417
#' @usage NULL

R/plot-build.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ggplot_build.ggplot <- function(plot) {
4646

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

5252
# Compute aesthetics to produce data with generalised variable names

R/plot.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ ggplot.default <- function(data = NULL, mapping = aes(), ...,
127127
theme = list(),
128128
coordinates = coord_cartesian(default = TRUE),
129129
facet = facet_null(),
130-
plot_env = environment
130+
plot_env = environment,
131+
layout = ggproto(NULL, Layout)
131132
), class = c("gg", "ggplot"))
132133

133134
p$labels <- make_labels(mapping)

tests/testthat/test-facet-layout.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ c <- data_frame(b = 3)
44
empty <- data_frame()
55

66
panel_layout <- function(facet, data) {
7-
layout <- create_layout(facet)
7+
layout <- create_layout(facet = facet, coord = CoordCartesian)
88
layout$setup(data)
99
layout$layout
1010
}

tests/testthat/test-facet-map.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ df_b <- unique(df["b"])
44
df_c <- unique(data_frame(c = 1))
55

66
panel_map_one <- function(facet, data, plot_data = data) {
7-
layout <- create_layout(facet)
7+
layout <- create_layout(facet = facet, coord = CoordCartesian)
88
layout$setup(list(data), plot_data)[[1]]
99
}
1010

0 commit comments

Comments
 (0)