Skip to content

Adapt to scales 1.4.0 #6441

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 5 commits into from
Apr 25, 2025
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Imports:
isoband,
lifecycle (> 1.0.1),
rlang (>= 1.1.0),
scales (>= 1.3.0),
scales (>= 1.4.0),
stats,
vctrs (>= 0.6.0),
withr (>= 2.5.0)
Expand Down
2 changes: 1 addition & 1 deletion R/geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ GeomSf <- ggproto("GeomSf", Geom,
other_default <- modify_list(
GeomPolygon$default_aes,
aes(
fill = from_theme(fill %||% col_mix(ink, paper, 0.9)),
fill = from_theme(fill %||% col_mix(ink, paper, 0.899)),
colour = from_theme(colour %||% col_mix(ink, paper, 0.35)),
linewidth = from_theme(0.4 * borderwidth)
)
Expand Down
26 changes: 13 additions & 13 deletions R/theme-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ theme_grey <- function(base_size = 11, base_family = "",
axis.line = element_blank(),
axis.line.x = NULL,
axis.line.y = NULL,
axis.text = element_text(size = rel(0.8), colour = col_mix(ink, paper, 0.305)),
axis.text = element_text(size = rel(0.8), colour = col_mix(ink, paper, 0.302)),
axis.text.x = element_text(margin = margin(t = 0.8 * half_line / 2), vjust = 1),
axis.text.x.top = element_text(margin = margin(b = 0.8 * half_line / 2), vjust = 0),
axis.text.y = element_text(margin = margin(r = 0.8 * half_line / 2), hjust = 1),
Expand Down Expand Up @@ -223,7 +223,7 @@ theme_grey <- function(base_size = 11, base_family = "",
legend.box.background = element_blank(),
legend.box.spacing = rel(2),

panel.background = element_rect(fill = col_mix(ink, paper, 0.925), colour = NA),
panel.background = element_rect(fill = col_mix(ink, paper, 0.92), colour = NA),
panel.border = element_blank(),
panel.grid = element_line(colour = paper),
panel.grid.minor = element_line(linewidth = rel(0.5)),
Expand All @@ -232,10 +232,10 @@ theme_grey <- function(base_size = 11, base_family = "",
panel.spacing.y = NULL,
panel.ontop = FALSE,

strip.background = element_rect(fill = col_mix(ink, paper, 0.854), colour = NA),
strip.background = element_rect(fill = col_mix(ink, paper, 0.85), colour = NA),
strip.clip = "on",
strip.text = element_text(
colour = col_mix(ink, paper, 0.105),
colour = col_mix(ink, paper, 0.1),
size = rel(0.8),
margin = margin_auto(0.8 * half_line)
),
Expand Down Expand Up @@ -303,7 +303,7 @@ theme_bw <- function(base_size = 11, base_family = "",
panel.background = element_rect(fill = paper, colour = NA),
panel.border = element_rect(colour = col_mix(ink, paper, 0.2)),
# make gridlines dark, same contrast with white as in theme_grey
panel.grid = element_line(colour = col_mix(ink, paper, 0.925)),
panel.grid = element_line(colour = col_mix(ink, paper, 0.92)),
panel.grid.minor = element_line(linewidth = rel(0.5)),
# contour strips to match panel contour
strip.background = element_rect(
Expand Down Expand Up @@ -380,18 +380,18 @@ theme_light <- function(base_size = 11, base_family = "",
theme(
# white panel with light grey border
panel.background = element_rect(fill = paper, colour = NA),
panel.border = element_rect(colour = col_mix(ink, paper, 0.705), linewidth = rel(1)),
panel.border = element_rect(colour = col_mix(ink, paper, 0.702), linewidth = rel(1)),
# light grey, thinner gridlines
# => make them slightly darker to keep acceptable contrast
panel.grid = element_line(colour = col_mix(ink, paper, 0.871)),
panel.grid.major = element_line(linewidth = rel(0.5)),
panel.grid.minor = element_line(linewidth = rel(0.25)),

# match axes ticks thickness to gridlines and colour to panel border
axis.ticks = element_line(colour = col_mix(ink, paper, 0.705), linewidth = rel(0.5)),
axis.ticks = element_line(colour = col_mix(ink, paper, 0.702), linewidth = rel(0.5)),

# dark strips with light text (inverse contrast compared to theme_grey)
strip.background = element_rect(fill = col_mix(ink, paper, 0.705), colour = NA),
strip.background = element_rect(fill = col_mix(ink, paper, 0.702), colour = NA),
strip.text = element_text(
colour = paper,
size = rel(0.8),
Expand Down Expand Up @@ -423,7 +423,7 @@ theme_dark <- function(base_size = 11, base_family = "",
) %+replace%
theme(
# dark panel
panel.background = element_rect(fill = col_mix(ink, paper, 0.5), colour = NA),
panel.background = element_rect(fill = col_mix(ink, paper, 0.499), colour = NA),
# inverse grid lines contrast compared to theme_grey
# make them thinner and try to keep the same visual contrast as in theme_light
panel.grid = element_line(colour = col_mix(ink, paper, 0.42)),
Expand All @@ -436,7 +436,7 @@ theme_dark <- function(base_size = 11, base_family = "",
# dark strips with light text (inverse contrast compared to theme_grey)
strip.background = element_rect(fill = col_mix(ink, paper, 0.15), colour = NA),
strip.text = element_text(
colour = col_mix(ink, paper, 0.9),
colour = col_mix(ink, paper, 0.899),
size = rel(0.8),
margin = margin_auto(0.8 * half_line)
),
Expand Down Expand Up @@ -649,7 +649,7 @@ theme_test <- function(base_size = 11, base_family = "",
axis.line = element_blank(),
axis.line.x = NULL,
axis.line.y = NULL,
axis.text = element_text(size = rel(0.8), colour = col_mix(ink, paper, 0.305)),
axis.text = element_text(size = rel(0.8), colour = col_mix(ink, paper, 0.302)),
axis.text.x = element_text(margin = margin(t = 0.8 * half_line / 2), vjust = 1),
axis.text.x.top = element_text(margin = margin(b = 0.8 * half_line / 2), vjust = 0),
axis.text.y = element_text(margin = margin(r = 0.8 * half_line / 2), hjust = 1),
Expand Down Expand Up @@ -715,12 +715,12 @@ theme_test <- function(base_size = 11, base_family = "",
panel.ontop = FALSE,

strip.background = element_rect(
fill = col_mix(ink, paper, 0.851),
fill = col_mix(ink, paper, 0.85),
colour = col_mix(ink, paper, 0.2)
),
strip.clip = "on",
strip.text = element_text(
colour = col_mix(ink, paper, 0.105),
colour = col_mix(ink, paper, 0.1),
size = rel(0.8),
margin = margin_auto(0.8 * half_line)
),
Expand Down
50 changes: 0 additions & 50 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -852,56 +852,6 @@ warn_dots_used <- function(env = caller_env(), call = caller_env()) {
)
}

# TODO: delete shims when {scales} releases >1.3.0.9000
# and bump {scales} version requirements
# Shim for scales/#424
col_mix <- function(a, b, amount = 0.5) {
input <- vec_recycle_common(a = a, b = b, amount = amount)
a <- grDevices::col2rgb(input$a, TRUE)
b <- grDevices::col2rgb(input$b, TRUE)
new <- (a * (1 - input$amount) + b * input$amount)
grDevices::rgb(
new["red", ], new["green", ], new["blue", ],
alpha = new["alpha", ], maxColorValue = 255
)
}

# Shim for scales/#427
as_discrete_pal <- function(x, ...) {
if (is.function(x)) {
return(x)
}
pal_manual(x)
}

# Shim for scales/#427
as_continuous_pal <- function(x, ...) {
if (is.function(x)) {
return(x)
}
is_color <- grepl("^#(([[:xdigit:]]{2}){3,4}|([[:xdigit:]]){3,4})$", x) |
x %in% grDevices::colours()
if (all(is_color)) {
colour_ramp(x)
} else {
stats::approxfun(seq(0, 1, length.out = length(x)), x)
}
}

# Replace shims by actual scales function when available
on_load({
nse <- getNamespaceExports("scales")
if ("col_mix" %in% nse) {
col_mix <- scales::col_mix
}
if ("as_discrete_pal" %in% nse) {
as_discrete_pal <- scales::as_discrete_pal
}
if ("as_continuous_pal" %in% nse) {
as_continuous_pal <- scales::as_continuous_pal
}
})

# TODO: Replace me if rlang/#1730 gets implemented
# Similar to `rlang::check_installed()` but returns boolean and misses
# features such as versions, comparisons and using {pak}.
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/scale-date.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
---

A <numeric> value was passed to a Datetime scale.
i The value was converted to a <POSIXt> object.
i The value was converted to a <POSIXct> object.

---

Expand Down
30 changes: 15 additions & 15 deletions tests/testthat/_snaps/theme/theme-with-inverted-colours.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.