Skip to content
R for the Rest of Us Logo

Mapping with R

34 lessons

Joining Geospatial Datasets (01_07)

This lesson is locked

Get access to all lessons in this course.

If the video is not playing correctly, you can watch it in a new window


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



Jay Cutler

Jay Cutler

March 15, 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!

David Solet

David Solet

March 29, 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.)