Transcript for:
Creating Maps with ggplot2 Tutorial

all right so welcome to our tips number 13 and this is one of the coolest tidy verse features which is making maps with ggplot so we're going to be going over how to make these amazing maps that you can really do a lot of cool stuff with these make great visuals for publications they make great visuals for reports so you're going to be focusing on learning ggplot and also working with map data which has a coordinate system usually identified by a latitude and longitude so we're going to show you how to make this map here which is for voting republican in 1976 and you can see which states voted democrat versus republican i'm going to show you how to do that all right so let's get started uh the first thing that you want to do if you haven't done so already sign up for the weekly r tips that'll give you access to the um the the github repo then what you'll do is you'll do a get pool and this will give you all the code so when you set that repo up you're going to have a new 0 1 3 gg plot 2 maps and these are all of the art tips in here we're going to go in here we're going to open up this file one three ggplot2 maps.r and that's going to open up this file here so that will be what you need to do just sign up for the rtips newsletter and you'll get access to it okay next thing we're going to load in some libraries so the first one is the tidy verse we're going to load the maps library and the map projection library tidy verse is what we're going to be teaching and that comes with ggplot2 and then you also will need to have maps which accesses the map data that we're going to be needing to create what's called the chloroplast so if i check out this plot over here it makes all of these map states and everything and then map projection and that's needed for to make sure that the projection looks correct you see how this kind of has a curve to it that is because we're using a coordinate system and a projection of that coordinate system okay first things first let's check out map data so this is one of the key functions that you're going to need to become familiar with it comes from ggplot2 and what this does is it allows us to access different map data sets and what it does is it pulls in some latitudes and longitudes and you can find out more information about the function here what we're going to be using it for first is creating a world map so i'm going to show you how to make a world map first it's just a basic map of the world using the map data function we're also going to be using ggplot to visualize this data that we that we pull in so ggplot is an amazingly powerful tool it's a skill that i cover in depth in ds4b101-r that's my foundational r course in in week four of that course you learn a ton about ggplot2 i think i go through about four and a half hours of video content just on gg plot tail so you want to learn that and that's going to be able to really help you with your skill set so we're going to first load in some data i'm going to use this map data and i'm just going to add a region called world and that's going to pull in all the data for the entire world and then we can view that data set and we see it's 99 000 by 99 338 rows by six columns and you can see it's got the latitude and the longitude it's also got groups in here and uh they're grouped by region and and so on in subregion okay the next thing is that we want to be able to plot that so we take our data set and we pipe it into ggplot just like normal and um this creates a blank canvas over here and then we're going to use this geom map which creates a geometry for a map now we're going to provide an aes which is a mapping so we're going to map the latitude and longitude so longitude is going to be along the x-axis latitude on the y and then you have to add a map id for the geomat to function properly and we're going to provide it this region right here so that's kind of like a group we also have a map and this is new a an actual argument in geomap called map and again we're going to provide it the world tibble which has the data set in here okay and then these are just a few aesthetic arguments to control what the map looks like so i'm going to call this world base and then we'll visualize it okay so this is what the map looks like now it looks a little weird because you can see that there's no projection to it so right now um there's no curve to it like the globe the of the world has a curve to it so we need to add something else to it in order to make this look right so if you're going to be putting this in a report don't put it in a report looking like this what you want to do is you want to take that world base and add a coordinate system and what we're going to be doing is using the orthogonal coordinate system and then i'm going to provide it in orientation and this is going to be the um again a coordinate system where this is the latitude the longitude and then that this is going to focus on the us so when i run this now i take that world base and now it looks like a world and i can focus in on different parts of the world and then i can apply my data set accordingly okay but typically what we want to do is we don't just want to focus on the entire world we usually want to look at a subset of the world so what we're going to be doing is then showing how you can use this map data function to focus in on the united states again we're going to be covering ggplot here and this is going to be doing basically what we did already for the world but just for the state at the u.s state level and and if you want to learn gg plot in detail i cover in my 101 course okay so uh what we're doing again here is using map data but this time focusing on the states and this usa table is what we save it as and this has again the latitude and longitude and you can see it's got the region in here and the group and we can see that there's a tibble here of now fifteen thousand five hundred and thirty seven by six so it's it's a lot smaller than the world tibble uh we're gonna go through the same process uh this time i moved the latitude and longitude in the map id up to the gt plot so before i had it as part of this geom map and the reason is is because eventually once we get down to making our next map we're going to want these latitude and longitude available to all of our different geometries so when you put it in ggplot2 this this latitude and longitude will get funneled to map and then it'll also get funneled to any other geometry okay so let's check this out we're just basically doing the same thing that we did for the world except now what we're doing is just highlighting the united states and the re and the way that that works is we now have usa tibble in here which is just the data for the united states and you can see the projection looks correct now if we did not include this coordinate map if we just do from here down you can see it'll look a little weird so the projections look off so you definitely want to have that chord map ortho and then put the orientation on the latitude and longitude that you're interested in okay so now we can move on now that we understand the basics of how to plot maps we're going to combine this with data and integrate data into our visualization because what we've been doing is just visualizing the states but we haven't actually added any data and this is what you typically want to do so what we're going to do is we're going to create the republican voting by u.s state and this is going to cover some key skills i cover them in my 101 course the first one is data wrangling with dplyr i cover that in weeks 2 and 3 of my 101 course and then there's also ggplot2 this is covered in depth in week four of my 101 course so definitely take that course if you really want to understand how these plots are developed okay first thing that we need to do we need to take we need to get some data and what i'm going to be doing is using from the maps maps library i'm going to pull out this vote repub which is every year from i believe 1856 all the way up through all of the elections all the way to 90 1976 they have a data set in here by state so what we're going to do is we're going to take that and we're going to do some data wrangling so it's not in the right format really what we want to do is we want to focus in on this 1976 which is the most recent data that this data set has um so first thing i do is i convert it to a table this table has some row names so i need to create a column called state next what i'm doing is just selecting the 1976 column here and also the state column so it kind of filters down the um the columns for me then i'm going to rename this column here republican proportion i do that and then i'm going to convert this from units to a percentage or a proportion do that by dividing by 100 and then what i'm going to do is overwrite the state column and make it two lower because that's what i need in order to match these regions uh when we do when we go to join so i'm gonna save this as republican voting tibble and then what i can do is i can take my usa table which looks something like this it's got a region column here with all the states and lower case and i can join it now on the state column because these are all lowercase so they should match up so we're going to use this left join function so again i teach this all in my 101 course in weeks two and three so now we have a u.s voting table where i've got the republican proportion for each one of these rows in here okay so now what i can do is i can make what's called a chloropleth map which is just coloring each one of these states in by the proportion that they vote republican we do this using kind of a similar process now i have a few additional options here that i've added in but the basics are the same so i'm going to take my usa voting table i'm going to use an aes of longitude and latitude for the x and y axis i'm going to provide a group equal to the sub region which is just this column right here okay ctrl enter and then what i'm going to do is i'm going to add my geom map with the map id equal to region and then we're going to do the orthogonal orientation so it looks correct so this is just all stuff that we've already done next what i'm going to do is i'm going to add a geom polygon and the aes is going to be group equal to group and fill equal to a republican so this is going to take from this usa voting table which looks something like this and it's going to take in the group which is this right here this column and it's going to fill based on this republican column so it's basically going to create a heat map this color equal to black will show i'll show you what that does so what i'd have is the outline for each one of these states equal to black so i can see them now this just gives you the standard color scheme which goes from dark blue to light blue and we want to change that so the next thing i'm going to do is i'm going to use a function called skill filled gradient 2 from low equal to blue the middle i want it to be white and at the high end i want it to be red so anything down here is going to be blue me and blue is the color for democrats red anything up here is going to be red and that's going to be for republicans and then in the middle which is at the 50th percentile that's going to be white so when i do this control enter i get something that looks like this so it goes from dark blue to red and you can see which states in 1976 were the most republican or democratic okay next i'm going to do a little bit of adjustment theme minimal that gets rid of some of the lines and such i'm going to add some labels so voting republican in 1976 and then i'm going to adjust the theme i'm going to give i'm going to make this a little bit bigger and color it red and then i'm going to put the legend position at the bottom here okay once i do that my plot is done and now i have a really cool voting republican in 1976 plot that you can you can definitely be proud of and you can and this is publication quality if you like this video don't forget to sign up for the tuesday free r tips newsletter you can just click this link here and it'll send you here put your email address in and every tuesday you'll get these videos you'll get the code and you'll get the tutorial right in your inbox