Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 11 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
add comment

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.