Gregory
Gregory

Reputation: 4279

How to create a raster from a data frame in r?

I have a data frame in which values (l) are specified for Cartesian coordinates (x, y) as in the following minimal working example.

set.seed(2013)
df <- data.frame( x = rep( 0:1, each=2 ),
                  y = rep( 0:1,  2),
                  l = rnorm( 4 ))

df
#   x y           l
# 1 0 0 -0.09202453
# 2 0 1  0.78901912
# 3 1 0 -0.66744232
# 4 1 1  1.36061149

I want to create a raster using the raster package, but my reading of the documentation has not revealed a simple method for loading data in the form that I have it into the raster cells. I've come up with a couple ways to do it using for loops, but I suspect that there's a much more direct approach that I'm missing.

Upvotes: 32

Views: 63062

Answers (3)

horse
horse

Reputation: 88

Updating this corresponding to @zubergu about Rasterizing irregular data.

So the answer that I have adapted from the link below and possibly makes it even more simpler to understand is:

library(raster)
library(rasterize)

# Suppose you have a dataframe like this
lon <- runif(20, -180, 180)
lat <- runif(20, -90, 90)
vals <- rnorm(20)
df <- data.frame(lon, lat, vals)

# will need to rename colnames for raster
colnames(df) <- c('x', 'y', 'vals')

# create a raster object
r_obj <- raster(xmn=-180, xmx=180, ymn=-90, ymx=90, resolution=c(5,5))

# use rasterize to create desired raster
r_data <- rasterize(x=df[, 1:2], # lon-lat data
                    y=r_obj, # raster object
                    field=df[, 3], # vals to fill raster with
                    fun=mean) # aggregate function

plot(r_data)

Original response:

for those of you like @yuliaUU looking to convert irregular data to a raster, please see @RobertH's answer here.

Upvotes: 5

Pankaj
Pankaj

Reputation: 1336

An easier solution exists as

 library(raster)
 dfr <- rasterFromXYZ(df)  #Convert first two columns as lon-lat and third as value                
 plot(dfr)
 dfr                  
 class       : RasterLayer 
 dimensions  : 2, 2, 4  (nrow, ncol, ncell)
 resolution  : 1, 1  (x, y)
 extent      : -0.5, 1.5, -0.5, 1.5  (xmin, xmax, ymin, ymax)
 coord. ref. : NA 
 data source : in memory
 names       : l 
 values      : -2.311813, 0.921186  (min, max)

Plot

Further, you may specify the CRS string. Detailed discussion is available here.

Upvotes: 62

mnel
mnel

Reputation: 115392

Here is one approach, via SpatialPixelsDataFrame

library(raster)
# create spatial points data frame
spg <- df
coordinates(spg) <- ~ x + y
# coerce to SpatialPixelsDataFrame
gridded(spg) <- TRUE
# coerce to raster
rasterDF <- raster(spg)
rasterDF
# class       : RasterLayer 
# dimensions  : 2, 2, 4  (nrow, ncol, ncell)
# resolution  : 1, 1  (x, y)
# extent      : -0.5, 1.5, -0.5, 1.5  (xmin, xmax, ymin, ymax)
# coord. ref. : NA 
# data source : in memory
# names       : l 
# values      : -0.6674423, 1.360611  (min, max)

help('raster') describes a number of methods to create a raster from objects of different classes.

Upvotes: 34

Related Questions