Skip to content

Commit

Permalink
Merge pull request #166 from dieghernan/colshift
Browse files Browse the repository at this point in the history
Expand PR #165 to other scales, tests and document
  • Loading branch information
dieghernan authored Jan 23, 2025
2 parents c487144 + 0fe863e commit 2e40870
Show file tree
Hide file tree
Showing 21 changed files with 1,081 additions and 90 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
- Improve handling of factors when several layers have different levels. This
is done using `terra::combineLevels()` (**terra** \>= `1.8-10`). See
<https://stackoverflow.com/questions/79340152>.
- Now `scales` that uses limits truncate the legend when `limits` parameter is
provided (#165 \@Fan-iX). Scales impacted:
- `scale_*_cross_blended_tint_c` and `scale_*_cross_blended_tint_b`.
- `scale_*_hypso_tint_c` and `scale_*_hypso_tint_b`.
- `scale_*_grass_c` and `scale_*_grass_b`.

# tidyterra 0.6.2

Expand Down
34 changes: 12 additions & 22 deletions R/scales_cross_blended.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,6 @@
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_cross_blended_c(palette = "cold_humid")
#'
#' # Use hypsometric tint version...
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_cross_blended_tint_c(palette = "cold_humid")
#'
#' # ...but not suitable for the range of the raster: adjust
#' my_lims <- minmax(volcano2_rast) %>% as.integer() + c(-2, 2)
#'
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_cross_blended_tint_c(
#' palette = "cold_humid",
#' limits = my_lims
#' )
#'
#' # Full map with true tints
#'
Expand Down Expand Up @@ -149,13 +135,12 @@
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_cross_blended_b(breaks = seq(70, 200, 25), palette = "arid")
#'
#' # With limits and breaks
#' # With breaks
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_cross_blended_tint_b(
#' scale_fill_cross_blended_b(
#' breaks = seq(75, 200, 25),
#' palette = "arid",
#' limits = my_lims
#' palette = "arid"
#' )
#'
#' # With discrete values
Expand Down Expand Up @@ -414,6 +399,11 @@ cross_blended.colors <- function(n, palette = "cold_humid",
#' almost as an uniform color. This could be adjusted using the
#' `limits`/`values` parameters.
#'
#' When passing `limits` parameter to `scale_*_cross_blended_tint_*` the
#' colors would be restricted of those specified by this parameter, keeping the
#' distribution of the tint. You can combine this with `oob` (i.e.
#' `oob = scales::oob_squish`) to avoid blank pixels in the plot.
#'
#' `cross_blended.colors2()` provides a gradient color palette where the
#' distance between colors is different depending of the type of color.
#' In contrast, `cross_blended.colors()` provides an uniform gradient across
Expand Down Expand Up @@ -503,8 +493,8 @@ scale_fill_cross_blended_tint_c <- function(palette = "cold_humid", ...,

if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)

ggplot2::scale_fill_gradientn(...,
colors = hexcol,
Expand Down Expand Up @@ -548,8 +538,8 @@ scale_colour_cross_blended_tint_c <- function(palette = "cold_humid", ...,

if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)

ggplot2::scale_colour_gradientn(...,
colors = hexcol,
Expand Down Expand Up @@ -593,8 +583,8 @@ scale_fill_cross_blended_tint_b <- function(palette = "cold_humid", ...,

if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)

ggplot2::scale_fill_stepsn(
...,
Expand Down Expand Up @@ -639,8 +629,8 @@ scale_colour_cross_blended_tint_b <- function(palette = "cold_humid", ...,

if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)

ggplot2::scale_colour_stepsn(
...,
Expand Down
13 changes: 9 additions & 4 deletions R/scales_grass.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
#' `use_grass_range` argument, When `FALSE` the color scales would be mapped
#' to the range of values of the `color/fill` aesthethics, See **Examples**.
#'
#' When passing `limits` parameter the colors would be restricted of those
#' specified by this parameter, keeping the distribution of the palette. You can
#' combine this with `oob` (i.e. `oob = scales::oob_squish`) to avoid blank
#' pixels in the plot.
#'
#' @examples
#' \donttest{
#' filepath <- system.file("extdata/volcano2.tif", package = "tidyterra")
Expand Down Expand Up @@ -239,8 +244,8 @@ scale_fill_grass_c <- function(palette = "viridis", ...,
} else {
if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)
}

ggplot2::scale_fill_gradientn(...,
Expand Down Expand Up @@ -292,8 +297,8 @@ scale_colour_grass_c <- function(palette = "viridis", ...,
} else {
if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)
}

ggplot2::scale_colour_gradientn(...,
Expand Down Expand Up @@ -350,8 +355,8 @@ scale_fill_grass_b <- function(palette = "viridis", ...,
} else {
if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)
}

ggplot2::scale_fill_stepsn(
Expand Down Expand Up @@ -404,8 +409,8 @@ scale_colour_grass_b <- function(palette = "viridis", ...,
} else {
if (is.null(values)) values <- hypsocol$limit
# Reescale
res <- scales::rescale(values)
if (is.null(limits)) limits <- range(values)
res <- scales::rescale(values, from = limits)
}

ggplot2::scale_color_stepsn(
Expand Down
27 changes: 9 additions & 18 deletions R/scales_hypso.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,6 @@
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_hypso_c(palette = "colombia_hypso")
#'
#' # Use hypsometric tint version...
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_hypso_tint_c(palette = "colombia_hypso")
#'
#' # ...but not suitable for the range of the raster: adjust
#' my_lims <- minmax(volcano2_rast) %>% as.integer() + c(-2, 2)
#'
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_hypso_tint_c(
#' palette = "colombia_hypso",
#' limits = my_lims
#' )
#'
#' # Full map with true tints
#'
Expand Down Expand Up @@ -134,13 +120,12 @@
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_hypso_b(breaks = seq(70, 200, 25), palette = "wiki-2.0_hypso")
#'
#' # With limits and breaks
#' # With breaks
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_hypso_tint_b(
#' scale_fill_hypso_b(
#' breaks = seq(75, 200, 25),
#' palette = "wiki-2.0_hypso",
#' limits = my_lims
#' palette = "wiki-2.0_hypso"
#' )
#'
#' # With discrete values
Expand Down Expand Up @@ -392,6 +377,12 @@ hypso.colors <- function(n, palette = "etopo1_hypso",
#' almost as an uniform color. This could be adjusted using the
#' `limits`/`values` parameters.
#'
#' When passing `limits` parameter to `scale_*_hypso_tint_*` the colors would
#' be restricted of those specified by this parameter, keeping the distribution
#' of the tint. You can combine this with `oob` (i.e.
#' `oob = scales::oob_squish`) to avoid blank pixels in the plot.
#'
#'
#' `hypso.colors2()` provides a gradient color palette where the distance
#' between colors is different depending of the type of color. In contrast,
#' `hypso.colors()` provides an uniform gradient across colors. See
Expand Down
26 changes: 8 additions & 18 deletions man/scale_cross_blended.Rd

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

5 changes: 5 additions & 0 deletions man/scale_grass.Rd

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

26 changes: 8 additions & 18 deletions man/scale_hypso.Rd

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

99 changes: 99 additions & 0 deletions tests/testthat/_snaps/scales_col_cross_blended/pr165-01-nolims.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions tests/testthat/_snaps/scales_col_cross_blended/pr165-02-lims1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions tests/testthat/_snaps/scales_col_cross_blended/pr165-03-lims1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions tests/testthat/_snaps/scales_fill_hypso/pr165-01-nolims.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions tests/testthat/_snaps/scales_fill_hypso/pr165-02-lims1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions tests/testthat/_snaps/scales_fill_hypso/pr165-03-lims1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions tests/testthat/_snaps/scales_grass/pr165-01-nolims.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions tests/testthat/_snaps/scales_grass/pr165-02-lims1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 102 additions & 0 deletions tests/testthat/_snaps/scales_grass/pr165-03-lims1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 35 additions & 2 deletions tests/testthat/test-scales_col_cross_blended.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ test_that("Continous scale tint", {
# Modify limits
p3 <- p + scale_color_cross_blended_tint_c(limits = c(20, 26))
mod_lims <- ggplot2::layer_data(p3)$colour
expect_true(!any(mod_lims %in% mod))
expect_identical(mod_lims, mod)
expect_true(!any(mod_lims %in% init))

# Modify also with values
Expand All @@ -222,7 +222,7 @@ test_that("Continous scale tint", {
mod_values <- ggplot2::layer_data(p4)$colour
expect_true(!any(mod_values %in% mod_lims))
expect_true(!any(mod_values %in% mod))
expect_true(!any(mod_values %in% init))
expect_true(any(mod_values %in% init))
})

test_that("Breaking scale", {
Expand Down Expand Up @@ -366,3 +366,36 @@ test_that("Breaking scale tint", {
expect_true(!any(mod_values %in% mod))
expect_true(!any(mod_values %in% init))
})


test_that("PR 165", {
suppressWarnings(library(ggplot2))
suppressWarnings(library(terra))

# Import also vector
f <- system.file("extdata/asia.tif", package = "tidyterra")
r <- rast(f)


p1 <- ggplot() +
geom_spatraster(data = r) +
scale_fill_cross_blended_tint_c()

wlims1 <- ggplot() +
geom_spatraster(data = r) +
scale_fill_cross_blended_tint_c(limits = c(500, 4000))

wlims2 <- ggplot() +
geom_spatraster(data = r) +
scale_fill_cross_blended_tint_c(
limits = c(0, 600),
oob = scales::squish
)



# Scales
vdiffr::expect_doppelganger("pr165_01: nolims", p1)
vdiffr::expect_doppelganger("pr165_02: lims1", wlims1)
vdiffr::expect_doppelganger("pr165_03: lims1", wlims2)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-scales_col_hypso.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ test_that("Continous scale tint", {
# Modify limits
p3 <- p + scale_colour_hypso_tint_c(limits = c(20, 26))
mod_lims <- ggplot2::layer_data(p3)$colour
expect_true(!any(mod_lims %in% mod))
expect_identical(mod_lims, mod)
expect_true(!any(mod_lims %in% init))

# Modify also with values
Expand All @@ -212,7 +212,7 @@ test_that("Continous scale tint", {
mod_values <- ggplot2::layer_data(p4)$colour
expect_true(!any(mod_values %in% mod_lims))
expect_true(!any(mod_values %in% mod))
expect_true(!any(mod_values %in% init))
expect_true(any(mod_values %in% init))
})

test_that("Breaking scale", {
Expand Down
Loading

0 comments on commit 2e40870

Please sign in to comment.