How to set line width and color when plotting a shapefile with plot()

CptNemo picture CptNemo · Sep 25, 2013 · Viewed 14.9k times · Source

I have a simple shapefile I want to plot with the general plot() (I noticed that ggplot is extremely slow in plotting maps).

I can correctly plot the shape with the code

library(maptools)    
map_shp <- readShapePoly(map_filepath)
map <- fortify(map_shp)
plot(map)

But how could I define the color and width of the lines?

Answer

Josh O&#39;Brien picture Josh O'Brien · Sep 25, 2013

If you aren't set on using ggplot2 for everything, you can sidestep fortify() and just use the sp package's well-developed tools for plotting objects of class SpatialPolygons*:

library(rgdal) # For readOGR(), generally preferable to readShapePoly() IMHO
library(spdep) # For the "columbus.shp" file that ships with the package

map <- readOGR(dsn = system.file("etc/shapes", package="spdep"), 
               layer = "columbus")
plot(map, border="red", lwd=3)

enter image description here