3

I've been trying to add a column of numerical data from a dataframe to a SpatialPolygon dataframe but every attempt leads to the latter dataframe being converted to a standard dataframe similar to the former. I needed to add the column so that I can create a choropleth map with the column's variable as the focus. Obviously the standard dataframe is no good since I'm trying to create a map using tmap.

This is how I've been trying to add the column (where shapefilecomb is the spatial dataframe and wardturnout is the variable containing the column in question):

shapefilecomb <- c(wardturnout)
Uwe
  • 41,420
  • 11
  • 90
  • 134
LoriDori
  • 147
  • 2
  • 8
  • Try 'shapefilecomb$wardturnout <- wardturnout' – mdsumner Apr 19 '17 at 20:33
  • @mdsumner Thanks this worked in terms of adding the column, but now when I try to run 'tm_shape(shapefilecomb) + tm_fill("turnout")' i get the error 'Error: Fill argument neither colors nor valid variable name(s)'. the "turnout" variable contains the numerical data I want to be mapped onto the chloropleth. Any ideas? – LoriDori Apr 19 '17 at 22:34
  • I used "wardturnout", make sure the names are consistent – mdsumner Apr 19 '17 at 22:42

1 Answers1

6

Adding a column into data slot of SpatialPolygonsDataFrame by assignment operator shapefilecomb$wardturnout <- wardturnout works, but it is not the safest way to do the job. It relies only on position (first data item goes to first polygon, second to second and so on). It can get messy.

It is best reserved for calculated fields - the shapefile$valuepercapita <- shapefile$value / shapefile$population kind of assignment.

For data from external sources it is much better idea to assign value by key. Function append_data from tmap package does it very nicely, and gives you a message not only when error occurs, but also confirmation when all data was matched perfectly (which I found as a nice touch when working with large sets of imperfect data).

outShape <- append_data(srcShape, frmData, key.shp = "KOD_LAU1", key.data = "LAU1")

Edit (as of 9/2019): This answer seems to be still going strong... The world has changed though.

  1. tmap::append_data() has been moved to tmaptools::append_data() and is by now deprecated

  2. sf has replaced sp as the go-to package in spatial data in R

In the sf world spatial data are stored in modified data.frames, and the most appropriate way to assign data items by key is one of the *_join() functions from dplyr - either dplyr::left_join() to be on safe side, or dplyr::inner_join() if filtering on both sides is actually desired behavior.

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44