Skip to content

Refactor guide styling #5554

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 28 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7f4a6a8
add more legend theme settings
teunbrand Nov 29, 2023
6751a66
remove elements as part of guide construction
teunbrand Nov 30, 2023
36136a3
add `theme` as required guide parameter
teunbrand Nov 30, 2023
3425f5b
bequeath axes with `theme` argument
teunbrand Nov 30, 2023
4f37180
wire `theme` into legends
teunbrand Nov 30, 2023
bf70ff9
impart `theme` upon colourbars
teunbrand Nov 30, 2023
de32582
entrust bins guide with `theme`
teunbrand Nov 30, 2023
854bca7
replacement utility
teunbrand Nov 30, 2023
21a000b
redocument
teunbrand Dec 1, 2023
72bcd1c
small tweaks
teunbrand Dec 1, 2023
3783e2e
backward compatibility mechanism
teunbrand Dec 1, 2023
9f8c0a7
add news bullet
teunbrand Dec 1, 2023
f2b872a
update tests
teunbrand Dec 1, 2023
d35f5be
Separate colourbar/coloursteps constructors
teunbrand Dec 1, 2023
ec87aa3
themes have default `legend.key.spacing`
teunbrand Dec 1, 2023
064bdcd
doc fixes
teunbrand Dec 1, 2023
014641d
resolve merge conflict
teunbrand Dec 1, 2023
86efab5
Merge branch 'main' into guide_themes
teunbrand Dec 7, 2023
2d9e7fc
add `theme` to stacked axis
teunbrand Dec 7, 2023
33897ae
There is no need for `justify_grobs()`
teunbrand Dec 7, 2023
8470aa2
resolve merge conflict
teunbrand Dec 8, 2023
4ee4cfb
update `replace_null()`
teunbrand Dec 8, 2023
6530b3c
fix examples
teunbrand Dec 8, 2023
9b2ae17
resolve merge conflict
teunbrand Dec 11, 2023
a14493b
adapt to latest changes
teunbrand Dec 11, 2023
b16a1bb
rename argument
teunbrand Dec 11, 2023
dad2815
Fix typo
teunbrand Dec 11, 2023
23b74f2
no need to use `justify_grobs()`
teunbrand Dec 11, 2023
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
18 changes: 18 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
`legend.title.position`. Previous style arguments in the `guide_*()` functions
have been soft-deprecated.

* `draw_key_label()` now better reflects the appearance of labels.

