Skip to content

How do I correctly use the alpha aesthetic with geom_spatraster? #113

Answered by dieghernan
jlegewie asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

You can do it using computed variables (i.e. value, lyr). In this case you are using the name of the layer, that does not work. See the docs:

This geom computes internally some variables that are available for use as aesthetics, using (for example) aes(alpha = after_stat(value)) ...:

library(terra)
#> terra 1.7.46
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggplot2)

r1 <- rast(nrows = 50, ncols = 50)
values(r1) <- runif(ncell(r1))

ggplot() +
  geom_spatraster(data = r1, aes(fill = lyr.1)) +
  scale_fill_continuous() +
  theme_void()

# With alpha, use computed variable value
ggplot() +

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jlegewie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
📊 ggplot2 Issues related with the implementation of ggplot2 functionalities ❔ q&a Questions on the usage of tidyterra
2 participants