Maps

library(tidyverse) # Maps using ggplot + geom_s
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(osmdata) # Fetch map data from osmdata.org
Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(tmap)
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')
library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(leaflet)
m <- leaflet() %>%
  # Add default OpenStreetMap map tiles
  addTiles() %>%
  # Set view to be roughly centred on Bangalore City
  setView(lng = 77.580643, lat = 12.972442, zoom = 12)

m
m %>% addMarkers(
  lng = 77.580643, lat = 12.972442,
  popup = "The birthplace of Rvind"
)
m %>%
  addPopups(
    lng = 77.580643,
    lat = 12.972442,
    popup = paste(
      "The birthplace of Rvind",
      "<br>",
      "Website: <a href = https://arvindvenkatadri.com>Arvind V's Website </a>",
      "<br>"
    ),
    ## Ensuring we cannot close the popup,
    ## else we will not be able to find where it is,
    ## since there is no Marker

    options = popupOptions(closeButton = FALSE)
  )
m %>%
  addMarkers(
    lng = 77.580643,
    lat = 12.972442,

    # Here is the Label defn.
    label = "The birthplace of Rvind",
    labelOptions = labelOptions(
      noHide = TRUE, # Label always visible
      textOnly = F,
      textsize = 20
    ),

    # And here is the popup defn.
    popup = paste(
      "PopUp Text: <a href = https://arvindvenkatadri.com>Arvind V's Website </a>",
      "<br>"
    )
  )
md_cities <- tibble(
  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()%>%
  # CircleMarkers, in blue
  # radius scales the Marker. Units are in Pixels!!
  # Here, radius is made proportional to `pop` number
  addCircleMarkers(
    radius = ~ pop / 1000, # Pixels!!
    color = "blue",
    stroke = FALSE, # no border for the Markers
    opacity = 0.8
  ) %>%
  # Circles, in red
  addCircles(
    radius = 5000, # Meters !!!
    stroke = TRUE,
    color = "yellow", # Stroke Colour
    weight = 3, # Stroke Weight
    fill = TRUE,
    fillColor = "red",
  )
Assuming "lng" and "lat" are longitude and latitude, respectively
Assuming "lng" and "lat" are longitude and latitude, respectively
leaflet() %>%
  addTiles() %>%
  setView(lng = 77.580643, lat = 12.972442, zoom = 6) %>%
  # arbitrary vector data for lat and lng
  addPolygons(
    lng = c(73.5, 75.9, 76.1, 77.23, 79.8),
    lat = c(10.12, 11.04, 11.87, 12.04, 10.7)
  )
india_airports <- read_delim("../../data/airports.csv")
Rows: 340 Columns: 20
── Column specification ────────────────────────────────────────────────────────
Delimiter: ";"
chr  (13): ident, type, name, continent, iso_country, iso_region, municipali...
dbl   (4): id, elevation_ft, scheduled_service, score
num   (2): lat, lon
dttm  (1): last_updated

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
leaflet(data = india_airports) %>%
  setView(lat = 18, lng = 77, zoom = 4) %>% 
   addProviderTiles(providers$Esri.NatGeoWorldMap) %>% 
    addCircleMarkers(
    lng = ~lon, lat = ~lat,
    radius = 2, # Pixels
    color = "red",
    opacity = 1
  )

tile is unit of a map

popup <- paste(
  "<strong>",
  india_airports$name,
  "</strong><br>",
  india_airports$iata_code,
  "<br>",
  india_airports$municipality,
  "<br>",
  "Elevation(feet)",
  india_airports$elevation_ft,
  "<br>",
  india_airports$wikipedia_link,
  "<br>"
)

iata_icon <- makeIcon(
  "images/iata-logo-transp.png", # Downloaded from www.iata.org
  iconWidth = 24,
  iconHeight = 24,
  iconAnchorX = 0,
  iconAnchorY = 0
)
leaflet(data = india_airports) %>%
  setView(lat = 18, lng = 77, zoom = 4) %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  addMarkers(
    icon = iata_icon,
    popup = popup
)
Assuming "lon" and "lat" are longitude and latitude, respectively
data(metro, package = "tmap")
metro
Simple feature collection with 436 features and 12 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -123.1193 ymin: -37.814 xmax: 174.7667 ymax: 60.16925
old-style crs object detected; please recreate object with a recent sf::st_crs()
Geodetic CRS:  WGS 84
First 10 features:
old-style crs object detected; please recreate object with a recent sf::st_crs()
old-style crs object detected; please recreate object with a recent sf::st_crs()
           name             name_long iso_a3 pop1950 pop1960 pop1970 pop1980
2         Kabul                 Kabul    AFG  170784  285352  471891  977824
8       Algiers El Djazair  (Algiers)    DZA  516450  871636 1281127 1621442
13       Luanda                Luanda    AGO  138413  219427  459225  771349
16 Buenos Aires          Buenos Aires    ARG 5097612 6597634 8104621 9422362
17      Cordoba               Cordoba    ARG  429249  605309  809794 1009521
25      Rosario               Rosario    ARG  554483  671349  816230  953491
32      Yerevan               Yerevan    ARM  341432  537759  778158 1041587
33     Adelaide              Adelaide    AUS  429277  571822  850168  971856
34     Brisbane              Brisbane    AUS  441718  602999  904777 1134833
37    Melbourne             Melbourne    AUS 1331966 1851220 2499109 2839019
    pop1990  pop2000  pop2010  pop2020  pop2030                    geometry
2   1549320  2401109  3722320  5721697  8279607   POINT (69.17246 34.52889)
8   1797068  2140577  2432023  2835218  3404575     POINT (3.04197 36.7525)
13  1390240  2591388  4508434  6836849 10428756   POINT (13.23432 -8.83682)
16 10513284 12406780 14245871 15894307 16956491 POINT (-58.40037 -34.60508)
17  1200168  1347561  1459268  1562509  1718192  POINT (-64.18105 -31.4135)
25  1083819  1152387  1298073  1453814  1606993 POINT (-60.63932 -32.94682)
32  1174524  1111301  1065597  1023703  1057459     POINT (44.51462 40.182)
33  1081618  1141623  1217990  1320783  1505422  POINT (138.5986 -34.92866)
34  1381306  1666203  2033617  2388517  2721325  POINT (153.0281 -27.46794)
37  3154314  3460541  3951216  4500501  5070873    POINT (144.9633 -37.814)
leaflet(data = metro) %>%
  setView(lat = 18, lng = 77, zoom = 4) %>%
  # Add CartoDB.Positron
  addProviderTiles(providers$CartoDB.Positron) %>% # CartoDB Basemap

  # Add Markers for each airport
  addCircleMarkers(
    radius = ~ sqrt(pop2030) / 350,
    color = "red",
    popup = paste(
      "Name: ", metro$name, "<br>",
      "Population 2030: ", metro$pop2030
    )
  )
old-style crs object detected; please recreate object with a recent sf::st_crs()
old-style crs object detected; please recreate object with a recent sf::st_crs()
old-style crs object detected; please recreate object with a recent sf::st_crs()
old-style crs object detected; please recreate object with a recent sf::st_crs()