Importing spatial data into R

A strength of R is the ability to read, write, plot, and analyze spatial data from a GIS (geographic information system).  This allows you to take full advantage of the statistical capabilities of R for spatial data.  I will provide simple examples to get you started.  If you are only looking for a code snippet this may be enough.  Alternatively, more realistic examples of the workflow for a typical project can be found for several common files types.  Files can also be written and exported into a GIS Exporting spatial data to a GIS.

Vector data

          (Shapefile or database)

                             
Simple Example of polygon shapefile import and plot
#Polygon shapefile in R
#load required R packages
library(maptools)
library(maps)
#Download shapefiles to local drive
#from the G310_extent.zip file at
#the bottom of this page. Unzip the file.
  
#Code below assumes file is saved
#here on #your local drive C:/
#Read shapefile with
#"readShapePoly" function from
#maptools package. 
G310 <-readShapePoly("C:/G310_extent/g310.shp")
#Produce a simple plot of the #result (Corpus Christi Bay, Texas #USA)
plot(G310, col="gray") 
box()



Gridded data

(Raster)


Simple Example of ascii grid import and plot

library(adehabitat)
datasc <- import.asc("C:/G310.asc", type=c("numeric"))
library(fields)
image.plot(datasc, main="Corpus Christi Bay, Texas \nBathymetry (m)",xlab="Longitude", ylab="Latitude")

#The result

Simple Example of a raster (grid) import and plot

You may find this code useful if you have a raster from a GIS that you want to import into R.

#To convert as raster image and plot using #default colors
library(raster)
datrast<-raster(datasc)
plot(datrast,main="Corpus Christi Bay, Texas \nBathymetry (m)",
xlab="Longitude", ylab="Latitude")

#For some applications it may be
#preferable to plot in lattice or ggplot2 graphics.
#This can be useful when #multi-panel plots are needed.
#For this example, the raster (datrast) will be used.
#My preferred method is using the
#ggplot2 or lattice libraries
#This requires converting the raster to a dataframe
#convert raster to data frame
library(raster)
library(ggplot2)
datdf <- data.frame(rasterToPoints(datrast))
z1 <- ggplot(datdf, aes(x,y)) + geom_raster(aes(fill=layer))


#To
plot with lattice
library(lattice)
library(raster)
#convert raster to data frame
datdf <- data.frame(rasterToPoints(datrast))
#plot with lattice graphics
levelplot(layer~x*y, data=datdf,   col.regions=rev(terrain.colors(100)),
    main="Corpus Christi Bay, Texas \nBathymetry (m)")

#To plot as a spatial object
library(sp)
#copy layer
datsp <- datdf        
#convert to sp object
coordinates(datsp) <- ~x+y
#plot, result not shown

spplot(datsp, c("layer"),
col.regions=rev(terrain.colors(100)),
main="Corpus Christi Bay, Texas \nBathymetry (m)")
ċ
G310.asc
(671k)
John froeschke,
Mar 21, 2012, 7:22 PM
ċ
John froeschke,
Mar 21, 2012, 7:07 PM
ċ
rasterinR.R
(0k)
John froeschke,
Mar 21, 2012, 7:37 PM
Comments