The leaflet
equivalent of the ggplot
function is leaflet
:
For example, NASA’s earth at night map:
leaflet() %>%
setView(lng = -79.442778, lat = 37.783889, zoom = 8) %>%
addProviderTiles(provider = providers$NASAGIBS.ViirsEarthAtNight2012)
Or CartoDB’s “Positron” map (nice if you want the map elements to be “light”):
leaflet() %>%
setView(lng = -79.442778, lat = 37.783889, zoom = 12) %>%
addProviderTiles(providers$CartoDB.Positron)
leaflet() %>%
addTiles() %>%
setView(lng = -79.442778, lat = 37.783889, zoom = 8) %>%
addWMSTiles(
"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers = "nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "Weather data © 2012 IEM Nexrad"
)
Here are the rules for your table:
lex <- data.frame( lat = c(37.789444 , 37.787673 , 37.785624 )
, lon = c(-79.441725, -79.443623 , -79.441544 )
, place = c("Lab" , "Classroom", "Thai food!")
, rating = c(7 , 5 , 10 )
, stringsAsFactors = FALSE
)
We can make a simple map as before, but this time we specify the data
argument in our call to leaflet
:
Nothing’s changed; but now we can use the additional columns to add annotations onto our map, like markers:
Assuming "lon" and "lat" are longitude and latitude, respectively
If you have a lot of markers in one place the clustering feature can be useful (note the lower zoom level):
leaflet(data = lex) %>%
setView(lng = -79.442778, lat = 37.783889, zoom = 10) %>%
addTiles() %>%
addMarkers(label = ~place, clusterOptions = markerClusterOptions())
Assuming "lon" and "lat" are longitude and latitude, respectively
You can also add two different kinds of “circles” onto your maps to visualize quantitative data. The first is addCircleMarkers
:
Assuming "lon" and "lat" are longitude and latitude, respectively
The second addCircles
:
Assuming "lon" and "lat" are longitude and latitude, respectively
The difference between the two is that the quantitative variable being used for the radius
different things:
addCircleMarkers
it specifies the size in pixels (image-land); no matter the zoom the circles are always the same sizeaddCircles
it specifies the size in meters (real-world); so they get bigger or smaller when you zoom.Here’s an example:
ui.R
library(shiny)
library(leaflet)
fluidPage(
# map output
leafletOutput("worldMap"),
# line break (puts some space between map and button)
br(),
# a button
actionButton("newButton", "New place!")
)
server.R
library(shiny)
library(leaflet)
function(input, output, session) {
lats <- -90:90
lons <- -180:180
output$worldMap <- renderLeaflet({
# note the dummy use of the action button input
btn <- input$newButton
leaflet() %>%
setView(lng = sample(lons, 1), lat = sample(lats, 1), zoom = 5) %>%
addTiles()
})
}
server.R
function(input, output, session) {
lats <- -90:90
lons <- -180:180
output$worldMap <- renderLeaflet({
leaflet() %>%
setView(lng = -79.442778, lat = 37.783889, zoom = 5) %>%
addTiles()
})
observe({
# note the dummy use of the action button input
btn <- input$newButton
leafletProxy("worldMap") %>%
setView(lng = sample(lons, 1), lat = sample(lats, 1), zoom = 5)
})
}
function(input, output, session) {
lats <- -90:90
lons <- -180:180
output$worldMap <- renderLeaflet({
leaflet() %>%
setView(lng = -79.442778, lat = 37.783889, zoom = 5) %>%
addTiles()
})
observe({
# note the dummy use of the action button input
btn <- input$newButton
leafletProxy("worldMap") %>%
setView(lng = sample(lons, 1), lat = sample(lats, 1), zoom = 5)
})
}
<environment: 0x56485a2a6df8>