Lecture Notes of Leaflet

Beginning to show a Map in Leaflet

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

Adding Markers

library(leaflet)
my_map <- leaflet()
my_map <- addTiles(my_map)
my_map <- addMarkers(my_map,lat = 39.2980803, lng = -76.5898801,
             popup = "Jeff Leek's Office")
my_map

Show Maltiple Points on a map

library(leaflet)
df <- data.frame(lat = runif(20, min = 39.2, max = 39.3),
                 lng = runif(20, min = -76.6, max = -76.5))
df <- leaflet(df)
df <- addTiles(df)
df <- addMarkers(df)
df

Add Icons on A Map

library(leaflet)
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, 39.2970681, 39.2824806),
    lng = c(-76.5929798, -76.6206598, -76.5469683, -76.6150537, -76.6016766)
)

map = leaflet(hopkinsLatLong)
map = addTiles(map)
map = addMarkers(map, icon = hopkinsIcon)
map

Show Clusters on A Map

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

Add Legened on A Map

library(leaflet)
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(df)
df <- addTiles(df)
df <- addCircleMarkers(df, color = df$col)
## Assuming "lng" and "lat" are longitude and latitude, respectively
df <- addLegend(df, labels = LETTERS[1:3], colors = c("blue","red","green"))
df

Show Population Scale on A Map

library(leaflet)
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))

a = md_cities$pop
md_cities <- leaflet(md_cities)
md_cities <- addTiles(md_cities)
md_cities <- addCircles(md_cities, weight = 1, radius = sqrt(a) * 30)
## Assuming "lng" and "lat" are longitude and latitude, respectively
md_cities

Create A Rectangles on A Map

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