R Maps: Beautiful Interactive Choropleth & Scatter Maps with Plotly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you've ever tried making a bap in r you might be overwhelmed by the sheer number of packages that exist these packages may look completely different and take in differently shaped data so it can be pretty confusing to figure out which one to use today i want to show you how to quickly and reliably make beautiful maps in r just like these with the plotly package if you see my other videos you might remember i created one on interactive graphs with plotly it's a fantastic package that's well maintained and can produce impressive visualizations just out of the box so let's get started the first map we're going to be making is this interactive choropleth map of the us we've got different u.s states colored by different minimum wages and we have this slider at the bottom that allows us to change the year our first step is going to be loading in our packages plotly of course is going to help us build out these interactive maps dplyr is going to help us with some data cleaning and data wrangling and the reader library for reading in our csv files now let's load in our data i'm using this data set i found online of minimum wages by states by year but we're only going to be looking at a few of these columns so here i'm reading in my data set and i'll go ahead and run this just so we can take a look we've got year state and wage but we actually need to do one more thing to this data frame the way the choropleth maps and plotly work for u.s states when you're telling plotley that you want to color a certain state like alabama you actually need to use the state abbreviation the two-letter abbreviation so i actually have to go and find a different data set online with just states and their abbreviations just so i could join the two tables together so let me do that really quick we'll read in our csv file called states and we'll include an inner join with our minimum wage data frame and states and instead of selecting the state name we're actually just going to select the state code and we can run all of this so now we've got our year our state code and our wage so let's move on to step two which is actually making our graph we're first going to start by creating a new variable called min wage graph and we're going to call the plot geo function this is part of the plotly library we need to specify our data so that's going to be min wage df we also need to specify a location mode in this case we're looking at u.s states so we'll put in usa states and essentially by doing this that'll tell plotly that our geographic data is going to be using those state codes and in order to get that slider at the bottom with all the different years we need to specify the frame by the year column and just note that anytime you're calling one of these columns in the plotly functions you have to put in the tilde for it so we're done with the plot geo function we're going to pipe this into add trace which is just adding this trace layer onto our graph we're going to specify the actual states so the variable is locations and the column name is code which again has our two letter state codes we also need to specify a z and a color and in this case it's going to be our wage because that's how we want the states to be colored by the different wages and once we've done that we can go ahead and run this and now output our min wage graph and you see we get this map of the world we've got our slider on the bottom and because it's plotly we can zoom in and of course hover over our data points to see more granular data but this graph is far from complete so let's make a few changes we first want to make sure that we're showing just the us and not the whole world so this is pretty easy we just need to add another layer onto our plotly map this one is going to be called layout and we're going to set the geo variable to a list and the list is going to have a variable called scope which is set to usa and i know this is a little confusing but bear with me we can run all of that again and now we just have the us there are actually different types of scopes and different projections that i'd recommend you look at in the documentation but you can get some really cool looking graphs in plotly the next thing we want to do is mess with the actual colors that our map is you'll notice that if we change the frame the scale of the wages actually changes too so it starts with the max at 2 and by 2017 we're at about 11. in order to fix that we just need to set a min and max value for our wage that'll be reflected in this legend over here so in our add trace we can set a z min as zero and a z max instead of manually specifying it i'm going to call the max function and we want the highest value in this wage column so that'll just be min wage df wage i also want to get away from the default color palette so i'm going to set our color scale down here to the electric color palette we'll run all this again now you can see the legend has the wage from zero all the way to the max and our color palette is a bit different the other thing i noticed was that the hover data isn't super informative when i hover over each of these dates i want it to show me the state name and the dollar value but with an actual dollar sign so what i'm actually going to do first is create a new column as part of our min wage data frame so i'll call the mutate command from d plier and i'll make our variable name called hover i'll use paste 0 so i can concatenate some variables together and i want to concatenate the state name so i'm actually going to call state but i need to make sure i specify it up here first and then a new line and then the dollar sign and then our wage variable and i'll run all of this and just show you what our data frame looks like now so now our hover variable has alabama and then a new line character that you can actually see and then the wage now we just need to get that column to appear when we hover over these data points in order to do that we just go back to our add trace we pass in another argument called text and this is going to be again a column which is hover we can go ahead and run that now we get zero montana zero dollars and mt so we still have too much data being shown we just need to make sure that the hover info variable is only showing our text that's specified here and this is kind of a weird way to do it to have to specify the text and hover info but i haven't found a better way to do it so let me know if you do if we run all this now when we hover over our states we only see the relevant data and again as we cycle through the frames this data will update and change the labels so functionally i'm pretty happy with this graph but i still think that there are some aesthetic changes i want to make i want to change up the font of the graph and also the style of the labels and i want to get rid of this toolbar at the top and maybe even add a title so let's start with the font and the labels we can change the font by going back down to our layout function and passing in the font the family i'm going to use is dm sans and this is just a font that's installed on my computer if you want to see all the fonts you can use all you need to do is called library extra font and of course install it if you don't have it and then you can go ahead and call the fonts command and this will show you any of the fonts that you can use so this will change the fonts of the graph but i also want to change the label style so i'm actually going to make some variables up here the first is going to be called font style and again we're going to specify our family as dm sans just so it matches the rest of our graph we're gonna make the size 15 and the color of the label is going to be black we're also going to make a label variable where the background color is going to be this hex value the border color is going to be transparent the font is going to be the font style that we just created up there we can run all of this and now just to make sure that our plotly graph is using this information we need to add another layer called style where our hover label is equal to this label variable that we specified up here and if we run all of this to refresh our graph now you can tell it's subtle but the font has changed and if i hover over it's now this white box with no border and our dm sans font so let's also get rid of this toolbar up here this is also pretty easy we just need to add a config layer and set display mode bar to false and if we run that we can see that it doesn't appear when i hover over let's add in a title and that'll just be in our layout with the title variable we'll call this minimum wage in the us this is from the year 1968 to 2017 so i'll just specify that and lastly wage right now these numbers might not be very obvious so let's just add a dollar sign before them and we can do this with the help of another layer called color bar where we set a tick prefix to the dollar so let's run all this just to see where our graph is and it looks pretty good to me we've got our legend updated the labels look good title looks good and our visualization is actually pretty insightful so let's move on to our next example this one is going to be a scatter plot on top of a map so we're plotting longitude and latitude data as opposed to state or country data and unlike before we're not going to use any of the interactive capabilities that plowly has we're just focused on building a very basic scatter map so we'll start by reading in our data and just selecting some of the columns so i can give you guys a good view of what this looks like so we've got about 88 000 rows of data so a lot of data points with a longitude a latitude a day time and shape and i don't think we'll be using these two columns since we're only plotting the longitude and latitude but just to give some more context and again we'll create our ufos graph we'll call that plot geo function with our data frame and now we specify a latitude and longitude with the lat and long variables so our latitude is our latitude column and our longitude is our longitude column and again these are just going to be the column names that are in your data frame over here now if i run this because there are 80 000 points then plotly wants to give me an interactive graph it's actually going to take a long time and make my computer slow so i'm first going to build my graph just based on a thousand data points and then when it looks good i'll build out the whole thing using all the data so i'll just pass in sample n to grab a random sample of a thousand rows from this data frame and we can output the graph as well so let's run all that and we get a similar map like we had last time where by default it zoomed out to show the whole world and just like before we're going to make a lot more visual changes to this so the first thing is going to be making the scope just the us before we just specified geo equals list scope usa but the geo variable actually can take in a lot of different arguments to change the different colors of land masses and borders so we're going to create a separate variable called geoproperties that's going to be equal to a list and again the scope is going to be usa i was also talking about how there are different types of projections and just to show you what that's like we can specify a projection type i'll use albers usa i'm going to set show land to true show subunits to false so in this case it won't show state outlines land color is going to be gray 10 which is like a dark gray show lakes is true and we're going to make the lakes white and then just like before we need to call the layout layer with geo is now going to be geo properties and let's run all this just to take a look so we've got just the us now with these blue dots plotted but there are a couple other changes we need to make i want to make these blue dots a bit smaller make them a different color and obviously get all 88 000 of them instead of just the 1000 i also need to make sure that before plotting all those 88 000 i need to disable this hover because plotly will lag a ton and i might also just remove this toolbar again just to make this look a bit more cleaner so we'll start by customizing these little dots and that's not too difficult we just need to pass in another variable called marker that takes a list the size of each point i'm going to do is 2 which is pretty small and will look good with a lot of data points i'm going to make the color almost a light yellow and i'm also going to set the opacity to 0.25 just so we get the cool effect where we can see different clusters based on a lot of data points being plotted there right now it's a little tough to see because the data points overlap each other to get rid of the hover info we actually add another layer called add markers here we're explicitly specifying them but by default they're already plotted we can specify them just to put in more properties so we'll say our hover info is none and to get rid of the toolbar up here we just need to copy this display mode bar equals false and i know that there are a lot of different layers a lot of different variables that i'm just randomly seem to be passing in and that is kind of the case you do have to go into the documentation to figure out exactly what you want to do but once you have a decent map that you think looks well you can typically reuse a lot of those properties and layers each time so i think it's worth learning some of these different useful layers that plotly has so before we run all this let's just get rid of our sample n so we're plotting all the data and we can run it if i open it up in my browser it looks a bit better you can see because of that opacity that we set we can see those clusters of where these big ufo sightings are and i think this is a pretty cool visualization so obviously these two examples i did aren't a very comprehensive view of all of plotly's features and again i'd recommend taking a look at the documentation to really fine-tune your plots the way you want them to look but i'm hoping that i could show you how you could quickly make some pretty good looking plots and if you found this video helpful in any way i definitely recommend checking out these other videos right here
Info
Channel: dataslice
Views: 10,046
Rating: 5 out of 5
Keywords: scatter maps r, interactive choropleth map, interactive maps r, r maps, r choropleth maps, plotly using r, plotly maps, r plotly, interactive ggplot2, how to make interactive graphs in r, plotly r graphs, interactive graphs in r, plotly, make interactive graphs in r, ggplotly, ggplot2, dynamic graphs in r, how to embed plotly graphs, r interactive graphs, r programming, interactive dashboards in r, rmarkdown, r markdown interactive, shiny in r, interactive dashboard r, leaflet
Id: RrtqBYLf404
Channel Id: undefined
Length: 12min 21sec (741 seconds)
Published: Thu Oct 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.