* The `minor_breaks` function argument in scales can now take a function with
two arguments: the scale's limits and the scale's major breaks (#3583).

* (internal) The `ScaleContinuous$get_breaks()` method no longer censors
the computed breaks.

* Plot scales now ignore `AsIs` objects constructed with `I(x)`, instead of
invoking the identity scale. This allows these columns to co-exist with other
layers that need a non-identity scale for the same aesthetic. Also, it makes
it easy to specify relative positions (@teunbrand, #5142).

* The `fill` aesthetic in many geoms now accepts grid's patterns and gradients.
For developers of layer extensions, this feature can be enabled by switching
from `fill = alpha(fill, alpha)` to `fill = fill_alpha(fill, alpha)` when
providing fills to `grid::gpar()` (@teunbrand, #3997).

* The plot's title, subtitle and caption now obey horizontal text margins
(#5533).

Expand Down
3 changes: 1 addition & 2 deletions R/guide-.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ Guide <- ggproto(
key <- params$key

# Setup parameters and theme
params$position <- params$position %||% position
params$direction <- params$direction %||% direction
params <- replace_null(params, position = position, direction = direction)
params <- self$setup_params(params)
elems <- self$setup_elements(params, self$elements, theme)
elems <- self$override_elements(params, elems, theme)
Expand Down
13 changes: 9 additions & 4 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ NULL
#' p
#'
#' # Remove the axis or style it
#' p + guides(size = guide_bins(theme = theme(legend.axis = element_blank())))
#' p + guides(size = guide_bins(
#' theme = theme(legend.axis.line = element_blank())
#' ))
#'
#' p + guides(size = guide_bins(show.limits = TRUE))
#'
#' my_arrow <- arrow(length = unit(1.5, "mm"), ends = "both")
#' p + guides(size = guide_bins(theme = theme(
#' legend.axis = element_line(arrow = my_arrow)
#' )))
#' p + guides(size = guide_bins(
#' theme = theme(legend.axis.line = element_line(arrow = my_arrow))
#' ))
#'
#' # Guides are merged together if possible
#' ggplot(mtcars) +
Expand All @@ -73,6 +75,9 @@ guide_bins <- function(
) {

theme <- deprecated_guide_args(theme, ...)
if (!is.null(position)) {
position <- arg_match0(position, c(.trbl, "inside"))
}

new_guide(
# title
Expand Down
5 changes: 5 additions & 0 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ guide_colourbar <- function(
raster = TRUE,
draw.ulim = TRUE,
draw.llim = TRUE,
position = NULL,
direction = NULL,
reverse = FALSE,
order = 0,
Expand All @@ -117,13 +118,17 @@ guide_colourbar <- function(
) {

theme <- deprecated_guide_args(theme, ...)
if (!is.null(position)) {
position <- arg_match0(position, c(.trbl, "inside"))
}

new_guide(
title = title,
theme = theme,
nbin = nbin,
raster = raster,
draw_lim = c(isTRUE(draw.llim), isTRUE(draw.ulim)),
position = position,
direction = direction,
reverse = reverse,
order = order,
Expand Down
6 changes: 6 additions & 0 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#' @param theme A [`theme`][theme()] object to style the guide individually or
#' differently from the plot's theme settings. The `theme` argument in the
#' guide override,s and is combined with, the plot's theme.
#' @param position A character string indicating where the legend should be
#' placed relative to the plot panels.
#' @param direction A character string indicating the direction of the guide.
#' One of "horizontal" or "vertical."
#' @param override.aes A list specifying aesthetic parameters of legend key.
Expand Down Expand Up @@ -111,6 +113,10 @@ guide_legend <- function(

theme <- deprecated_guide_args(theme, ...)

if (!is.null(position)) {
position <- arg_match0(position, c(.trbl, "inside"))
}

new_guide(
# Title
title = title,
Expand Down
6 changes: 4 additions & 2 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,11 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
legend.text.position = el_def("character"),
legend.title = el_def("element_text", "title"),
legend.title.position = el_def("character"),
legend.position = el_def(c("character", "numeric", "integer")),
legend.direction = el_def("character"),
legend.byrow = el_def("logical"),
legend.position = el_def("character"),
legend.position.inside = el_def(c("numeric", "integer")),
legend.direction = el_def("character"),

legend.justification = el_def(c("character", "numeric", "integer")),
legend.justification.top = el_def(
c("character", "numeric", "integer"),
Expand Down
6 changes: 4 additions & 2 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
#' `title`)
#' @param legend.title.position placement of legend title relative to the main
#' legend ("top", "right", "bottom" or "left").
#' @param legend.position the position of legends ("none", "left", "right",
#' "bottom", "top", or two-element numeric vector)
#' @param legend.position the default position of legends ("none", "left",
#' "right", "bottom", "top", "inside")
#' @param legend.position.inside A numeric vector of length two setting the
#' placement of legends that have the `"inside"` position.
#' @param legend.direction layout of items in legends ("horizontal" or
#' "vertical")
#' @param legend.byrow whether the legend-matrix is filled by columns
Expand Down
10 changes: 9 additions & 1 deletion R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,17 @@ vec_rbind0 <- function(..., .error_call = current_env(), .call = caller_env()) {
)
}

# This function is used to vectorise the following pattern:
#
# list$name1 <- list$name1 %||% value
# list$name2 <- list$name2 %||% value
#
# and express this pattern as:
#
# replace_null(list, name1 = value, name2 = value)
replace_null <- function(list, ..., env = caller_env()) {
# Collect dots without evaluating
dots <- match.call(replace_null, expand.dots = FALSE)$`...`
dots <- enexprs(...)
# Select arguments that are null in `list`
nms <- names(dots)
nms <- nms[vapply(list[nms], is.null, logical(1))]
Expand Down
5 changes: 5 additions & 0 deletions man/guide_axis_stack.Rd

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

11 changes: 7 additions & 4 deletions man/guide_bins.Rd

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

1 change: 1 addition & 0 deletions man/guide_legend.Rd

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

7 changes: 5 additions & 2 deletions man/theme.Rd

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