library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(leaflet)
LLeaflet is the leading open-source JavaScript library for mobile-friendly interactive maps. Weighing just about 39 KB of JS, it has all the mapping features most developers ever need.
Leaflet is designed with simplicity, performance and usability in mind. It works efficiently across all major desktop and mobile platforms, can be extended with lots of plugins, has a beautiful, easy to use and well-documented API and a simple, readable source code that is a joy to contribute to. Leaflet is designed with simplicity, performance and usability in mind. It works efficiently across all major desktop and mobile platforms out of the box, taking advantage of HTML5 and CSS3 on modern browsers while being accessible on older ones too. It can be extended with a huge amount of plugins, has a beautiful, easy to use and well-documented API and a simple, readable source code that is a joy to contribute to.
For more info, docs and tutorials, check out the official website. For Leaflet downloads (including the built master version), check out the download page.
We’re happy to meet new contributors. If you want to get involved with Leaflet development, check out the contribution guide. Let’s make the best mapping library that will ever exist, and push the limits of what’s possible with online maps!
YTo make a custom icon, we usually need two images — the actual icon image and the image of its shadow. For this tutorial, we took the Leaflet logo and created four images out of it — 3 leaf images of different colors and one shadow image for the three:
library(leaflet)
library(dplyr)
my_map <- my_map %>%
addMarkers(lat=39.2980803, lng=-76.5898801,
popup="Jeff Leek's Office")
my_map
columns lat and lng you can pipe that data frame into leaflet() to add all the points at once.
Thankfully you can make your own markers from .png files.
hopkinsIcon <- makeIcon(
iconUrl = "http://brand.jhu.edu/content/uploads/2014/06/university.shield.small_.blue_.png",
iconWidth = 31*216/230, iconHeight = 20,
iconAnchorX = 31*216/230/2, iconAnchorY = 19
)
hopkinsLatLong <- data.frame(
lat = c(39.2969166, 39.3688851, 39.2956617),
lng = c(-76.5929298, -76.6206598, -76.5468683))
hopkinsLatLong %>%
leaflet() %>%
addTiles() %>%
addMarkers(icon = hopkinsIcon)
hopkinsite<- 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)
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())
Instead of adding markers or clusters you can easily add circle markers using addCircleMarkers().
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()
You can draw arbitrary shapes on the maps you create, including circles and squares. The code below draws a map where the circle on each city is proportional to the population of that city.
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
leaflet() %>%
addTiles() %>%
addRectangles(lat1 = 37.38532328, lng1 = -122.059325,
lat2 = 37.3893420, lng2 = -122.06234225)
The legend of a graph reflects the data displayed in the graph’s Y-axis, also called the graph series. This is the data that comes from the columns of the corresponding grid report, and usually represents metrics.
A graph legend generally appears as a box to the right or left of your graph. The box contains small samples of each color on the graph as well as a short description of what each color means. A graph legend is a common component of any graph report, because it helps the analyst understand what the colors and shapes in the graph mean in terms of your data.
As you format your graph, you may find that you need to make a graph legend visible or format it. Steps are below for displaying, hiding, and formatting a graph legend in both MicroStrategy Developer and MicroStrategy Web.
df <- data.frame(lat = runif(20, min = 39.25, max = 39.35),
lng = runif(20, min = -76.65, max = -76.55),
col = sample(c("black", "green", "blue"), 20, replace = TRUE),
stringsAsFactors = FALSE)
df %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(color = df$col) %>%
addLegend(labels = LETTERS[1:3], colors = c("black", "black", "black"))
You may have noticed that we used the new keyword for creating LeafIcon instances. So why do all Leaflet classes get created without it? The answer is simple: the real Leaflet classes are named with a capital letter (e.g. L.Icon), and they also need to be created with new, but there are also shortcuts with lowercase names (L.icon), created for convenience like this.