Skip to content
R for the Rest of Us Logo

Joining Geospatial Datasets (01_07)

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

Open project 01_07.

Use mapview() to visualise the results of the Brexit Referendum by combining shapefiles with the a .csv file containing the results.

  • Combine together the shapefiles for England, Scotland and Wales with bind_rows()

  • Import the referendum results .csv file

  • Use left_join() to join the UK shapefiles with the referendum results

  • Use mapview(zcol = "results") to visualise the shapefiles after joining

Learn More

As mentioned in the video, there are animated GIFs explaining how each of the mutating join functions work on the tidyexplain website. The Going Deeper with R course also covers the mechanics of joins in more detail.

It’s important to remember that the sf object must always go in the first argument of your joins, otherwise the geometry information will be lost.

It’s often necessary to disambiguate country names using a join and the countrycode package, which is covered in the next lessons.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

J. Jesús Bautista-Romero

J. Jesús Bautista-Romero

February 23, 2022

Charlotte: A comment: I first used "by = c("name" = "area_name"))" to join the objects in the left_join(), then I saw your video and you used the code columns to join them and the maps were a little different. I understand that some names are dissimilar between shape and csv objects and this result in NAs cells of the right side columns. by the way, amazing lessons, you teach very well

Regards.

Jesús

Hi Jesús, just a quick note to say that Charlie is sick this week, but will get back to you soon!

J. Jesús Bautista-Romero

J. Jesús Bautista-Romero

February 25, 2022

Hi David. So sorry about that news. I wish Charlie a quick recovery, a lot of good vibes.

Charlie Hadley

Charlie Hadley

March 3, 2022

Thanks for the well wishes, Jesús! This is a good example of how GIS datasets can be unexpectedly annoying. When you join using by = c(“name” = “area_name”))” there are 7 areas with NA values, you can see several of these areas as grey in the map. I'll concentrate on the south most, which has name = Cornwall,Isles of Scilly but area_name = Cornwall. Which means in the uk_sf object both the area of Cornwall and the Isles of Scilly are combined into one. That's very unexpected. I'd recommend if you have geographic codes to always use them if possible as these are much more likely to give you useful values. I'm really glad you're enjoying the course! Cheers, Charlie

Jay Cutler

Jay Cutler

March 14, 2022

Hi Charlie, I noticed that in your example, countries like the Republic of Congo were omitted from the final map because the text under 'name' (world_sf) and 'country' (gapminder_2007) were not the same. Do you have a process to match up the names of geographic units besides carefully reviewing datasets before joining and using mutate(case_when()) to fix inconsistencies? Thanks!

Jay Cutler

Jay Cutler

March 14, 2022

Well, looks like I just needed to watch the next class before sending this!

Charlie Hadley

Charlie Hadley

March 15, 2022

Ha! There's an argument for the "disambiguating country names" video to come first but to do it properly you need to know how to join first 😀

David Solet

David Solet

March 28, 2022

Hello Charlie, In the solution, I'm not sure where "brexit_votes" came from. To check, I ran the code and it resulted in an error, not found. Sorry if I missed something! (My result seemed to be fine.)

Charlie Hadley

Charlie Hadley

March 29, 2022

Hi David! Thanks for commenting, you caught a mistake! In the Gist solution it did erroneously use brexit_votes from an earlier draft but I changed my mind and used referendum_results later on.

I've updated the solution now so hopefully it won't cause any confusion for anyone else.

Thanks, Charlie

David Solet

David Solet

March 30, 2022

Hello Charlie, Thanks for the correction. Overall, I'm really enjoying the course! David