NA values are shown in gray color #44
-
NA values are removed in the package examples; however, they are shown in gray color in my case: library(tidyterra)
library(ggplot2)
library(terra)
nz_elev = rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
ggplot() +
geom_spatraster(data = nz_elev)
#> SpatRaster resampled to ncells = 501147 Created on 2022-07-07 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
HI @Nowosad ! Plotting On the examples I think I used library(tidyterra)
#> ── Attaching packages ─────────────────────────────────────── tidyterra 0.2.0 ──
#>
#> Suppress this startup message by setting Sys.setenv(tidyterra.quiet = TRUE)
#> ✔ tibble 3.1.6 ✔ dplyr 1.0.7
#> ✔ tidyr 1.1.4
library(ggplot2)
library(terra)
#> terra 1.5.34
#>
#> Attaching package: 'terra'
#> The following object is masked from 'package:tidyr':
#>
#> extract
nz_elev <- rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
ggplot() +
geom_spatraster(data = nz_elev)
#> SpatRaster resampled to ncells = 501147 # The underlying default scale is this
ggplot() +
geom_spatraster(data = nz_elev) +
scale_fill_continuous(na.value = "grey50")
#> SpatRaster resampled to ncells = 501147 # Avoid plotting NAs with this
ggplot() +
geom_spatraster(data = nz_elev) +
scale_fill_continuous(na.value = NA)
#> SpatRaster resampled to ncells = 501147 # The scales of tidyterra has na.value = NA by default, so NAs are not plotted
ggplot() +
geom_spatraster(data = nz_elev) +
scale_fill_whitebox_c()
#> SpatRaster resampled to ncells = 501147 # But his value can be manipulated
ggplot() +
geom_spatraster(data = nz_elev) +
scale_fill_whitebox_c(na.value = "lightblue1")
#> SpatRaster resampled to ncells = 501147 Created on 2022-07-07 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
For completitude, the default on library(tidyterra)
library(terra)
nz_elev <- rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
ramp <- colorRampPalette(c("#132B43","#56B1F7"))
# The default on terra::plot is not plotting NAs
plot(nz_elev, col=ramp(100)) # While this would be the default on ggplot2
plot(nz_elev, col=ramp(100), colNA="grey50") Created on 2022-07-07 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the explanation, @dieghernan ! |
Beta Was this translation helpful? Give feedback.
HI @Nowosad !
Plotting
NA
s is the default behaviour on ggplot2, that plotsNA
s withgrey50
.On the examples I think I used
tidyterra::scale_fill_*
. These scales differs of the default ggplot2 scales on an option,na.value
, that have been set toNA
. This preventsNA
s from being plotted, but still can be manipulated. Hope this reprex provides clarity: