Skip to content

Refactor facet panel drawing code #5917

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 19 commits into from
Jun 24, 2024
Merged
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* (internal) rearranged the code of `Facet$draw_paensl()` method (@teunbrand).
* `geom_rug()` prints a warning when `na.rm = FALSE`, as per documentation (@pn317, #5905)
* `position_dodge(preserve = "single")` now handles multi-row geoms better,
such as `geom_violin()` (@teunbrand based on @clauswilke's work, #2801).
Expand Down
86 changes: 84 additions & 2 deletions R/facet-.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,32 @@ Facet <- ggproto("Facet", NULL,
draw_front = function(data, layout, x_scales, y_scales, theme, params) {
rep(list(zeroGrob()), vec_unique_count(layout$PANEL))
},
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
cli::cli_abort("Not implemented.")
draw_panels = function(self, panels, layout, x_scales = NULL, y_scales = NULL,
ranges, coord, data = NULL, theme, params) {

free <- params$free %||% list(x = FALSE, y = FALSE)
space <- params$space_free %||% list(x = FALSE, y = FALSE)

if ((free$x || free$y) && !coord$is_free()) {
cli::cli_abort(
"{.fn {snake_class(self)}} can't use free scales with \\
{.fn {snake_class(coord)}}."
)
}

aspect_ratio <- theme$aspect.ratio
if (!is.null(aspect_ratio) && (space$x || space$y)) {
cli::cli_abort("Free scales cannot be mixed with a fixed aspect ratio.")
}
Comment on lines +150 to +152
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be triggered for FacetWrap as it has no params$space_free, which defaults to list(x = FALSE, y = FALSE).


table <- self$init_gtable(
panels, layout, theme, ranges, params,
aspect_ratio = aspect_ratio %||% coord$aspect(ranges[[1]]),
clip = coord$clip
)

table <- self$attach_axes(table, layout, ranges, coord, theme, params)
self$attach_strips(table, layout, params, theme)
},
draw_labels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, labels, params) {
panel_dim <- find_panel(panels)
Expand Down Expand Up @@ -173,6 +197,64 @@ Facet <- ggproto("Facet", NULL,
finish_data = function(data, layout, x_scales, y_scales, params) {
data
},
init_gtable = function(panels, layout, theme, ranges, params,
aspect_ratio = NULL, clip = "on") {

# Initialise matrix of panels
dim <- c(max(layout$ROW), max(layout$COL))
table <- matrix(list(zeroGrob()), dim[1], dim[2])
table[cbind(layout$ROW, layout$COL)] <- panels

# Set initial sizes
widths <- unit(rep(1, dim[2]), "null")
heights <- unit(rep(1 * abs(aspect_ratio %||% 1), dim[1]), "null")

# When space are free, let panel parameter limits determine size of panel
space <- params$space_free %||% list(x = FALSE, y = FALSE)
if (space$x) {
idx <- layout$PANEL[layout$ROW == 1]
widths <- vapply(idx, function(i) diff(ranges[[i]]$x.range), numeric(1))
widths <- unit(widths, "null")
}

if (space$y) {
idx <- layout$PANEL[layout$COL == 1]
heights <- vapply(idx, function(i) diff(ranges[[i]]$y.range), numeric(1))
heights <- unit(heights, "null")
}

# Build gtable
table <- gtable_matrix(
"layout", table,
widths = widths, heights = heights,
respect = !is.null(aspect_ratio),
clip = clip, z = matrix(1, dim[1], dim[2])
)

# Set panel names
table$layout$name <- paste(
"panel",
rep(seq_len(dim[2]), dim[1]),
rep(seq_len(dim[1]), each = dim[2]),
sep = "-"
)

# Add spacing between panels
spacing <- lapply(
c(x = "panel.spacing.x", y = "panel.spacing.y"),
calc_element, theme = theme
)

table <- gtable_add_col_space(table, spacing$x)
table <- gtable_add_row_space(table, spacing$y)
table
},
attach_axes = function(table, layout, ranges, coord, theme, params) {
table
},
attach_strips = function(table, layout, params, theme) {
table
},
vars = function() {
character(0)
}
Expand Down
Loading
Loading