R - stuck with plot () - polygons of a polygonal shape based on the value of the slot

I have a shapefile showing remote areas in Australia received from the Australian Bureau of Statistics:

http://www.abs.gov.au/AUSSTATS/ abs@.nsf /DetailsPage/1270.0.55.005July%202011?OpenDocument

At the same URL is the PDF file β€œASGS Remoteness Structure Edition 2011 PDF Maps” - I am trying to reproduce the first map from this PDF.

I read in the shapefile and added color information to the data slot:

 ra <- readShapeSpatial("RA_2011_AUST", delete_null_obj = TRUE) ra@data $COLOUR <- "#FFFFFF" ra@data $COLOUR[(as.numeric(as.character( ra@data $RA_CODE11)) %% 10) == 0] <- "#006837" ra@data $COLOUR[(as.numeric(as.character( ra@data $RA_CODE11)) %% 10) == 1] <- "#31A354" ra@data $COLOUR[(as.numeric(as.character( ra@data $RA_CODE11)) %% 10) == 2] <- "#78C679" ra@data $COLOUR[(as.numeric(as.character( ra@data $RA_CODE11)) %% 10) == 3] <- "#C2E699" ra@data $COLOUR[(as.numeric(as.character( ra@data $RA_CODE11)) %% 10) == 4] <- "#FFFFCC" 

The only thing left for me to do is build a map! That's where I got stuck ...

ra@polygons is a list of 35 polygons, each of which has an ID slot, which is an index for the ra@data data frame. So all I have to do is say plot() to find the color in ra@data $COLOUR[ID] . Well, not quite. Each of the 35 polygons (the Polygon class) has its own list of polygons (the Polygon class); Only 6902 polygons !!!

My understanding of plot() is that I have to pass it a color vector in the same order as polygons. Therefore, I believe that I will have to create a vector of length 6902 with each element containing the color value for the associated polygon. How am I doing this so far?

It would be easy enough if the polygons were constructed in order, but this is not so. Each of the 35 polygons has a plotOrder slot, which is an integer vector, so the color vector should apparently be ordered by the values ​​in each of these vectors.

At this point, all this seems too complicated. Am I completely disconnected here?

Thank you for your advice!

+7
source share
1 answer

You have already done everything!

 plot(ra, col=ra@data $COLOUR) 

Or even as @Spacedman suggested:

 plot(ra, col=ra$COLOUR) 

What is it!

enter image description here

And if you want to get rid of the borders of the polygon:

 plot(ra, col=ra$COLOUR, border=NA) 

enter image description here

Edit : An attempt to explain this behavior:

According to ?SpatialPointsDataFrame :

SpatialPolygonsDataFrame with matching identifiers by default checks the row names of the data frame on the polygon identifier slots. Then they must be consistent with each other and be unique (Polygons objects cannot share identifiers); rows of data frames will be redirected, if necessary, to match the identifiers of the polygons.

The value that the polygons are ordered according to their identifier and, therefore, are in the order of the rows of the data frame in the @data slot.

Now, if you look at the plot.SpatialPolygons function (using getAnywhere(plot.SpatialPolygons) ), at some point there are lines like this:

 ... polys <- slot(x, "polygons") pO <- slot(x, "plotOrder") if (!is.null(density)) { if (missing(col)) col <- par("fg") if (length(col) != n) col <- rep(col, n, n) if (length(density) != n) density <- rep(density, n, n) if (length(angle) != n) angle <- rep(angle, n, n) for (j in pO) .polygonRingHoles(polys[[j]], border = border[j], xpd = xpd, density = density[j], angle = angle[j], col = col[j], pbg = pbg, lty = lty, ...) } ... 

The vector entered in col is in the same order as the polygons slot and, therefore, as an ID. plotOrder used to index all of them in the same way.

+11
source

All Articles