1
votes

In this thread ( GADM-Maps cross-country comparision graphics ) Gavin Simpson helped me to merge multiple Maps from GADM to one object that can be plotted.

But how can I now access one part of the spatial polygons data frame - for instance how could I change the color of one country?

Additional question (I don't really need that now, but maybe I can learn something): Why does the solution I posted in the former thread only work with level 0? Is there a way to make it also work with other levels?

[edit]
answers below show how to change colors. to access one of the rows int the spatial polygons data frame one can use

> row.names(df)
# outputs the accessable rows
# e.g. [1] "ARG" "CHL"
> plot(df["ARG",])
# plots just Argentina
3

3 Answers

2
votes

You can specify the colours directly, by passing in a vector of colours, one for each row in the SpatialPolygonsDataFrame.

Here's an example using data from the maptools package.

 library(maptools)

  xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

  plot(xx)  ## default plot

  plot(xx, col = "lightblue")  ## single colour

  plot(xx, col = sample(rainbow(nrow(xx)))) ## colour for every poly

  ## colour for all, with one different
  cols <- rep("lightblue", nrow(xx))

  cols[10] <- "grey"

  plot(xx, col = cols)
2
votes

A simplistic answer (others may have more elegant solutions) is to pull the names vector out of your object and use which to get an index. E.g.:

cols <- rep("lightgreen",length(NAME_ENGLI))
cols[which(NAME_ENGLI=="ARG")] <- "red"
plot(xx,col=cols)
1
votes

Assuming you have a SpatialPolygons object, you just specify col argument. See sp vignette on classes and methods: http://cran.r-project.org/web/packages/sp/vignettes/sp.pdf (specifically chapter 7).