5

I have a regional shapefile of England and Wales but without containing Scotland and Northern Ireland.

Regions

I want to add the outlines of Scotland and Northern Ireland in without regional divisions so the combined would consist of the following polygons:

  • London
  • North-West
  • North-East
  • East of England
  • East Midlands
  • West Midlands
  • South-West
  • South-East
  • Yorkshire & The Humber
  • Scotland
  • Northern Ireland

I have a separate shapefile with just the country border of Scotland and Ireland - my question is it possible to add this to my regions map and if so how can I do so in R?

enter image description here

2
  • 2
    Are you using the sp package classes for your data or the newer sf classes? Do you care about attributes of your regions (like names, populations etc)? Have you tried reading them in and using rbind because these things should behave like data frames.
    – Spacedman
    May 25 '18 at 13:12
  • @Spacedman I'm using sp still. My shapefile doesn't contain many other attributes - those come from a spatial points file which I can then join on later if that makes sense?
    – Henry Dark
    May 25 '18 at 13:44
5

You can use rbind to combine spatial data objects:

Read in a couple of shapefiles:

> la = readOGR(".","la")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "la"
with 12035 features
It has 4 fields
> pr = readOGR(".","pr")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "pr"
with 12786 features
It has 3 fields

Try using rbind:

> lapr = rbind(la,pr)
Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

This has failed because there's a different number of columns in each shapefile - you can't put data with four columns (fields) with data that has three columns:

> names(la)
[1] "POSTCODE" "UPP"      "PC_AREA"  "N"       
> names(pr)
[1] "POSTCODE" "UPP"      "PC_AREA" 

Fix this some way. I'll remove the N column from la:

> la$N=NULL

And now it works:

> lapr = rbind(la,pr)

Giving me an object with the total features:

> dim(lapr)
[1] 24821     3

So you do need to make sure your objects have identically-named columns, which you'll have to do by removing, adding, or renaming columns. Then you can rbind them together.

7
  • Hi @Spacedman, just seen this - amazing. Could you expand a bit on how to get past the non identical columns issue? My two shapefiles appear to have entirely different columns in the data slot. Do I just need to create dummy columns with the same name in each shapefile so that they match?
    – Henry Dark
    May 29 '18 at 10:53
  • 1
    Something like that. If there are columns you don't need, delete them (assign to NULL) and if there are columns with similar names for the same thing (eg "NAME" and "Name") then set the names to the same in one or the other, and if there are names that make no sense in one but you want to keep in the other then create a dummy.
    – Spacedman
    May 29 '18 at 11:08
  • Worked like a charm @Spacedman! One more question if you don't mind - can I delete attributes from a shapefile in the same way as I can delete columns? To explain, I wanted to find a northern ireland coastline shapefile but can't seem to find one without a local breakdown. What I may have to do is get one including the 4 provinces - Ulster, Leinster, Munster & Connaught, and just delete all but the first - can this be done simply?
    – Henry Dark
    May 29 '18 at 14:24
  • You mean delete features? Yes, do: ulster = counties[1,]
    – Spacedman
    May 29 '18 at 14:35
  • Following your fab advice I was able to stich together N.Ireland with Scotland, Ireland and Wales! The only thing I'm not sure about is why, in the list of features, there are like 15 for Scotland and 1 each for the english counties - my last question is will this matter when I go about choroplething this map? I've posted an image of what I mean above @Spacedman
    – Henry Dark
    May 29 '18 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.