ggplot2r-rasterrastervis

gplot throws unable to find an inherited method for function ‘gplot’ for signature ‘"missing"’ with RasterBrick


I followed this answer to create raster files for each attribute in a shapefile and got a RasterBrick at the end. But when I try to plot it using gplot or ggplot2, it throws an error. I tried reading the file again and then stacking it, but the error is the same.

Link for creating the raster: https://stackoverflow.com/a/47664716/11696009

Error while using gplot:

> gplot(data = x) + geom_tile(aes(fill = value)) + facet_wrap(~ variable)

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘gplot’ for signature ‘"missing"’

With ggplot2 it throws the following error:

> ggplot(data = x) + geom_tile(aes(fill = value)) + facet_wrap(~ variable) + theme_bw()

Error in `fortify()`:
! `data` must be a <data.frame>, or an object coercible by `fortify()`, not an S4 object with class <RasterBrick>.
Run `rlang::last_error()` to see where the error occurred.

Please help me how to resolve this or at least what the reason for this error is.

Reproducible example:

library(raster)
library(rasterVis)
library(ggplot2)

r <- raster(ncols=36, nrows=18)
p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)
p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))
att <- data.frame(id=1:3, var1=10:12, var2=c(6,9,6))
pols <- spPolygons(p1, p2, p3, attr=att)

pols$id <- 1:nrow(pols) 
r <- rasterize(pols, r, field='id')
x <- subs(r, data.frame(pols), by='id', which=2:ncol(pols), filename="rstr.grd")


Solution

  • As the error states, "'data' must be a data.frame, or an object coercible by fortify(), not an S4 object with class RasterBrick". This means ggplot2 cannot interpret a RasterBrick. You have to convert it to something ggplot2 can handle, such as a dataframe:

    library(raster)
    library(ggplot2)
    library(tidyverse)
    
    # Convert RasterBrick created by your example code to dataframe
    df <- as.data.frame(x, xy = TRUE)
    

    As I am not not familiar with RasterBrick objects, I'm not sure which variables you want to extract. I have assumed that the default "var1" and "var2" values in the "df" dataframe represent the same thing so I've pivoted df so both "var1" and "var2" are in the same column. Omit this next step if this is not appropriate. NOTE that if you do skip the pivot_longer() step, in the ggplot() code change both references to "value" to either "var1" or "var2":

    df <- df %>% pivot_longer(!c(x, y))
    

    and now plot df:

    ggplot(df, aes(x, y)) +
      geom_tile(aes(fill = value), show.legend = FALSE) +
      facet_wrap(~ value) + 
      theme_bw()
    

    and the result, using "value" to fill result: result

    This may not be exactly what you want, but it should get you some way to solving your issue.