I have a dataframe df1
with 10 columns. Two of these columns are lng
and lat
.
I want to create a SpatialPointsDataframe
f
SpatialPointsDataFrame
you need 3 components:proj4string
of the coordinates (AKA, coordinate reference system (CRS))# load some example data
library(sp) # spatial library
data(meuse) # load built in dataset
# prepare coordinates, data, and proj4string
coords <- meuse[ , c("x", "y")] # coordinates
data <- meuse[ , 3:14] # data
crs <- CRS("+init=epsg:28992") # proj4string of coords
# make the SpatialPointsDataFrame object
spdf <- SpatialPointsDataFrame(coords = coords,
data = data,
proj4string = crs)
# check the object class
class(spdf)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
# plot the copper column
spplot(spdf, "copper")