R tutorial: Creating Maps and mapping data with ggplot2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so this video is going to show you how you can use r to create maps and then how you can fill in the different countries on those maps according to some data it's a relatively straightforward process and we only need to do two pack use two packages to do the basics of this gg plot two and tie diversities cleaning of the data before we analyze it of course you only need to do that once once those packages installed you've got them but you do need to pull them out your library for each session what i'm going to do is actually um do some maps based on vaccination rates for coronavirus so i've got the data and for vaccination rates for european countries and i'm going to create a data frame that's called eu vax and i'm going to read in my css v file remember when you're reading the csv file you need to change the path to where it sits on your computer and this data can be found in the link below so i'm just going to run in my csv file and this is what the data looks like so we've got the region and we've got percentage vaccinated so what we need to do is get some coordinates for the essentially the entire world so what we can do is we can use gg plot 2 and to get map data for the entire world so we're going to create a data frame called map data and this is going to get map data for the entire world if we run that let's view that map data so basically this gives us longitude and latitude to map borders of every country in the world and include some sub-regions as well of different countries so this is a huge spreadsheet so what we need to do is now is combine this data frame with this data frame so i'm just going to going to still call this one map data what i'm going to do is i'm going to join using this left join command the map data so this world map data with my euvax data and i'm going to use the column region which i have labeled in the country here and the country here in both data sets so now if you view my map data we can see i've just added a new column essentially that's percentage vaccinated and you can see it's blank for all the countries that i don't have in my eu vax data set so if i was to just map this now it's going to be i'd give me the entire world but there's going to be loads of blank places just great places so what i'm going to do is i'm going to create a new data frame that's called map data 1 and i'm going to filter map data and basically what i'm going to say is i'm going to remove anyone that is n a which has no data in my map data data set for percent vaccinated so this command here basically says get rid of anyone with n a four percent vaccinated in the map date to data frame so if i run this now and then i view my new data frame i've called map data one you can see now the regions are only ones that were also included in my eu max data set so now i'm only going to be mapping people i have the percentage of vaccinated data for so then i need to make a map or making a maps actually quite straightforward so what i'm going to do is i'm going to call it map1 and i'm going to use ggplot my ggplot command and i want the data frames used to be map data one which is my filtered data my aesthetics i just want the x to be long y is lat what that means is i want you to plot the longitudes and the latitudes so this is basically all these countries have loads of longitude and latitude which allows me to draw the shape of the country i have to have group equals group written in here as well this just makes sure all the countries fit together neatly you can see in map data one as well i've got group here so this just makes sure that my countries that i'm sort of plotting on my map are all neatly fitted together and then i ask for my polygon and basically what i'm saying is i want you to fill my aesthetic aes mic so i wanted to be filled according to percent vaccinated and color equals black just means it's going to do a black line around these polygons around the colored in countries so everything all the countries can have a black line around them so they're clearly separated from each other it will automatically give me a gradient for this fill as well so let's run that let's view map one zoom in on that there you go here's my map so as you can see it does a sense vaccinated we're color coded here the uk is doing the best here now you'll see we're missing countries so for like likes of albanians and montenegro is not there for example as well and that's because we haven't got data in that eu data set for it that's why they're excluded you could include them as well and in another way but i'm just going to show you how to map the things we've got data for so as you can see this has produced our percentage vaccinated so we can see the lighter countries doing a bit better the darker ones are doing a bit worse and so on we can make some alterations to that map and relatively straightforward i'm going to show you how to do that i'm going to create a new map which is called map 2 and so i just read in map one so reading the map i've created i'm going to change my scale fill gradient so how it colors things in and i'm going to change its label to percent vaccinated so rather than the column title of perk underscore vaccinated it's going to say percentage vaccinated low scores are going to be yellow high scores are going to be red i've got no information for that given country it's going to be great and then this i'm going to get rid of the text on the axis i'm going to get rid of the little ticks little marks on the axis i'm going to get rid of the axes titles as well and i'm going to get rid of the rectangular background as well to create map number two there's map number two so you can see changed my colors got rid of the longitude latitude label because it's not like really useful and now we can see which countries are doing better than waste and so on as well now of course that essentially does it for you and you can play around with colors you know you just look at gg plot for the color palettes and so on you could change variable names and so one other thing i'll just quickly do and is that you may wish to get a little picture a little logo of your research group whatever in the corner of a map so this can be done relatively straightforward using a package that's called cow plot so again you'll need to install the package and pull out the library each time you use it so what i'm going to do is i'm going to add a logo to this map let's do that i'd use this command here just write gg draw and then add and then draw image so i'm going to basically take this image from my desktop to picture my dog bill this is the position of it on the x axis the position on the y axis of the map so if you view it as having an axis right in the middle there and that's the size of the picture so i can change the picture scale i can make it bigger or smaller and then i say draw plot map two so that's just gonna add this to my map as well so again you need to make sure the picture you're going to put on it has the right location so i just click that and run that and it takes a little moment but then there we go here's my logo of my dog pill smiling of course you may have a different logo that you wish to put on your map and we you know we can adjust this so if i change that to 0.32 for example run that and you can see bill's gonna move across a little bit so three five let's move them across a little further there we go lovely so there's my little logo in the corner so you can adjust the position you can move it up and down and so on and we could make them bigger or smaller just using that scale flap there so we're just simply adding it to map to so that's how we could add a little logo to it as well so that's basically it and this is the face of a series of videos i'm going to do based on some maps i'll show you how to do animated maps and so on a bit further down the line but hopefully you can see how really you can produce this map with this little bit of code really is all that's really required once you've stitched together a few data sets and luckily you know with the map data command everything's automatic for you as usual the data and the code for this are found below the video and if you enjoy this please follow the channel on youtube for lots of other helpful r tutorials as well as things for spss and jasp as well
Info
Channel: Statistics Guides with Dr Paul Christiansen
Views: 8,092
Rating: 4.8993711 out of 5
Keywords: Rstats, Rstudio, covid, ggmap, ggplot, ggplot2, mapping data, maps, vaccine
Id: AgWgPSZ7Gp0
Channel Id: undefined
Length: 10min 30sec (630 seconds)
Published: Tue Mar 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.