Skip to content
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
4 changes: 2 additions & 2 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' Arguments that apply to a single legend are respected, most of which have
#' the `legend`-prefix. Arguments that apply to combined legends
#' (the legend box) are ignored, including `legend.position`,
#' `legend.justification.*`, `legend.location` and `legend.box.*`.
#' `legend.just.*`, `legend.location` and `legend.box.*`.
#' @param position A character string indicating where the legend should be
#' placed relative to the plot panels.
#' One of "top", "right", "bottom", "left", or "inside".
Expand Down Expand Up @@ -179,7 +179,7 @@ GuideLegend <- ggproto(
key = "legend.key",
key_height = "legend.key.height",
key_width = "legend.key.width",
key_just = "legend.key.justification",
key_just = "legend.key.just",
text = "legend.text",
theme.title = "legend.title",
spacing_x = "legend.key.spacing.x",
Expand Down
10 changes: 5 additions & 5 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ Guides <- ggproto(
}

# prepare the position of inside legends
default_inside_just <- calc_element("legend.justification.inside", theme)
default_inside_just <- calc_element("legend.just.inside", theme)
default_inside_position <- calc_element("legend.position.inside", theme)

groups <- data_frame0(
Expand All @@ -514,9 +514,9 @@ Guides <- ggproto(
for (i in which(positions == "inside")) {
# the actual inside position and justification can be set in each guide
# by `theme` argument, here, we won't use `calc_element()` which will
# use inherits from `legend.justification` or `legend.position`, we only
# use inherits from `legend.just` or `legend.position`, we only
# follow the inside elements from the guide theme
just <- params[[i]]$theme[["legend.justification.inside"]]
just <- params[[i]]$theme[["legend.just.inside"]]
just <- valid.just(just %||% default_inside_just)
coord <- params[[i]]$theme[["legend.position.inside"]]
coord <- coord %||% default_inside_position %||% just
Expand All @@ -541,7 +541,7 @@ Guides <- ggproto(
if (position == "inside") {
adjust <- theme(
legend.position.inside = groups$key$coords[[i]],
legend.justification.inside = groups$key$justs[[i]]
legend.just.inside = groups$key$justs[[i]]
)
}
adjust <- add_theme(theme, adjust, "internal theme settings")
Expand Down Expand Up @@ -615,7 +615,7 @@ Guides <- ggproto(
stretch_y <- any(unlist(lapply(heights, unitType)) == "null")

# Global justification of the complete legend box
global_just <- paste0("legend.justification.", position)
global_just <- paste0("legend.just.", position)
global_just <- valid.just(calc_element(global_just, theme))

if (position == "inside") {
Expand Down
4 changes: 2 additions & 2 deletions R/theme-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ theme_grey <- function(base_size = 11, base_family = "",
legend.ticks.length = rel(0.2),
legend.position = "right",
legend.direction = NULL,
legend.justification = "center",
legend.just = "center",
legend.box = NULL,
legend.box.margin = margin_auto(0),
legend.box.background = element_blank(),
Expand Down Expand Up @@ -707,7 +707,7 @@ theme_test <- function(base_size = 11, base_family = "",
legend.ticks.length = rel(0.2),
legend.position = "right",
legend.direction = NULL,
legend.justification = "center",
legend.just = "center",
legend.box = NULL,
legend.box.margin = margin_auto(0, unit = "cm"),
legend.box.background = element_blank(),
Expand Down
24 changes: 12 additions & 12 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
legend.key.spacing = el_def(c("unit", "rel"), "spacing"),
legend.key.spacing.x = el_def(c("unit", "rel"), "legend.key.spacing"),
legend.key.spacing.y = el_def(c("unit", "rel"), "legend.key.spacing"),
legend.key.justification = el_def(c("character", "numeric", "integer")),
legend.key.just = el_def(c("character", "numeric", "integer")),
legend.frame = el_def(element_rect, "rect"),
legend.axis.line = el_def(element_line, "line"),
legend.ticks = el_def(element_line, "legend.axis.line"),
Expand All @@ -918,26 +918,26 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
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(
legend.just = el_def(c("character", "numeric", "integer")),
legend.just.top = el_def(
c("character", "numeric", "integer"),
"legend.justification"
"legend.just"
),
legend.justification.bottom = el_def(
legend.just.bottom = el_def(
c("character", "numeric", "integer"),
"legend.justification"
"legend.just"
),
legend.justification.left = el_def(
legend.just.left = el_def(
c("character", "numeric", "integer"),
"legend.justification"
"legend.just"
),
legend.justification.right = el_def(
legend.just.right = el_def(
c("character", "numeric", "integer"),
"legend.justification"
"legend.just"
),
legend.justification.inside = el_def(
legend.just.inside = el_def(
c("character", "numeric", "integer"),
"legend.justification"
"legend.just"
),

legend.location = el_def("character"),
Expand Down
2 changes: 1 addition & 1 deletion R/theme-sub.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ theme_sub_legend <- function(
spacing, spacing.x, spacing.y, margin,
# Seys
key, key.size, key.height, key.width, key.spacing, key.spacing.x,
key.spacing.y, key.justification,
key.spacing.y, key.just,
# Settings
byrow, position, direction, location, position.inside,
# Justification
Expand Down
43 changes: 29 additions & 14 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
#' between legend keys given as a `unit`. Spacing in the horizontal (x) and
#' vertical (y) direction inherit from `legend.key.spacing` or can be
#' specified separately. `legend.key.spacing` inherits from `spacing`.
#' @param legend.key.justification Justification for positioning legend keys
#' @param legend.key.just Justification for positioning legend keys
#' when more space is available than needed for display. The default, `NULL`,
#' stretches keys into the available space. Can be a location like `"center"`
#' or `"top"`, or a two-element numeric vector.
Expand All @@ -112,11 +112,11 @@
#' "vertical")
#' @param legend.byrow whether the legend-matrix is filled by columns
#' (`FALSE`, the default) or by rows (`TRUE`).
#' @param legend.justification anchor point for positioning legend inside plot
#' @param legend.just anchor point for positioning legend inside plot
#' ("center" or two-element numeric vector) or the justification according to
#' the plot area when positioned outside the plot
#' @param legend.justification.top,legend.justification.bottom,legend.justification.left,legend.justification.right,legend.justification.inside
#' Same as `legend.justification` but specified per `legend.position` option.
#' @param legend.just.top,legend.just.bottom,legend.just.left,legend.just.right,legend.just.inside
#' Same as `legend.just` but specified per `legend.position` option.
#' @param legend.location Relative placement of legends outside the plot as a
#' string. Can be `"panel"` (default) to align legends to the panels or
#' `"plot"` to align legends to the plot as a whole.
Expand Down Expand Up @@ -280,15 +280,15 @@
#'
#' # Position
#' p2 + theme(legend.position = "none")
#' p2 + theme(legend.justification = "top")
#' p2 + theme(legend.just = "top")
#' p2 + theme(legend.position = "bottom")
#'
#' # Or place legends inside the plot using relative coordinates between 0 and 1
#' # legend.justification sets the corner that the position refers to
#' # legend.just sets the corner that the position refers to
#' p2 + theme(
#' legend.position = "inside",
#' legend.position.inside = c(.95, .95),
#' legend.justification = c("right", "top"),
#' legend.just = c("right", "top"),
#' legend.box.just = "right",
#' legend.margin = margin_auto(6)
#' )
Expand Down Expand Up @@ -416,7 +416,7 @@ theme <- function(...,
legend.key.spacing,
legend.key.spacing.x,
legend.key.spacing.y,
legend.key.justification,
legend.key.just,
legend.frame,
legend.ticks,
legend.ticks.length,
Expand All @@ -429,12 +429,12 @@ theme <- function(...,
legend.position.inside,
legend.direction,
legend.byrow,
legend.justification,
legend.justification.top,
legend.justification.bottom,
legend.justification.left,
legend.justification.right,
legend.justification.inside,
legend.just,
legend.just.top,
legend.just.bottom,
legend.just.left,
legend.just.right,
legend.just.inside,
legend.location,
legend.box,
legend.box.just,
Expand Down Expand Up @@ -545,6 +545,21 @@ fix_theme_deprecations <- function(elements) {
elements$legend.position.inside <- elements$legend.position
elements$legend.position <- "inside"
}
just_settings <- c(
"legend.key.just",
paste0("legend.just", c("", ".top", ".bottom", ".left", ".right", ".inside"))
)
justification_settings <- gsub("\\.just", "\\.justification", just_settings)
for (i in which(justification_settings %in% names(elements))) {
deprecate_warn0(
"4.0.1", # TODO: double check intended version before merging
paste0("theme(", justification_settings[i], ")"),
paste0("theme(", just_settings[i], ")")
)
elements[[just_settings[i]]] <- elements[[justification_settings[i]]]
elements[[justification_settings[i]]] <- NULL
}

elements
}

Expand Down
2 changes: 1 addition & 1 deletion man/guide_bins.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/guide_colourbar.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/guide_coloursteps.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/guide_custom.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/guide_legend.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/subtheme.Rd

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

26 changes: 13 additions & 13 deletions man/theme.Rd

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

7 changes: 5 additions & 2 deletions tests/testthat/_snaps/theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
[17] "axis.minor.ticks.r" "axis.ticks.length.theta"
[19] "axis.ticks.length.r" "axis.minor.ticks.length.theta"
[21] "axis.minor.ticks.length.r" "axis.line.theta"
[23] "axis.line.r" "complete"
[25] "validate"
[23] "axis.line.r" "legend.just"
[25] "legend.just.top" "legend.just.bottom"
[27] "legend.just.left" "legend.just.right"
[29] "legend.just.inside" "complete"
[31] "validate"

6 changes: 3 additions & 3 deletions tests/testthat/test-guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test_that("absent titles don't take up space", {
legend.title = element_blank(),
legend.margin = margin(),
legend.position = "top",
legend.justification = "left",
legend.just = "left",
legend.key = element_rect(colour = "black"),
axis.line = element_line(colour = "black")
)
Expand Down Expand Up @@ -221,7 +221,7 @@ test_that("legend.byrow works in `guide_legend()`", {
expect_doppelganger("legend.byrow = TRUE", p)
})

test_that("legend.key.justification works as intended", {
test_that("legend.key.just works as intended", {

p <- ggplot(mtcars, aes(mpg, disp, colour = factor(cyl), size = drat)) +
geom_point() +
Expand All @@ -231,7 +231,7 @@ test_that("legend.key.justification works as intended", {
scale_colour_discrete(
labels = c("one line", "up\nto\nfour\nlines", "up\nto\nfive\nwhole\nlines")
) +
theme(legend.key.justification = c(1, 0))
theme(legend.key.just = c(1, 0))

expect_doppelganger("legend key justification", p)

Expand Down
Loading
Loading