[GIS] How to read ArcGIS files in R

dbfmxdrshapefile

I am new to using spatial data in R. So far the tutorials I have seen consist of using maptools, map, ggplot2 and rgdal to open ".shp" files along with ".csv" files to plot the polygons/ points using latitude and longitude.

I have always used ArcGIS and presently have these set of files: US-101.shp, US-101.shx, US-101.dbf and US-101.mxd

I would like to open these files so that I could use the shapefile along with a data frame related to it. I can open the shapefile and also see the table which has "ID", "LAYER", "COLOR", "LINETYPE", "LINEWIDTH" and "ELEVATION" variables.

I am not sure how to use it to plot the shapefile. In ArcGIS the layer is easily visible once I open .mxd file.

Best Answer

As handy as R is for so many tasks, it is important to remember that 1) R is not a GIS and 2) quality mapping is downright difficult compared to creating maps with QGIS or ArcGIS. The following example borrows heavily from two R-bloggers blogs (blog 1 and blog 2). Here, I simply mapped a polygon shapefile using Google Satellite Imagery as a basemap.


enter image description here


require(rgdal)
require(ggplot2)
require(rgeos)
require(ggmap)
require(RColorBrewer)

# Read shapefile using OGR
shp = "C:/temp/circles.shp"
myshp = readOGR(shp, layer = basename(strsplit(shp, "\\.")[[1]])[1]) # This is a fancy way of being lazy, so I do not need to type the layer name in

# Convert to lat long
myshp_proj = spTransform(myshp, CRS("+proj=longlat +datum=WGS84"))

# Find polygon centroid (This centers the map)
centroid = gCentroid(myshp_proj)

# Get the Google basemap
mapImageData1 = get_map(location = c(lon = centroid$x, lat = centroid$y),
                    color = "color",
                    source = "google",
                    maptype = "satellite",
                    zoom = 13)

# Convert shapefile to format ggmap can work with
polys = fortify(myshp_proj)

# Define the color scheme for mapping shp
colors = brewer.pal(9, "OrRd")

# create the final map
ggmap(mapImageData1) +
    geom_polygon(aes(x = long, y = lat, group = group),
    data = polys,
    color = colors[9],
    fill = colors[6],
    alpha = 0.5) +
    labs(x = "Longitude", y = "Latitude")