Skip to content
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

geom_spatraster with alpha scaled by different layer to fill #154

Open
claudehspencer opened this issue Sep 4, 2024 · 1 comment
Open

Comments

@claudehspencer
Copy link

claudehspencer commented Sep 4, 2024

Just wondering if the following approach was possible in tidyterra.

I have a spatraster with a layer for prediction value and another layer for standard error, and I want to plot using one layer to scale fill and the other to scale alpha. The current approach uses geom_tile after converting spatraster to a dataframe.

library(tidyterra)
library(ggplot2)

x <- rast(array(data = rnorm(120, 0, 1), dim = c(5, 5, 2)))
names(x) = c("prediction", "se")

xdf <- as.data.frame(x, xy = T)

ggplot() +
  geom_tile(data = xdf, aes(x = x, y = y, fill = se, alpha = prediction)) +
  scale_alpha_continuous(range = c(0, 1)) +
  scale_fill_gradient(low = "white", high = "deeppink3") +
  theme_minimal()
@dieghernan
Copy link
Owner

Hi,

Currently there is no way to achieve this with tidyterra, however your solution is completely fine IMHO. I would only add a coord_sf(crs = pull_crs(x)) to create a spatial-aware plot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants