2024-03-21
Kinds of maps (outline vs. tiles; interactive vs. static)
Creating basic maps
Adding points
Shading areas
Tile service providers
Dynamic: leaflet
for drawing raster or tiled maps (leafletjs.com)
Static: ggmap
(part of tidyverse, tiles from several services)
Both rely on internet services such as:
city | city_ascii | lat | lng | country | iso2 | iso3 | admin_name | capital | population | id |
---|---|---|---|---|---|---|---|---|---|---|
Tokyo | Tokyo | 35.6897 | 139.6922 | Japan | JP | JPN | Tōkyō | primary | 37977000 | 1392685764 |
Jakarta | Jakarta | -6.2146 | 106.8451 | Indonesia | ID | IDN | Jakarta | primary | 34540000 | 1360771077 |
Delhi | Delhi | 28.6600 | 77.2300 | India | IN | IND | Delhi | admin | 29617000 | 1356872604 |
Mumbai | Mumbai | 18.9667 | 72.8333 | India | IN | IND | Mahārāshtra | admin | 23355000 | 1356226629 |
Manila | Manila | 14.5958 | 120.9772 | Philippines | PH | PHL | Manila | primary | 23088000 | 1608618140 |
Shanghai | Shanghai | 31.1667 | 121.4667 | China | CN | CHN | Shanghai | admin | 22120000 | 1156073548 |
library(geojsonio)
pal <- colorNumeric("viridis", NULL) # make a viridis palette
canada <- geojson_read("https://gist.githubusercontent.com/mikelotis/2156d7c170d10d2c77cb79424fe2137d/raw/7a13748ed7ea5ba64876c77c53b6cb64dd5c3ab0/canada-province.geojson", what="sp")
my_values = runif(13, 0, 10) # random numbers between 0 and 10
m3 <- canada |>
leaflet() |>
addTiles() |>
addPolygons(fillColor = ~ pal(my_values),
weight = 1, color = "black") |>
addLegend(pal = pal, values = my_values, opacity = 1.0)
Google maps have more options, but you must sign up for an API key first. These examples are for anyone who wants to experiment with that option.
See help for the package ggmap
for more services (google maps, open street maps).
Start with a small integer for zoom
and increase it if your map is fuzzy. (Don’t burden yourself or a free service by downloading unnecessary data.)
Outline vs tiled (image) maps
Make a basic map
Add points and labels
Fill regions with colour (requires polygons)
There are several tile services, but free access comes and goes frequently