I have SpatialPolygons or SpatialPolygonsDataFrames which I'd like to plot. How do I change the color using the three plotting systems in R (base graphics, lattice graphics, and ggplot2)?

Example data:

Srs1 = Polygons(list(Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))), "s1")
Srs2 = Polygons(list(Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))), "s2")

SpDF <- SpatialPolygonsDataFrame( SpatialPolygons(list(Srs1,Srs2)), 
            data.frame( z=1:2, row.names=c("s1","s2") ) )
spplot(SpDF, zcol="z")

test map


Base graphics (plot)

For base graphics, col= sets the fill and border= sets the border color.

plot(SpDF, col="red",border="green")

If you want a choropleth map, set col= to a vector of colors, one for each polygon's data value.

base graphics

Lattice graphics (spplot)

Unlike base graphics, the col= option for lattice graphics controls the border color. For no border, set col=NA or col="transparent".

spplot(SpDF, zcol="z", col=NA)

no border

For the polygon fill, set col.regions to a standard R color scheme (gray, rainbow.colors, topo.colors, etc.):

spplot(SpDF, zcol="z", col.regions=gray(seq(0,1,.01)))



As usual for ggplot2, you specify the levels and it picks the color scheme. You can override the scheme for the whole plot, however. Details to follow at some point.

  • Great question/answer. Perhaps you would like to add information about setting the background colour. To set the background colour in spplot, use par.settings = list(panel.background=list(col="lightblue")). – nograpes Feb 12 '16 at 15:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.