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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,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 @@ -492,6 +493,8 @@ export(layer_sf)
export(lims)
export(map_data)
export(margin)
export(margin_auto)
export(margin_part)
export(max_height)
export(max_width)
export(mean_cl_boot)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 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, #6115)
* New `margin_auto()` specification for theme margins.
* (internal) Using `after_scale()` in the `Geom*$default_aes()` field is now
evaluated in the context of data (@teunbrand, #6135)
* Fixed bug where binned scales wouldn't simultaneously accept transformations
Expand Down
2 changes: 1 addition & 1 deletion R/geom-rug.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' p +
#' geom_rug(outside = TRUE, sides = "tr") +
#' coord_cartesian(clip = "off") +
#' theme(plot.margin = margin(1, 1, 1, 1, "cm"))
#' theme(plot.margin = margin_auto(1, unit = "cm"))
#'
#' # increase the line length and
#' # expand axis to avoid overplotting
Expand Down
2 changes: 1 addition & 1 deletion R/legend-draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
fontface = data$fontface %||% 1,
fontsize = (data$size %||% 3.88) * .pt
),
margin = margin(0.1, 0.1, 0.1, 0.1, unit = "lines"),
margin = margin_auto(0.1, unit = "lines"),

Check warning on line 280 in R/legend-draw.R

View check run for this annotation

Codecov / codecov/patch

R/legend-draw.R#L280

Added line #L280 was not covered by tests
margin_x = TRUE, margin_y = TRUE
)
attr(grob, "width") <- convertWidth(grobWidth(grob), "cm", valueOnly = TRUE)
Expand Down
12 changes: 12 additions & 0 deletions R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ margin <- function(t = 0, r = 0, b = 0, l = 0, unit = "pt") {
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)
}

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

#' @export
#' @rdname is_tests
is.margin <- function(x) inherits(x, "margin")
Expand Down
20 changes: 10 additions & 10 deletions R/theme-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ theme_grey <- function(base_size = 11, base_family = "",
title = element_text(family = header_family),

spacing = unit(half_line, "pt"),
margins = margin(half_line, half_line, half_line, half_line),
margins = margin_auto(half_line),

geom = element_geom(
ink = ink, paper = paper, accent = "#3366FF",
Expand Down Expand Up @@ -226,7 +226,7 @@ theme_grey <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = col_mix(ink, paper, 0.105),
size = rel(0.8),
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
margin = margin_auto(0.8 * half_line)
),
strip.text.x = NULL,
strip.text.y = element_text(angle = -90),
Expand Down Expand Up @@ -341,7 +341,7 @@ theme_linedraw <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = paper,
size = rel(0.8),
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
margin = margin_auto(0.8 * half_line)
),

complete = TRUE
Expand Down Expand Up @@ -384,7 +384,7 @@ theme_light <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = paper,
size = rel(0.8),
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
margin = margin_auto(0.8 * half_line)
),

complete = TRUE
Expand Down Expand Up @@ -427,7 +427,7 @@ theme_dark <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = col_mix(ink, paper, 0.9),
size = rel(0.8),
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
margin = margin_auto(0.8 * half_line)
),

complete = TRUE
Expand Down Expand Up @@ -518,7 +518,7 @@ theme_void <- function(base_size = 11, base_family = "",
),
title = element_text(family = header_family),
spacing = unit(half_line, "pt"),
margins = margin(half_line, half_line, half_line, half_line),
margins = margin_auto(half_line),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks.length = rel(0),
Expand Down Expand Up @@ -602,7 +602,7 @@ theme_test <- function(base_size = 11, base_family = "",
),
title = element_text(family = header_family),
spacing = unit(half_line, "pt"),
margins = margin(half_line, half_line, half_line, half_line),
margins = margin_auto(half_line),
geom = element_geom(
ink = ink, paper = paper, accent = "#3366FF",
linewidth = base_line_size, borderwidth = base_line_size,
Expand Down Expand Up @@ -651,7 +651,7 @@ theme_test <- function(base_size = 11, base_family = "",
legend.spacing = rel(2),
legend.spacing.x = NULL,
legend.spacing.y = NULL,
legend.margin = margin(0, 0, 0, 0, "cm"),
legend.margin = margin_auto(0, unit = "cm"),
legend.key = NULL,
legend.key.size = unit(1.2, "lines"),
legend.key.height = NULL,
Expand All @@ -666,7 +666,7 @@ theme_test <- function(base_size = 11, base_family = "",
legend.direction = NULL,
legend.justification = "center",
legend.box = NULL,
legend.box.margin = margin(0, 0, 0, 0, "cm"),
legend.box.margin = margin_auto(0, unit = "cm"),
legend.box.background = element_blank(),
legend.box.spacing = rel(2),

Expand All @@ -687,7 +687,7 @@ theme_test <- function(base_size = 11, base_family = "",
strip.text = element_text(
colour = col_mix(ink, paper, 0.105),
size = rel(0.8),
margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
margin = margin_auto(0.8 * half_line)
),
strip.text.x = NULL,
strip.text.y = element_text(angle = -90),
Expand Down
5 changes: 3 additions & 2 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#' - `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()`, `margin_part()` and `margin_auto()` are all 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 Expand Up @@ -42,7 +43,7 @@
#'
#' plot + theme(
#' panel.background = element_rect(fill = "white"),
#' plot.margin = margin(2, 2, 2, 2, "cm"),
#' plot.margin = margin_auto(2, unit = "cm"),
#' plot.background = element_rect(
#' fill = "grey90",
#' colour = "black",
Expand Down
29 changes: 27 additions & 2 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@
#' legend.position.inside = c(.95, .95),
#' legend.justification = c("right", "top"),
#' legend.box.just = "right",
#' legend.margin = margin(6, 6, 6, 6)
#' legend.margin = margin_auto(6)
#' )
#'
#' # The legend.box properties work similarly for the space around
#' # all the legends
#' p2 + theme(
#' legend.box.background = element_rect(),
#' legend.box.margin = margin(6, 6, 6, 6)
#' legend.box.margin = margin_auto(6)
#' )
#'
#' # You can also control the display of the keys
Expand Down Expand Up @@ -835,6 +835,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 @@ -868,6 +880,15 @@
return(e1)
}

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

Check warning on line 885 in R/theme.R

View check run for this annotation

Codecov / codecov/patch

R/theme.R#L885

Added line #L885 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 @@ -897,6 +918,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
is_subclass <- !any(inherits(e2, class(e1), which = TRUE) == 0)
is_subclass <- is_subclass && length(setdiff(class(e2), class(e1)) > 0)
Expand Down
11 changes: 9 additions & 2 deletions man/element.Rd

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

2 changes: 1 addition & 1 deletion man/geom_rug.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.

4 changes: 2 additions & 2 deletions man/theme.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 @@ -613,6 +613,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