Data Products - Leaflet

The goal is to create a web page using R Markdown that features a map created with Leaflet.

We are going to include the following as well including lots of other information 1. Featuring a Date less than two months before the date that you’re grading this assignment 2. Featuring an interactive map that appears to have been created with Leaflet

Have shown the date as well as the Interactive map using leaflet

Import packages

library(leaflet)
  1. Today’s Date is : 2024-02-29

My First Map using Leaflet. This is interactive where u can click to expand

my_map <- leaflet() %>% 
          addTiles()

my_map

Adding Markers - Showing Cisco San Jose Office - Interactive Map too

my_map <- my_map %>%
  addMarkers(lat=37.417077, lng=-121.933656, 
             popup="Cisco San Jose Office")
my_map

Adding many Markers

set.seed(233)
df <- data.frame(lat = runif(10, min = 37.1, max = 37.5),
                 lng = runif(10, min = -121.93365, max = -121.933656))
## Warning in runif(10, min = -121.93365, max = -121.933656): NAs produced
set.seed(2016-04-25)
df <- data.frame(lat = c(37.41,37.41,37.42),
                 lng =c(-121.93,-121.95,-121.92))
df %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers()
print(df)
##     lat     lng
## 1 37.41 -121.93
## 2 37.41 -121.95
## 3 37.42 -121.92

Adding Custom Markers To Our Interactive Map

hopkinsIcon <- makeIcon(
  iconUrl = "http://brand.jhu.edu/content/uploads/2014/06/university.shield.small_.blue_.png",
  iconWidth = 31*215/230, iconHeight = 31,
  iconAnchorX = 31*215/230/2, iconAnchorY = 16
)

hopkinsLatLong <- data.frame(
  lat = c(39.2973166, 39.3288851, 39.2906617),
  lng = c(-76.5929798, -76.6206598, -76.5469683))

hopkinsLatLong %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon = hopkinsIcon)

Adding Multiple Popups

hopkinsSites <- c(
  "<a href='http://www.jhsph.edu/'>East Baltimore Campus</a>",
  "<a href='https://apply.jhu.edu/visit/homewood/'>Homewood Campus</a>",
  "<a href='http://www.hopkinsmedicine.org/johns_hopkins_bayview/'>Bayview Medical Center</a>",
  "<a href='http://www.peabody.jhu.edu/'>Peabody Institute</a>",
  "<a href='http://carey.jhu.edu/'>Carey Business School</a>"
)

hopkinsLatLong %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon = hopkinsIcon, popup = hopkinsSites)

Mapping Clusters

df <- data.frame(lat = runif(500, min = 39.25, max = 39.35),
                 lng = runif(500, min = -76.65, max = -76.55))
df %>% 
  leaflet() %>%
  addTiles() %>%
  addMarkers(clusterOptions = markerClusterOptions())

Mapping Circle Markers

df <- data.frame(lat = runif(20, min = 39.25, max = 39.35),
                 lng = runif(20, min = -76.65, max = -76.55))
df %>% 
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers()

Drawing Circles

md_cities <- data.frame(name = c("Baltimore", "Frederick", "Rockville", "Gaithersburg", 
                                 "Bowie", "Hagerstown", "Annapolis", "College Park", "Salisbury", "Laurel"),
                        pop = c(619493, 66169, 62334, 61045, 55232,
                                39890, 38880, 30587, 30484, 25346),
                        lat = c(39.2920592, 39.4143921, 39.0840, 39.1434, 39.0068, 39.6418, 38.9784, 38.9897, 38.3607, 39.0993),
                        lng = c(-76.6077852, -77.4204875, -77.1528, -77.2014, -76.7791, -77.7200, -76.4922, -76.9378, -75.5994, -76.8483))
md_cities %>%
  leaflet() %>%
  addTiles() %>%
  addCircles(weight = 1, radius = sqrt(md_cities$pop) * 30)
## Assuming "lng" and "lat" are longitude and latitude, respectively

Drawing Rectangles

leaflet() %>%
  addTiles() %>%
  addRectangles(lat1 = 37.3858, lng1 = -122.0595, 
                lat2 = 37.3890, lng2 = -122.0625)

Adding Legends

df <- data.frame(lat = runif(20, min = 39.25, max = 39.35),
                 lng = runif(20, min = -76.65, max = -76.55),
                 col = sample(c("red", "blue", "green"), 20, replace = TRUE),
                 stringsAsFactors = FALSE)

df %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers(color = df$col) %>%
  addLegend(labels = LETTERS[22:24], colors = c("blue", "red", "green"))
## Assuming "lng" and "lat" are longitude and latitude, respectively