[GIS] Converting data from data frame into raster file using R

rraster

I have the following data frame (long, lat, value) (all coordinates are within Colombia):

Spoints:
        x           y           z
    1   -75.67083   7.57916667  7.328901e-19
    2   -74.59583   2.34583333  4.470517e-28
    3   -73.67083   5.82083333  2.796526e-11
    4   -75.31250   8.68750000  2.867735e-20
    5   -73.88750   10.47083333 4.460837e-14
    6   -73.22083   -1.56250000 3.127243e-35
    7   -72.12083   -1.47083333 1.361218e-37
    8   -74.54583   9.60416667  1.108581e-37

I would like to create a raster and plot it on Colombia's map.

Best Answer

You are providing spurious information and omitting important information. I do not care that you are plotting over the "wrld_simpl" data but would like to know what the resulting object classes are and if there are any attributes in the SpatialPixelsDataFrame and resulting raster objects. I would ask, why are you projecting to the same projection? The spTransform is completely unnecessary and possible causing you issues.

Please clarify your post to distill it to the actual issue. To assign values, the raster function should just grab the first attribute in the @data slot. However you can specify which column you want by specifying the column name. Here is a quick worked example.

library(sp)
library(raster)

data(meuse.grid) 
coordinates(meuse.grid) <- ~x+y 
gridded(meuse.grid) <- TRUE

class(meuse.grid)
str(meuse.grid@data)

r <- raster(meuse.grid, "dist") 
class(r)
plot(r)