4

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.

migrated from stackoverflow.com Apr 28 '15 at 19:29

This question came from our site for professional and enthusiast programmers.

  • 2
    A 'shapefile' is not a single file, it's actually the combination of a .shp file, a .shx file and a .dbf file. They can be read into R using functions in the rgdal, maptools, etc. packages, as @Pascal noted. – Ben Apr 8 '15 at 2:43
6

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")
0

You cannot open an .mxd (ArcMap map document) file in R, so you will not be able to use the same data frame you had in ArcMap. R is statistical software; although it includes a lot of tools for data visualization, it is primarily for statistical analysis, and you are unlikely to find as many utilities for mapping and cartography as you are in ArcMap (or QGIS which, like R, is freely available software).

To open and plot a shapefile in R, first install the rgdal package, then:

library(rgdal)
data.shape<-readOGR(dsn="C:/shapfile_directory",layer="US-101")
plot(data.shape)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy