grey scale map #42
-
How can I display in grey scale?
|
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
To extend a bit my question: I'm trying to display individual spectral bands of multispectral images. I need plots to be in consistent grey scale, that is, an image with range 0-100 should be half bright as another in the 100-200 range. |
Beta Was this translation helpful? Give feedback.
-
Hi, So I see here two issues:
See if this code helps: library(tidyterra)
#> ── Attaching packages ─────────────────────────────────────── tidyterra 0.2.0 ──
#>
#> Suppress this startup message by setting Sys.setenv(tidyterra.quiet = TRUE)
#> ✔ tibble 3.1.7 ✔ dplyr 1.0.9
#> ✔ tidyr 1.2.0
library(ggplot2)
file <- system.file("extdata/cyl_tile.tif", package = "tidyterra")
tile <- terra::rast(file)
ggplot() +
geom_spatraster(data = tile, aes(fill = cyl_tile_1)) +
scale_fill_grey()
#> Error: Continuous value supplied to discrete scale
# Use a custom scale, scale_fill_grey is for discrete values
ggplot() +
geom_spatraster(data = tile, aes(fill = cyl_tile_1)) +
scale_fill_gradientn(colors = gray.colors(100,
start = 0.2,
end = .8, rev = TRUE
)) # Multiband
ggplot() +
geom_spatraster(data = tile) +
scale_fill_gradientn(colors = gray.colors(100,
start = 0.2,
end = .8, rev = TRUE
)) +
facet_wrap(~lyr) # Now on multiband with different ranges
tile2 <- tile %>%
mutate(cyl_tile_2_mod = as.integer(100 * cyl_tile_2 / 250)) %>%
select(cyl_tile_1, cyl_tile_2_mod)
tile2
#> class : SpatRaster
#> dimensions : 212, 261, 2 (nrow, ncol, nlyr)
#> resolution : 2445.985, 2445.985 (x, y)
#> extent : -812067, -173664.9, 4852834, 5371383 (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
#> sources : memory
#> memory
#> names : cyl_tile_1, cyl_tile_2_mod
#> min values : 35, 14
#> max values : 253, 100
ggplot() +
geom_spatraster(data = tile2) +
scale_fill_gradientn(colors = gray.colors(100,
start = 0.2,
end = .8, rev = TRUE
)) +
facet_wrap(~lyr) Created on 2022-09-20 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
Yes, many thanks, |
Beta Was this translation helpful? Give feedback.
-
Why not normalize data so you would have the same scale? |
Beta Was this translation helpful? Give feedback.
-
That is what I often do with |
Beta Was this translation helpful? Give feedback.
-
So all this issue is more related with how to work with the ggplot2 system rather than a bug or issue on tidyterra. I would suggests to ask a question on https://stackoverflow.com/questions/tagged/ggplot2. However, I think I can put some light on this. I assume library(tidyterra)
#> ── Attaching packages ────────────────────────────────── tidyterra 0.2.0.9000 ──
#>
#> Suppress this startup message by setting Sys.setenv(tidyterra.quiet = TRUE)
#> ✔ tibble 3.1.8 ✔ dplyr 1.0.10
#> ✔ tidyr 1.2.1
library(ggplot2)
r <- terra::rast(system.file("extdata/cyl_temp.tif",
package = "tidyterra"
))
# Select two layers
r2 <- r %>%
mutate(tavg_05_mod = tavg_05 * 2) %>%
select(tavg_04, tavg_05_mod)
# Different ranges
r2
#> class : SpatRaster
#> dimensions : 89, 116, 2 (nrow, ncol, nlyr)
#> resolution : 3856.617, 3856.617 (x, y)
#> extent : 2893583, 3340950, 2019451, 2362690 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> sources : memory
#> memory
#> names : tavg_04, tavg_05_mod
#> min values : 0.565614, 8.588204
#> max values : 13.283829, 33.481796
pal <- gray.colors(100, rev = FALSE)
ggplot() +
geom_spatraster(data = r2) +
facet_wrap(~lyr) +
scale_fill_gradientn(colours = pal, na.value = NA) # Adjust limits
# Note the change on the legend and the colors of the plot
ggplot() +
geom_spatraster(data = r2) +
facet_wrap(~lyr) +
scale_fill_gradientn(
colours = pal, na.value = NA,
limits = c(0, 100)
) # Individual plots with the same limits
ggplot() +
geom_spatraster(data = r2 %>% select(1)) +
facet_wrap(~lyr) +
scale_fill_gradientn(
colours = pal, na.value = NA,
limits = c(0, 100)
) ggplot() +
geom_spatraster(data = r2 %>% select(2)) +
facet_wrap(~lyr) +
scale_fill_gradientn(
colours = pal, na.value = NA,
limits = c(0, 100)
) Created on 2022-09-20 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
-
I agree with closing, but please note that some of these examples would be very useful in the doc, where there is nothing of grey scale display. Also, I would personally prefer having a grey scale by default when using geom_spatraster() instead of the current shades of blue. |
Beta Was this translation helpful? Give feedback.
-
Thanks, regarding
This is to be tackle on ggplot2 not tidyterra. The default blue palette is the one in use for ggplot2: library(ggplot2)
# Default palette on ggplot2
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_tile() In the previous example tidyterra is not involved. But good news, as per ?scale_colour_continuous the default continuous palette con be changed with # Can be changed
# Get default
tmp <- getOption("ggplot2.continuous.fill") # store current setting
# Change the default
# Custom default palette function
scale_fill_cont_grey <- function(...) {
greycols <- grey.colors(2)
scale_fill_gradient(..., low = greycols[1], high = greycols[2], na.value = NA)
}
# Now make it by default
options(ggplot2.continuous.fill = scale_fill_cont_grey)
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_tile() In the previous example, we didn't need to add Now see how this plays with tidyterra: # Check with tidyterra
library(tidyterra)
#> ── Attaching packages ─────────────────────────────────────── tidyterra 0.2.0 ──
#>
#> Suppress this startup message by setting Sys.setenv(tidyterra.quiet = TRUE)
#> ✔ tibble 3.1.7 ✔ dplyr 1.0.9
#> ✔ tidyr 1.2.0
r <- terra::rast(system.file("extdata/cyl_temp.tif", package = "tidyterra"))
ggplot() +
geom_spatraster(data = r) +
facet_wrap(~lyr) We got it! By last, restore the ggplot blue palette as the default palette: # Restore the ggplot2 default option
options(ggplot2.continuous.fill = tmp)
# Now back to blue
ggplot() +
geom_spatraster(data = r) +
facet_wrap(~lyr) Created on 2022-09-21 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
I understand you want to stick to ggplot2 general conventions, but note, on the other hand, that geom_spatraster() is very particular within a ggplot context. While these blue shades might be a good (or tolerable) default in the general ggplot plot, this is not the case for an image (perhaps acceptable for a map, but not for an image). I do emphasize that the grey scale should be the default in geom_spatraster(). |
Beta Was this translation helpful? Give feedback.
Hi,
So I see here two issues:
scale_fill_grey()
is a discrete scale, meant to be used with categorical variables. You would need to create aequivalent version with
scale_fill_gradientn()
.ggplot2
if you usefacet_wrap()
.See if this code helps: