22 / 6 / 2018
library(leaflet)
my_map <- leaflet() %>% addTiles()
adding markers
my_map <- my_map %>%
addMarkers(lat = 39.2980803, lng = -76.5898801, popup = "Jeff Leek's office" )
# may_map <-addMarkers(my_map, lat = 39.2980803, lng = -76.5898801, popup = "Jeff Leek's office" )
why is piping important
my_map <-
leaflet() %>%
addTiles() %>%
addMarkers(lat = 39.2980803, lng = -76.5898801, popup = "Jeff Leek's office" )
my_map
## adding many markers
set.seed(2016-04-25)
df <- data.frame(lat = runif(20, min = 39.2, max = 39.3),
lng = runif(20, min = -76.6, max = -76.5))
df %>%
leaflet() %>%
addTiles()%>%
addMarkers()
## Making Custom Markers
hopkinsIcon <- makeIcon(
# you need icons !! not images
iconUrl = "https://thenounproject.com/term/rose/",
iconWidth = 31*215/230, iconHeight = 31,
iconAnchorX = 31*215/230/2, iconAnchorY = 16
)
hopkinsLatLong <- data.frame(
lat = c (39.273166, 39.3288851, 36.2906617, 39.2970681, 39.282406),
lng = c (-76.5929798, -76.6206598, -76.5469683, -76.6150537, -76.6016766)
)
hopkinsLatLong %>%
leaflet() %>%
addTiles()%>%
addMarkers(icon = hopkinsIcon)
clustering maps
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
df %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers()
draw shapes
md_cities <- data.frame(name = c ("A", "B", "C", "D", "E", "F", "G", "H"),
pop = c (619493, 66169, 62334, 61045, 55232,
39890, 38880, 30587),
lat = c (39.2920592, 39.4143921, 39.0840, 38.9748 ,39.273166, 39.3288851, 36.2906617, 39.2970681),
lng = c (-76.6077852,-77.4204875,-77.1528,-77.1528,-76.5929798, -76.6206598, -76.5469683, -76.6150537)
)
md_cities %>%
leaflet() %>%
addTiles() %>%
addCircles(weight = 1, radius = sqrt(md_cities$pop) * 30)
## Assuming "lng" and "lat" are longitude and latitude, respectively
rectangles
leaflet() %>%
addTiles() %>%
addRectangles(lat1 = 37.3858, lng1 = -122.0595,
lat2 = 37.3890, lng2 = -122.0625)
different colors & 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[1:3], colors = c ("blue", "red", "green"))
## Assuming "lng" and "lat" are longitude and latitude, respectively