Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

library(sp)
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

share|improve this question
up vote 18 down vote accepted

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

col.regions

ggplot2

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.

share|improve this answer
    
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 at 15:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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