Background

The following is a very simple demonstration on using the Leaflet package in R in order to create custom, interactive maps. This assignment is part of the Data Products course under John Hopkins University’s Data Science Specialization. The prompt is simply as follows:

Create a web page using R Markdown that features a map created with Leaflet. Host your webpage on either GitHub Pages, RPubs, or NeoCities. Your webpage must contain the date that you created the document, and it must contain a map created with Leaflet.

Being an LA native, I figured I would map some of the most iconic locations of Los Angeles. Regardless of where you are from, im sure you may very well recognize or know about some of these places!

Loading in packages

library(magrittr)
library(leaflet)

Creating dataframes

Creating a dataframe named df containing the latitude and longitude pairs of all locations:

df <- data.frame(lat = c(34.134321663007796, 34.11839783971528, 34.10207104066168, 
                         34.10319200032858, 34.11218765245353, 34.06895468791129, 
                         34.08176907409841, 34.095153498021034, 34.05087632658836,
                         34.0082694486014, 34.055447291522576, 34.077844932261634,
                         34.13813083441205, 34.123444832695164, 34.056135830774956,
                         34.14038812911993, 34.062345110191416, 34.0417227397312,
                         33.944678397683376, 34.02231898160459, 34.043136197685314,
                         33.98444581336207, 34.101594443043794),
                 long = c(-118.32153825755006, -118.3003923274951, -118.34097319395559,
                          -118.32639642618466, -118.33917944358957, -118.40271042904106, 
                          -118.41343279555342, -118.37325795507714, -118.24908552426096,
                          -118.49878837526985, -118.24986221166493, -118.47470301669082,
                          -118.35354159240782, -118.38768707514237, -118.23662518009054,
                          -118.29313760640255, -118.23835397537668, -118.23295500159496,
                          -118.40999727934133, -118.28514586907941, -118.26723303433198,
                          -118.47105718502225, -118.33837748816529))

Creating a list containing the names of all locations, paired in the proper corresponding order as the df dataframe:

names <- c("Hollywood Sign", "Griffith Observatory", "TCL Chinese Theatre", "Capitol Records Building", 
           "Hollywood Bowl", "Rodeo Dr", "The Beverly Hills Hotel", "Sunset Boulevard", 
           "Grand Central Market", "Santa Monica Pier", "Walt Disney Concert Hall", "The Getty",
           "Universal Studios", "Mulholland Dr", "Union Station", "Griffith Park", "Chinatown",
           "Arts District", "LAX", "USC", "Crypto.com Arena", "Venice Beach", "Hollywood Blvd Stars")

Creating the map

To create the plot/map, we simply pass the dataframe to the associated leaflet functions, and specify the popup argument to our list of names:

df %>% leaflet() %>% 
        addTiles() %>% 
        addMarkers(popup = names)
It’s that easy! You can scroll to zoom and click on each marker to see the name of each location.