Introduction

This is an example of a web page created using R Markdown. Below is a map created with Leaflet.

Map

library(leaflet)

# Create a basic map
leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng = -0.09, lat = 51.505, popup = "I am a popup")

Adding Multiple Markers with Popups

You can add multiple markers to your map, each with its own popup message.

library(leaflet)

leaflet() %>%
  addTiles() %>%
  addMarkers(lng = c(-0.09, -0.08, -0.07), lat = c(51.505, 51.51, 51.515), 
             popup = c("Marker 1", "Marker 2", "Marker 3"))

Adding Circle Markers

Circle markers can be added to highlight specific areas with a radius.

leaflet() %>%
  addTiles() %>%
  addCircleMarkers(lng = -0.09, lat = 51.505, radius = 20, 
                   color = "red", popup = "A circle marker")

Using Custom Icons

You can use custom icons for your markers to make them more visually appealing.

icon <- makeIcon(
  iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "https://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

leaflet() %>%
  addTiles() %>%
  addMarkers(lng = -0.09, lat = 51.505, icon = icon, popup = "Custom icon")

Adding Polylines

You can add lines connecting various points, which is useful for showing routes or paths.

leaflet() %>%
  addTiles() %>%
  addPolylines(lng = c(-0.09, -0.08, -0.07), lat = c(51.505, 51.51, 51.515), 
               color = "blue")

Adding Polygons

Polygons can be used to highlight specific areas on the map.

leaflet() %>%
  addTiles() %>%
  addPolygons(lng = c(-0.09, -0.08, -0.07, -0.09), lat = c(51.505, 51.51, 51.515, 51.505), 
              color = "green", fillColor = "green", fillOpacity = 0.5)

Adding GeoJSON Data

You can add GeoJSON data to display complex shapes and areas.

geojson <- '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-0.09,51.505]},"properties":{"popupContent":"This is a GeoJSON point!"}}]}'

leaflet() %>%
  addTiles() %>%
  addGeoJSON(geojson)

Using Different Tile Layers

You can use different tile layers to change the base map style.

leaflet() %>%
  addProviderTiles(providers$Stamen.Toner) %>%
  addMarkers(lng = -0.09, lat = 51.505, popup = "Different tile layer")

Adding Mini Maps

You can add a mini map for better navigation.

leaflet() %>%
  addTiles() %>%
  addMiniMap()

Adding Layers Control

You can add a layers control to switch between different groups of markers or layers.

leaflet() %>%
  addTiles(group = "OSM (default)") %>%
  addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
  addLayersControl(
    baseGroups = c("OSM (default)", "Toner"),
    options = layersControlOptions(collapsed = FALSE)
  )

Finally, here is an example that incorporates all the Features described:

library(leaflet)

icon <- makeIcon(
  iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
  iconWidth = 38, iconHeight = 95,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "https://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

leaflet() %>%
  addTiles(group = "OSM (default)") %>%
  addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
  addMarkers(lng = -0.09, lat = 51.505, icon = icon, popup = "Custom icon") %>%
  addCircleMarkers(lng = -0.08, lat = 51.51, radius = 20, color = "red", popup = "A circle marker") %>%
  addPolylines(lng = c(-0.09, -0.08, -0.07), lat = c(51.505, 51.51, 51.515), color = "blue") %>%
  addPolygons(lng = c(-0.09, -0.08, -0.07, -0.09), lat = c(51.505, 51.51, 51.515, 51.505), 
              color = "green", fillColor = "green", fillOpacity = 0.5) %>%
  addLayersControl(
    baseGroups = c("OSM (default)", "Toner"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addMiniMap()