Skip to content

Commit

Permalink
Fix for position = "inside" (#51)
Browse files Browse the repository at this point in the history
* customise `check_position()`

* resolve `position = "inside"` as right-positioned

* include inside where appropriate

* add news bullet
  • Loading branch information
teunbrand authored Feb 15, 2025
1 parent 3dc2b3a commit 073ae43
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# legendry (development version)

* Fixed bug hindering `position = "inside"` placement (#42)

# legendry 0.2.0

This is a small feature release introducing dendrogram scales and a size guide.
Expand Down
2 changes: 1 addition & 1 deletion R/compose-.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Compose <- ggproto(
title <- scale$make_title(params$title %|W|% scale$name %|W|% title)
position <- params$position <- params$position %|W|% NULL
aesthetic <- params$aesthetic <- aesthetic %||% scale$aesthetics[1]
check_position(position, allow_null = TRUE)
check_position(position, inside = TRUE, allow_null = TRUE)

key <- resolve_key(params$key, allow_null = TRUE)
if (is.function(key)) {
Expand Down
7 changes: 4 additions & 3 deletions R/compose-crux.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ ComposeCrux <- ggproto(
elements$text_position <- elements$text_position %||%
switch(params$direction, horizontal = "bottom", vertical = "right")

check_position(elements$title_position, .trbl, arg = "legend.title.position")
check_position(elements$title_position, theta = FALSE, arg = "legend.title.position")
elements
},

Expand All @@ -139,9 +139,10 @@ ComposeCrux <- ggproto(
return(zeroGrob())
}

position <- params$position <- params$position %||% position
direction <- params$direction <- params$direction %||% direction
check_position(position, .trbl)
position <- params$position <- params$position %||% position
position <- switch(position, inside = "right", position)
check_position(position)
check_argmatch(direction, c("horizontal", "vertical"))

theme <- theme + params$theme
Expand Down
1 change: 1 addition & 0 deletions R/compose-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ ComposeStack <- ggproto(

theme <- theme + params$theme
position <- params$position <- params$position %||% position
position <- switch(position, inside = "right", position)
check_position(position)
params$guide_params <-
set_list_element(params$guide_params, "position", position)
Expand Down
5 changes: 3 additions & 2 deletions R/guide-colring.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ GuideColring <- ggproto(
override_elements = function(params, elements, theme) {
elements$title_position <- elements$title_position %||%
switch(params$direction, horizontal = "left", vertical = "top")
check_position(elements$title_position, .trbl, arg = "legend.title.position")
check_position(elements$title_position, theta = FALSE, arg = "legend.title.position")
elements$width <- cm(elements$width)
elements$size <- cm(elements$size) * 5
elements$margin <- elements$margin %||% margin()
Expand All @@ -252,7 +252,8 @@ GuideColring <- ggproto(

position <- params$position <- params$position %||% position
direction <- params$direction <- params$direction %||% direction
check_position(position, .trbl)
position <- switch(position, inside = "right", position)
check_position(position, theta = FALSE)
check_argmatch(direction, c("horizontal", "vertical"))

theme <- theme + params$theme
Expand Down
2 changes: 1 addition & 1 deletion R/guide-legend-base.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ guide_legend_base <- function(
order = 0
) {

check_position(position, allow_null = TRUE)
check_position(position, theta = FALSE, inside = TRUE, allow_null = TRUE)
check_argmatch(direction, c("horizontal", "vertical"), allow_null = TRUE)
check_bool(reverse)
check_number_whole(nrow, min = 1, allow_null = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/guide-legend-cross.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ guide_legend_cross <- function(
order = 0
) {

check_position(position, allow_null = TRUE)
check_position(position, theta = FALSE, inside = TRUE, allow_null = TRUE)
check_argmatch(direction, c("horizontal", "vertical"), allow_null = TRUE)
check_bool(swap)

Expand Down
2 changes: 1 addition & 1 deletion R/guide-legend-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ guide_legend_group <- function(
order = 0
) {

check_position(position, allow_null = TRUE)
check_position(position, theta = FALSE, inside = TRUE, allow_null = TRUE)
check_argmatch(direction, c("horizontal", "vertical"), allow_null = TRUE)
check_number_whole(nrow, min = 1, allow_null = TRUE)
check_number_whole(ncol, min = 1, allow_null = TRUE)
Expand Down
33 changes: 28 additions & 5 deletions R/utils-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,34 @@ check_argmatch <- function(
)
}

check_position <- new_function(
`[[<-`(fn_fmls(check_argmatch), "options", .trblt),
body(check_argmatch),
fn_env(check_argmatch)
)
check_position <- function(
x, options = .trbl, theta = TRUE, inside = FALSE,
...,
allow_null = FALSE,
arg = caller_arg(x), call = caller_env()
) {
if (!missing(x)) {
if (allow_null && is_null(x)) {
return(invisible(NULL))
}
if (is.character(x)) {
if (theta) {
options <- c(options, "theta", "theta.sec")
}
if (inside) {
options <- c(options, "inside")
}
arg_match0(x, options, arg_nm = arg, error_call = call)
return(invisible(NULL))
}
}

stop_input_type(
x, "a single string", ...,
allow_na = FALSE, allow_null = allow_null,
arg = arg, call = call
)
}

check_unique <- function(x, arg = caller_arg(x), call = caller_env()) {
if (!vec_duplicate_any(x)) {
Expand Down

0 comments on commit 073ae43

Please sign in to comment.