Skip to content
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

Setting panel sizes in theme() #6094

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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)

* Added `panel.widths` and `panel.heights` to `theme()` (#5338, @teunbrand).
* The `summary()` method for ggplots is now more terse about facets
(@teunbrand, #5989).
* `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle`
Expand Down
50 changes: 49 additions & 1 deletion R/layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Layout <- ggproto("Layout", NULL,
theme,
self$facet_params
)
plot_table <- self$set_panel_size(plot_table, theme)

# Draw individual labels, then add to gtable
labels <- self$coord$labels(
Expand Down Expand Up @@ -297,10 +298,57 @@ Layout <- ggproto("Layout", NULL,
})
names(label_grobs) <- names(labels)
label_grobs
},

set_panel_size = function(table, theme) {

new_widths <- calc_element("panel.widths", theme)
new_heights <- calc_element("panel.heights", theme)

if (is.null(new_widths) && is.null(new_heights)) {
return(table)
}

if (isTRUE(table$respect)) {
args <- !c(is.null(new_widths), is.null(new_heights))
args <- c("panel.widths", "panel.heights")[args]
cli::cli_warn(
"Aspect ratios are overruled by {.arg {args}} theme element{?s}."
)
teunbrand marked this conversation as resolved.
Show resolved Hide resolved
table$respect <- FALSE
}

rows <- panel_rows(table)
cols <- panel_cols(table)

if (length(new_widths) == 1L && nrow(cols) > 1L) {
# Get total size of non-panel widths in between panels
extra <- setdiff(seq(min(cols$l), max(cols$r)), union(cols$l, cols$r))
extra <- unit(sum(width_cm(table$widths[extra])), "cm")
# Distribute width proportionally
relative <- as.numeric(table$widths[cols$l]) # assumed to be simple units
new_widths <- (new_widths - extra) * (relative / sum(relative))
}
if (!is.null(new_widths)) {
table$widths[cols$l] <- rep(new_widths, length.out = nrow(cols))
}

if (length(new_heights) == 1L && nrow(rows) > 1L) {
# Get total size of non-panel heights in between panels
extra <- setdiff(seq(min(rows$t), max(rows$t)), union(rows$t, rows$b))
extra <- unit(sum(height_cm(table$heights[extra])), "cm")
# Distribute height proportionally
relative <- as.numeric(table$heights[rows$t]) # assumed to be simple units
new_heights <- (new_heights - extra) * (relative / sum(relative))
}
if (!is.null(new_heights)) {
table$heights[rows$t] <- rep(new_heights, length.out = nrow(rows))
}

table
}
)


# Helpers -----------------------------------------------------------------

# Function for applying scale method to multiple variables in a given
Expand Down
2 changes: 2 additions & 0 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,8 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
panel.grid.minor.x = el_def("element_line", "panel.grid.minor"),
panel.grid.minor.y = el_def("element_line", "panel.grid.minor"),
panel.ontop = el_def("logical"),
panel.widths = el_def("unit"),
panel.heights = el_def("unit"),

strip.background = el_def("element_rect", "rect"),
strip.background.x = el_def("element_rect", "strip.background"),
Expand Down
5 changes: 5 additions & 0 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
#' and x axis grid lines are vertical. `panel.grid.*.*` inherits from
#' `panel.grid.*` which inherits from `panel.grid`, which in turn inherits
#' from `line`
#' @param panel.widths,panel.heights Sizes for panels (`units`). Can be a
#' single unit to set the total size for the panel area, or a unit vector to
#' set the size of individual panels.
#' @param panel.ontop option to place the panel (background, gridlines) over
#' the data layers (`logical`). Usually used with a transparent or blank
#' `panel.background`.
Expand Down Expand Up @@ -427,6 +430,8 @@ theme <- function(...,
panel.grid.minor.x,
panel.grid.minor.y,
panel.ontop,
panel.widths,
panel.heights,
plot.background,
plot.title,
plot.title.position,
Expand Down
6 changes: 6 additions & 0 deletions man/theme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/testthat/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,47 @@ test_that("complete_theme completes a theme", {
reset_theme_settings()
})

test_that("panel.widths and panel.heights works with free-space panels", {

df <- data.frame(x = c(1, 1, 2, 1, 3), g = c("A", "B", "B", "C", "C"))

p <- ggplotGrob(
ggplot(df, aes(x, x)) +
geom_point() +
scale_x_continuous(expand = expansion(add = 1)) +
facet_grid(~ g, scales = "free_x", space = "free_x") +
theme(
panel.widths = unit(11, "cm"),
panel.spacing.x = unit(1, "cm")
)
)

idx <- range(panel_cols(p)$l)
expect_equal(as.numeric(p$widths[seq(idx[1], idx[2])]), c(2, 1, 3, 1, 4))

p <- ggplotGrob(
ggplot(df, aes(x, x)) +
geom_point() +
scale_y_continuous(expand = expansion(add = 1)) +
facet_grid(g ~ ., scales = "free_y", space = "free_y") +
theme(
panel.heights = unit(11, "cm"),
panel.spacing.y = unit(1, "cm")
)
)

idx <- range(panel_rows(p)$t)
expect_equal(as.numeric(p$heights[seq(idx[1], idx[2])]), c(2, 1, 3, 1, 4))

})

test_that("panel.widths and panel.heights appropriately warn about aspect override", {
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme(aspect.ratio = 1, panel.widths = unit(4, "cm"))
expect_warning(ggplotGrob(p), "Aspect ratios are overruled")
})

# Visual tests ------------------------------------------------------------

test_that("aspect ratio is honored", {
Expand Down
Loading