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

Partial margins #6116

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ S3method(makeContext,dotstackGrob)
S3method(merge_element,default)
S3method(merge_element,element)
S3method(merge_element,element_blank)
S3method(merge_element,margin)
S3method(pattern_alpha,GridPattern)
S3method(pattern_alpha,GridTilingPattern)
S3method(pattern_alpha,default)
Expand Down Expand Up @@ -484,6 +485,7 @@ export(layer_sf)
export(lims)
export(map_data)
export(margin)
export(margin_part)
export(max_height)
export(max_width)
export(mean_cl_boot)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Theme margins can have NA-units to inherit from parent elements. The new
function `margin_part()` has NA-units as default (@teunbrand, #6155)
* Built-in `theme_*()` functions now have `ink` and `paper` arguments to control
foreground and background colours respectively (@teunbrand)
* The `summary()` method for ggplots is now more terse about facets
Expand Down
7 changes: 7 additions & 0 deletions R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ margin <- function(t = 0, r = 0, b = 0, l = 0, unit = "pt") {
class(u) <- c("margin", class(u))
u
}

#' @rdname element
#' @export
margin_part <- function(t = NA, r = NA, b = NA, l = NA, unit = "pt") {
margin(t = t, r = r, b = b, l = l, unit = unit)
}

is.margin <- function(x) {
inherits(x, "margin")
}
Expand Down
2 changes: 1 addition & 1 deletion R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' - `element_geom()`: defaults for drawing layers.
#'
#' `rel()` is used to specify sizes relative to the parent,
#' `margin()` is used to specify the margins of elements.
#' `margin()` and `margin_part()` are used to specify the margins of elements.
#'
#' @param fill Fill colour.
#' @param colour,color Line/border colour. Color is an alias for colour.
Expand Down
25 changes: 25 additions & 0 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@
new
}

#' @rdname merge_element
#' @export
merge_element.margin <- function(new, old) {
if (is.null(old) || inherits(old, "element_blank")) {
return(new)
}
if (anyNA(new)) {
new[is.na(new)] <- old[is.na(new)]
}
new
}

#' Combine the properties of two elements
#'
#' @param e1 An element object
Expand Down Expand Up @@ -864,6 +876,15 @@
return(e1)
}

if (inherits(e1, "margin") && inherits(e2, "margin")) {
if (anyNA(e2)) {
e2[is.na(e2)] <- unit(0, "pt")

Check warning on line 881 in R/theme.R

View check run for this annotation

Codecov / codecov/patch

R/theme.R#L881

Added line #L881 was not covered by tests
}
if (anyNA(e1)) {
e1[is.na(e1)] <- e2[is.na(e1)]
}
}

# If neither of e1 or e2 are element_* objects, return e1
if (!inherits(e1, "element") && !inherits(e2, "element")) {
return(e1)
Expand Down Expand Up @@ -893,6 +914,10 @@
e1$linewidth <- e2$linewidth * unclass(e1$linewidth)
}

if (inherits(e1, "element_text")) {
e1$margin <- combine_elements(e1$margin, e2$margin)
}

# If e2 is 'richer' than e1, fill e2 with e1 parameters
if (is.subclass(e2, e1)) {
new <- defaults(e1, e2)
Expand Down
5 changes: 4 additions & 1 deletion man/element.Rd

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

3 changes: 3 additions & 0 deletions man/merge_element.Rd

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

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

test_that("margin_part() mechanics work as expected", {

t <- theme_gray() +
theme(plot.margin = margin_part(b = 11))

test <- calc_element("plot.margin", t)
expect_equal(as.numeric(test), c(5.5, 5.5, 11, 5.5))

t <- theme_gray() +
theme(margins = margin_part(b = 11))

test <- calc_element("plot.margin", t)
expect_equal(as.numeric(test), c(5.5, 5.5, 11, 5.5))

})

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

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