3

How does one colour in a subset of regions (e.g. only "Magugu" and "Nkaiti) as identified by the $Name_3 field in a spatial polygon? I played around with plot() and a subset-vector, but without success. Many thanks in advance, W

Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 2800 obs. of  14 variables:
  .. ..$ PID      : int [1:2800] 97867 97868 97869 97870 97871 97872 97873 97874 97875 97876 ...
  .. ..$ ID_0     : int [1:2800] 225 225 225 225 225 225 225 225 225 225 ...
  .. ..$ ISO      : chr [1:2800] "TZA" "TZA" "TZA" "TZA" ...
  .. ..$ NAME_0   : chr [1:2800] "Tanzania" "Tanzania" "Tanzania" "Tanzania" ...
  .. ..$ ID_1     : int [1:2800] 12 12 12 12 12 12 12 12 12 12 ...
  .. ..$ NAME_1   : chr [1:2800] "Manyara" "Manyara" "Manyara" "Manyara" ...
  .. ..$ ID_2     : int [1:2800] 52 52 52 52 52 52 52 52 52 53 ...
  .. ..$ NAME_2   : chr [1:2800] "Babati" "Babati" "Babati" "Babati" ...
  .. ..$ ID_3     : int [1:2800] 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 ...
  .. ..$ NAME_3   : chr [1:2800] "Magugu" "Mamire" "Mwada" "Nkaiti" ...
  .. ..$ NL_NAME_3: chr [1:2800] "" "" "" "" ...
  .. ..$ VARNAME_3: chr [1:2800] "" "" "" "" ...
  .. ..$ TYPE_3   : chr [1:2800] "Division" "Division" "Division" "Division" ...
  .. ..$ ENGTYPE_3: chr [1:2800] "Division" "Division" "Division" "Division" ...
  ..@ polygons   :List of 2800
plannapus
  • 18,529
  • 4
  • 72
  • 94
user1885116
  • 1,757
  • 4
  • 26
  • 39
  • See this question: http://gis.stackexchange.com/questions/36877/how-do-i-change-the-polygon-fill-color-and-border-color-for-spatialpolygons-obje – Ari B. Friedman Dec 07 '12 at 10:47
  • Thanks - and apologies for potentially not picking up the obvious - but i understand to colour them "at random" (e.g. plot(x, col=1:3), but not how to pick out and colour specific regions... – user1885116 Dec 07 '12 at 17:54
  • Additionally, [this question](http://stackoverflow.com/questions/14745815/r-stuck-with-plot-colouring-shapefile-polygons-based-upon-a-slot-value) covers this issue as well. Essentially the order of the color vector corresponds to the order of the rows of the @data slot, which in turn correspond to the ID of your polygons. – plannapus Mar 06 '13 at 09:00

1 Answers1

0

The base graphics plot() deals with colors the same way all base graphics objects do: you pass it a vector of colors. The vector has one element for each element of your SpatialPolygonsDataFrame, and the element content is the color.

See this question for how to plot colors in base graphics in general: Plot with conditional colors based on values in R

And this question for the specific application to SPDFs: https://gis.stackexchange.com/questions/36877/how-do-i-change-the-polygon-fill-color-and-border-color-for-spatialpolygons-obje

Community
  • 1
  • 1
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235