Home > Enterprise >  Plotting RasterLayer objects using ggplot
Plotting RasterLayer objects using ggplot

Time:01-11

I am incredibly sorry, but I feel like there aren´t good example datasets for this question, and thus I will not be able to prdocue a reproducible example. If this makes this question untenable then I will definitely delete.

I am trying to plot a RasterLayer object in ggplot2 using geom_raster(), but get an error. I have seen that it seems like you have to convert RasterLayer objects to dataframes, but doing a simple conversion doesn't seem to work on this particular RasterLayer.

For reference, this data plots easily using the plot() function, but I'd like to do it in ggplot if possible due to its versatility.

Here is the code that attempts to plot the data. Of note, the object is stored in a list, along with many other model objects that I would like to plot:

library(ggplot2)
ggplot()   geom_raster(data = mylist$model_object)

And the error:

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

Here is what happens when I run mylist$model_object :

class      : RasterLayer 
dimensions : 114, 180, 20520  (nrow, ncol, ncell)
resolution : 0.1666667, 0.1666667  (x, y)
extent     : -116, -86, 14, 33  (xmin, xmax, ymin, ymax)
crs        :  proj=longlat  datum=WGS84  no_defs 
source     : memory
names      : layer 
values     : 0, 0.5845846  (min, max)

And here is the structure of the object:

str(mylist$model_object)
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 0
  .. .. ..@ blockcols   : int 0
  .. .. ..@ driver      : chr ""
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : num [1:20520] 0.002 0 0 0 0 ...
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi TRUE
  .. .. ..@ fromdisk  : logi FALSE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num 0
  .. .. ..@ max       : num 0.585
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr "layer"
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num -116
  .. .. ..@ xmax: num -86
  .. .. ..@ ymin: num 14
  .. .. ..@ ymax: num 33
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 180
  ..@ nrows   : int 114
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr " proj=longlat  datum=WGS84  no_defs"
  .. .. ..$ comment: chr "GEOGCRS[\"unknown\",\n    DATUM[\"World Geodetic System 1984\",\n        ELLIPSOID[\"WGS 84\",6378137,298.25722"| __truncated__
  ..@ history : list()
  ..@ z       : list()

I totally understand if this is not enough to go off of. If anyone has a suggestion about easy to access raster data online that I can use as a proxy repoducible example, it would be much appreciated. Thank you!

CodePudding user response:

We may use the as.data.frame method for raster

> grep("data.frame", methods(class = "RasterLayer"), value = TRUE)
[1] "as.data.frame,Raster-method"        "extract,Raster,data.frame-method"   "rasterize,data.frame,Raster-method" "subs,Raster,data.frame-method"  
as.data.frame(mylist$model_object, xy = TRUE)
  • Related