Leaflet Mapping in RStudio - Adding Markers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hey guys welcome back to the next video on creating a map in our or our studio so in the last video which I'll put a link to in the top right hand corner we went over installing a package the leaflet package loading that package in and then just the very basic components so creating a variable called leaflet and telling it that you wanted to add the map tiles the generic map tiles so the next step we actually want to add in some information or points to our map as in most people would I mean like it's nice to have this I guess but you would want your personal information or or whatever you're using this for your business your research anything you want to add your own information so I'm going to show you how to do that I have some data that I got that was it was just a just made up data it comes from a website called maca root comm I'll put a link in the description below you can you know you can put in any kind of variables a wide range of variables that you would like in a mock or pretend set of data and then it'll spit out all this fake data for you to just play around so you can use it in and maybe get used to something like this project that we're working ok so because of that we're going to need to use a different package that is already in it should come pre-loaded with our it's called the SP package and we're just going to add it to our current our current project so we just add another library function and we call the SP function this time I actually do have a sample bit of code that allows you to just list initially all the packages you want and it will it will install them and then load them in as well and it'll do it all automatically for you so you don't have to type out library function leaflet library function SP and because sometimes you could have like twenty to thirty packages in one script and you don't want to necessarily have to type that out every time anyway so this set of data is right here it's called mock data CSV okay so in order to bring that in I'm actually going to I'm going to write all this before the map because this is I guess this would be the step you would take probably before probably before you actually start writing your leaflet code like down here but the last video I just wanted to show you guys that you can do it and it's super easy so now that we've loaded in the SP I think I was good there we go now I have now we've loaded it and actually I'm going to need to reload in leaflet as well I can check my packages and type in leaflet it's been checked type in SP and see that that's been checked as well so that's good so what we'll need to do is bring in our CSV file so I'm just going to name a variable I'm gonna call it data okay and it's going to equal the I'm going to use the read CSV function okay and we're just going to call in we're just going to call it the actual file so the file name down here is mock underscore data all caps so mock on the score data and it's important to have the dot CSV or else it will not work again ctrl enter there it is cool thing now I can actually click on this little icon here and get a table view all the information and once again just to be clear this is completely fake data all made up with a random generator so don't worry about any kind of personal information being shared here so now we have this data table now and one of the issues with it is we actually have some missing values I wasn't going to include this but I feel like the people watching this video or using it as a reference you guys are going to get lots of lots of information but you're going to end up sometimes with data that has missing values okay and this is important to know how to deal with them and what you want to do with them so what we're going to do for this is I'm going to find there was one somewhere they're trying to find it on here so you guys can get the visual of it I just had that my square oh there we go so right here we have Kelly come on Kelly Arthur Kelly Arthur Kelly come on Arthur Kelly male lives in Xen hi Owen I'm reading the wrong one man uh Roy Howell alright Roy Howell did not give us the latitude and longitude whether that was him whether that was the program that was supposed to grab it or he had his privacy settings on blah blah blah we're able to use income but we couldn't get implication this can screw up your your data later on we need to make sure is you know what you're going to do with this are you going to try to find a workaround and try and just guess and just put in a lot of - longitude based on their city are you going to take that data away completely or is there another path that you are employer or whoever you've been contracted to work for or your professor or whatever you're doing is there a different plan for the purpose of this video we're just going to get rid of it so I'm going to create another variable I'm actually the name of the exact same thing so right now we're going to write over top of this you might not always want to do that you might want to make it like a data one so that you can always go back to this table later I'm just doing this because it has it's less complicated with the amount of with the amount of variables that you have in there and you're an environment okay so I'm going to call the previous data variable right because this one isn't in effect yet okay so I'm calling our variable data and I'm going to run the complete cases function and we're going to run it on data okay and you have to have a little comma here that's just one of those weird things I sure I could explain it to you or you can look it up if you like go ahead by all means but you'll just need that I'm going to hit control enter okay and we can see that there was a change so I'll go back here I'm going to hit read CSV again see how it says 1,000 observations okay and now I hit this again and there's two less okay so there was actually two people that had missing values so we got rid of those cases so we don't have to worry about that problem anymore okay so the next step we need to make this spatial data I mean we have in our table that it's out that there are lat latitudes and longitudes but we need to make this a data frame and make it a spatial data frame so I'm going to say that data lomg so long longitude and you can see here that's the name of the column okay is going to equal the as numeric function and it's going to equal data long this may seem kind of redundant but what this does is this ensures that that column is a number column not a text character column or something else that would confuse the computer down the road so run that so we're going to do the same thing for latitude okay so latitude as numeric numeric and data lat okay hold enter there we go the next thing I'm going to do is where this is where the SP package comes in we're going to name a new variable data dot FP okay what we're doing here is creating a new spatial point data frame and we're literally going to echo so spatial point data frame there we go so we're running this this we're going to run this function and inside of it we need to tell it where to find the spatial information from our table okay so we wanted to find the our coordinates inside of our data variable right and we're inside the data variable we're going to tell it that it's in columns eight and nine okay so that's the Latin long or I think that long would be long in line yep so Latin long so you want to make sure on your data table whatever it is that the I'm going to write eight and nine but you want the first number to be I'm going to put it inside of a there we go the first number is going to be a latitude and the second numbers going to be your long your longitude okay and then we're going to do it again so another comma and we're going to call data one more time okay and we're going to we're going to call the negative to this so negative see it just means a list from eight and nine okay so once again all we're saying is we're going to take our data table CSV it's just as she imagined just a sheet of paper right now so it does we're going to turn it into a magical spatial points data frame that will allow us to map things and where are we going to get that spatial data from well we're going to get it from column eight nine and the negatives of calling 1892 okay so these are kind of like the bounds like boundaries I'm going to run this line all right now you can see up here we have a value data SP that we want in a formal class a spatial point data frame that's perfect okay now we go back into our previously written code where we have em with leaflet and add tiles and I can run this and it'll be the exact same map that you guys saw before right nothing really there but I want to add in this data now okay so I'm going to run another pipette symbol okay control-shift-n for those of you who did not see the video the first video in this tutorial and we're just skipping ahead I know I've done pad with plenty of video tutorials before so don't worry [Music] and we're going to run a function called add markers okay and should pop up there for you inside of here we're going to list this is this might be a little bit confusing I'm going to hit a equals data right just to clarify this is a variable that will always be inside of the add markers function we mean our data data okay so if I named this Brent because that's my name and that's the variable name that I chose for it then I would go here and call this Brent Brent right so I just don't want you guys to get to to confuse with this you could do the data SP and we'll it should work either way but we created the special points data frame for future purposes and it won't really be used in this video now we need to tell it where to add the markers so we're going to run the LNG variable such as stands for longitude okay and we're going to make it equal to and this is where it gets nice and nice and easy you insert this symbol here it's usually beset the beside the number one on your keyboard and hit shift there and gives you that little little symbol and you have to take long because inside of this this variable we have a column of longitudes so it's just this symbol just means hey go through that entire that entire table and grab and make a point for every single longitude value that there is okay and we're going to do the exact same thing for latitude isn't sure that you get so flat equals left okay so the only thing I watch out for is you make sure that this is spelled properly from wherever it is you're referencing it in your table just happens to be one in mine you could have longitude you can have a latitude for this instead of lat you could name it whatever you want as long as you know that those are those represent longitude and latitude okay and the last thing we're going to do is I want the user to be able to click on my icon and get some information so I want them to get the information off of the first name of my data table and I'm only using first name because it's exactly how it's spelt there that's why it's spelt like this there's no special trick if the first name column was just labeled name I would literally just write name there okay so this should work I mean funnier things happened I'm just oh good nice little error there highlight that and hit enter you're an expression long sound you know what I might be wrong here let's see if this works there we go so yeah so just use your data variable we're not going to use the sp1 for now okay so we've created this now for the magic where we print our map again okay so let's go over have our cursor over the M and where's the a run that variable control enter and hey I this is a cluster of information but again this is what happens when you have like weird fake data but just to show you guys how cool this is I can go here I can click hey Christopher look at that Christopher alright there's Jonathan one is right there Kathleen so let's be cool now let's say I don't want that information up now I want to see I don't know I want to see income really simple okay you just replace this it's the name of the income column okay so I can hit run again it runs the whole thing nice and smooth and let's reprint our variables and map here it is and I'll just click as you can see is showing income in probably not the next video but the next few I'm going to get really get into how to style your map so right now I'm just showing you the basic functionality but probably probably the next couple videos I will show you guys how you can actually customize your markers themselves and customize the little pop-up window here that comes up because there's some pretty cool things that you can do for that as well anyway thank you for watching the second tutorial video in this series if you like the video please click like if you didn't like it you can hit this like but leave a comment in the comment box below and let me know what you guys thought also don't forget to subscribe and click the notification button so that you can know when the next video is posted right away okay see you next time
Info
Channel: Brent Thorne
Views: 13,526
Rating: undefined out of 5
Keywords: RStudio, Leaflet, Geomatics, Geomatechs, Coding, GIS, Technology, Tech, Tutorial, Add Markers, Markers, Add, Mapping, Digital Mapping
Id: dBk8gGX1MNk
Channel Id: undefined
Length: 19min 17sec (1157 seconds)
Published: Thu Feb 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.