maximusdooku
maximusdooku

Reputation: 5512

How can I plot a SpatialPolygonDataframe variable?

I have a SPDF of this type:

class       : SpatialPolygonsDataFrame 
features    : 11723 
extent      : -2294145, -1102155, 2191605, 3506145  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0 
variables   : 10
names       : OBJECTID,    POI_ID, PROD_UNIT, Y_Centroid, X_Centroid,   AREA, seg_id,   Avg, Lon_Centro, Lat_Centro 

When I plot it using plot(spdf), I get the polygons.

But I want to color these polygons using one of the variables (say, AREA).

I know how to do it using spplot, but I want to use baseplot as it is much faster. How can I do this?

This is how I would do it using spplot:

spplot(SPDF, "AREA", main = "area (m2)")

Upvotes: 0

Views: 741

Answers (1)

Michael Harper
Michael Harper

Reputation: 15369

This is possible within base R, but not the most user-friendly. It works easiest if you save the colour you want to be plotted as a column within the dataset.

Here is a reproducible dataset:

library(maptools)
setwd(system.file("shapes", package="maptools"))
columbus <- readShapeSpatial("columbus.shp")
plot(columbus)

enter image description here

There are a total of 49 shapefiles in this case, so I am going to just assign a random palette of 7 colours:

columbus$colour <- rep(c("red", "blue", "green", "orange", "pink", "grey", "yellow"),7)
plot(columbus, col=columbus$colour)

enter image description here

If you wanted to assign a colour range, you can check out this answer here: R - stuck with plot() - Colouring shapefile polygons based upon a slot value

Upvotes: 2

Related Questions