2

I downloaded the MCD12Q1 data (land cover and category type data) by MODIStsp package, and I want to plot it using tidyterra package.

For example,

library(terra)
library(tidyterra)
library(tidyverse)
t <- rast(ncol=10,nrow=10,
          vals=sample(x=1:10,size=100,replace = T))
plot(t)
t[t==6] <- NA
plot(t)
text(t)
names(t) <- 'v'
f <- t %>%
  mutate(fac = as.factor(v))
plot(f)
ggplot()+
  geom_spatraster(data=f,
                  aes(fill=fac))+
  theme_bw()+
  scale_fill_cross_blended_d()

enter image description here

2 Answers 2

5

Use na.translate=FALSE in the scale function:

ggplot() + 
 geom_spatraster(data=f,aes(fill=fac)) +
 theme_bw() + 
 scale_fill_cross_blended_d(na.translate=FALSE)

enter image description here

Why isn't this documented? Well, scale_fill_cross_blended_d calls some other scale function, maybe scale_fill_discrete and the help for that says that ... args are passed on to the scale type, but doesn't give any clues how to find out how to get help for the scale type, or have a "See Also" section but eventually (by searching the source code for na.translate, which I could only do because I knew what I was looking for) I found the general parameters for discrete scales via ?discrete_scale which lists na.translate and other parameters that should work for all discrete scales.

See Also: discrete_scale would save a lot of people a lot of searching and maybe I'll submit a documentation request.

5
  • Thanks, but why this parameter is not showed in the help package.
    – jackywang
    Jan 27, 2023 at 16:48
  • Hi, that’s not showed because that option is part of ggplot2 system. The scales I provided in the package leverages on that
    – dieghernan
    Jan 27, 2023 at 17:27
  • It is in fact defined via on the man page where you can find a link to the underlying functions
    – dieghernan
    Jan 27, 2023 at 17:28
  • Issues/PR welcomed github.com/dieghernan/tidyterra/issues/new/choose
    – dieghernan
    Jan 27, 2023 at 17:32
  • I think the help for the scale being used here is fine, its just chasing down all the links and somehow finding ?discrete_scale where the parameter is finally documented. If you are a ggplot new user you might look at ?scale_colour_discrete but I can't find a path to ?discrete_scale, just a twisty turny maze of passages.... Its a documentation PR to ggplot2 that I'd want to make.
    – Spacedman
    Jan 27, 2023 at 17:38
0

I prefer using tmap. Note my order of loading libraries, placing of functions, arguments and replacing == with %in%. This can be reproduced even more parsimoniously using other tidyverse functions I will then digress from the OP.

library(tmap)
library(tidyverse)

t <- rast(ncol=10,nrow=10,
          vals=sample(x=1:10,
                      size=100,
                      replace = T))

t[t %in% 6] <- NA

names(t) <- 'v'

tm_shape(t) +
  tm_raster() +
  tm_layout(
    legend.outside = TRUE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.