0

I am trying to plot a variable (R_NIR_ave) which is in an data.frame called "output" consisting of a list of grids with its coordinates and the value for the variable. Because of the way that I calculated this variable, I also have values for grids that correspond to oceans, however I would like to mask these values.

The code I use is as follows:

map <- ggplot(output2, aes(x = lon, y = lat)) +
  geom_tile(data = output2, aes(fill = R_NIR_ave)) +
  geom_contour(data = output2, aes(z = R_NIR_ave), 
               breaks = 0,
               colour = "black", size = 1) +
  borders(
    database = "world",
    fill = NA,
    colour = "black",
    xlim = NULL,
    ylim = NULL )+
  scale_fill_gradient(low = "saddlebrown", high = "lightyellow1", 
                      na.value="white") +
  labs(x = "Longitude", y = "Latitude", fill = "Value") +
  coord_quickmap()

dev.new()
map +
  theme_bw()

Edit: Following suggestions by Tjebo I tried running the following code which gives the following error "Error in FUN(X[[i]], ...) : object 'lon' not found"

    library(ggplot2)
    sea <- ne_download(scale = 10, type = 'ocean', category = "physical",
                       returnclass = "sf")
    #plot
    map <- ggplot(output2, aes(x = lon, y = lat)) +
      geom_tile(data = output2, aes(fill = R_NIR_ave)) +
      geom_contour(data = output2, aes(z = R_NIR_ave), 
                   breaks = 0, # contour for continent
                   colour = "black", size = 1) +
      geom_sf(data = sea, fill = "white") +
      borders(
        database = "world",
        fill = NA,
        colour = "black",
        xlim = NULL,
        ylim = NULL )+
      # scale_fill_gradientn(                       colors = terrain.colors(100),na.value = "grey50") +
      scale_fill_gradient(low = "saddlebrown", high = "lightyellow1",na.value="white", limits=c(my.min(output2$R_NIR_ave),my.max(output2$R_NIR_ave)))+
      # scale_fill_gradient2(low = "grey0", mid = "grey74",
      #                      high = "grey95", midpoint = 35, space = "Lab",
      #                      na.value = "plum1", guide = "colourbar", aesthetics = "fill")+
      
      labs(x = "Longitude", y = "Latitude", fill = "Value")+
      coord_quickmap()
    map +
      theme_bw()
Minicola
  • 1
  • 2
  • hi. check this **very** related thread - there is a proposed solution and also you'll also find plenty of linked threads https://stackoverflow.com/questions/74980559/combining-land-only-maps-and-contour-plots-using-ggplot?noredirect=1&lq=1 – tjebo Mar 29 '23 at 18:42

0 Answers0