0

I have a SpatialPolygonsDataFrame (poly) what I would like to attribute values from another dataframe (df). In the poly@polygons slot are a hole bunch of polygons that have a unique ID. This ID corresponds to ID values in df. df has two types of columns. df$ID are unique IDs (that correspond to poly@polygons ID) and df$1988:df$2014 are columns designating the year. Below each year is the value in question.

Any thoughts on how to get that value, and really all the values for all years for a certain ID, into poly? The end goal is to visualize the polygons colored by the value, one for all years combined, and then a GIF showing the change in values from year to year.

1
  • Does poly@polygons$ID Yield the polygon ID's for you? Assuming a single ID occurs for many years that could be the way to get at it. Considering you have identified the ID's as unique I'm curious how you would know if the sequential years are the same polygon. Is there another variable to lets you know that the polygon is from a set of sequential polygons? Commented Feb 1, 2016 at 22:04

1 Answer 1

1

If I'm understanding the question correctly you want something like this:

newPoly <- merge(poly, df, by.x = 0, by.y = "ID")

sp has a merge method which should join the the spatial data.frame and the normal one as long as the spatial one is first. by.x = 0 specifies to join by row names which should correspond to the ID slots in the spatial polygon. After you've done this you can plot the the values for the various different years

using eithernewPoly$1998ornewPoly@data$1998`

I've never tried merging a spatial polygon using row names so I'm not positive it works, if it doesn't I would create a new column in the data slot of the spatial polygon like so:

poly@data$ID <- row.names(poly@data)

You can create new columns in data slot just be very careful about sorting or removing rows because it will mess up the plotting.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I got the merge function to work but that puts the values in the data slot, to necessarily attached to a polygon, other than an ID column in the data slot. When plotting there is no way, that I know of anyway, to refer to data and a polygons slots at the same time to produce a result. Maybe there is I just don't know how to do it.
I'm not sure I follow, you shouldn't be editing the polygons slot anything you want to plot should be put into the data slot. If you edit the polygon slot you risk messing up your shape file in a pretty serious way...There are lots of different ways to plot in R ggplot, tmap, base. All with different pros and cons. The simplest way is plot(newPoly, col = newPoly@data$1998 There are lots of resources for plotting on stackoverflow and around the interwebs.
The above would typically be used for a categorical variable, if you have a continuous variable you could cut it into bins or use a gradient. I don't remember how to plot with a gradient in base R off the top of my head I'm more of a ggplot person.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.