R Tutorial: Plotting DataCamp HQ

Learn to build interactive maps in R using the leaflet package. The tutorial covers adding markers to pinpoint locations, supplying coordinates as vectors or data frames, and enriching markers with pop-ups. Maps automatically center on single markers or fit bounds for multiple locations.
Chapters
We've been creating maps with a single layer, a basemap. To plot data on our maps, we can add layers to the basemap similar to how you add a layer in ggplot2. One of the most common layers to add are location markers, which you can add by piping the result of add tiles into the addmarkers function. There are several options for supplying the data for your markers. We'll focus on two approaches, using numeric columns from a data frame and using numeric vectors with a length of one. For example, if we plot datacamp's New York headquarters by passing the coordinates to add markers as numeric vectors with one element, our web map will plot a blue pin. You may have noticed that our map is zoomed in, but we didn't use set view, or fit bounds. When you add markers to your map without setting the view, leaflet will automatically set the boundaries of the base map based on the markers that you're plotting. If you're plotting a single marker, leaflet will center the map on that marker. If you're plotting multiple markers, leaflet will set the bounds so that they're all visible. If you want to add multiple markers, we can use a data frame or a tibble to pass the coordinates to the addmarkers function. For example, to plot both datacamps New York and Belgium offices, we can use a tibble with the coordinates in our addmarkers function. Call. An alternate approach to mapping points from a dataframe is to pipe the data. Then Add Markers will search the column names for names that are most likely your coordinates. The leaflet package will send you a message to let you know if a match was found. It's often helpful to provide users with information about the points we've mapped. A common way to do this is by adding pop ups that will appear whenever a user clicks on a marker. We can add popups by specifying the popup argument in the addmarkers function. For example, we can have the text from the HQ column appear whenever a marker is clicked. For this example, with just two markers, it may be more useful to add pop ups to the map without the markers. We can do this by replacing the addmarkers function with the Add popups function. If we had more than a few markers, this approach may be problematic. Once we have our map shaped up to our liking, we can store it in an R object, similar to how you store ggplots as R objects. You can add and edit layers of a leaflet map once it's stored as an object for example, we can store a base map with a particular view as an object called M. Then we can pipe M into the Add Markers function to plot Datacamp's office on the map. Now it's your turn to create a leaflet map that plots Datacamp's headquarters.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







