Data set details

Data set description: Spatial features of Lagos (Nigeria)
Source: OpenStreetMap (OSM)
Details on the retrieved data: Hospitals, highways and rivers in Lagos (Nigeria), plotted along with the map studied region.
Spatial and temporal resolution: High-resolution data at city level.

Retrieving Open Street Map data using the osmdata package

osmdata is an R package for downloading data from OpenStreetMap (OSM). This tutorial takes you through the steps of retrieving points of interest in defined geographical areas using the osmdata package, and visualising them using the ggmap and ggplot2 packages.

Installing the osmdata package

We can install and load the osmdata package from CRAN as follows

install.packages("osmdata")
library(osmdata)

Exploring the osmdata package

The osmdata package provides spatial data about a wide range of spatial properties and objects across the world. The available_features() function can be used to get the list of recognized features in OSM. A list of the available features can be found in the OSM wiki.

available_features()
##   [1] "4wd_only"                    "abandoned"                  
##   [3] "abutters"                    "access"                     
##   [5] "addr"                        "addr:city"                  
##   [7] "addr:conscriptionnumber"     "addr:country"               
##   [9] "addr:county"                 "addr:district"              
##  [11] "addr:flats"                  "addr:full"                  
##  [13] "addr:hamlet"                 "addr:housename"             
##  [15] "addr:housenumber"            "addr:inclusion"             
##  [17] "addr:interpolation"          "addr:place"                 
##  [19] "addr:postbox"                "addr:postcode"              
##  [21] "addr:province"               "addr:state"                 
##  [23] "addr:street"                 "addr:subdistrict"           
##  [25] "addr:suburb"                 "addr:unit"                  
##  [27] "admin_level"                 "aeroway"                    
##  [29] "agricultural"                "alt_name"                   
##  [31] "amenity"                     "area"                       
##  [33] "atv"                         "backward"                   
##  [35] "barrier"                     "basin"                      
##  [37] "bdouble"                     "bicycle"                    
##  [39] "bicycle_road"                "biergarten"                 
##  [41] "boat"                        "border_type"                
##  [43] "boundary"                    "brand"                      
##  [45] "bridge"                      "building"                   
##  [47] "building:colour"             "building:fireproof"         
##  [49] "building:flats"              "building:levels"            
##  [51] "building:material"           "building:min_level"         
##  [53] "building:part"               "building:soft_storey"       
##  [55] "bus_bay"                     "busway"                     
##  [57] "capacity"                    "castle_type"                
##  [59] "change"                      "charge"                     
##  [61] "clothes"                     "construction"               
##  [63] "construction#Railways"       "covered"                    
##  [65] "craft"                       "crossing"                   
##  [67] "crossing:island"             "cuisine"                    
##  [69] "cutting"                     "cycleway"                   
##  [71] "denomination"                "destination"                
##  [73] "diet"                        "direction"                  
##  [75] "dispensing"                  "disused"                    
##  [77] "drinking_water"              "drive_in"                   
##  [79] "drive_through"               "ele"                        
##  [81] "electric_bicycle"            "electrified"                
##  [83] "embankment"                  "embedded_rails"             
##  [85] "emergency"                   "end_date"                   
##  [87] "entrance"                    "est_width"                  
##  [89] "fee"                         "female"                     
##  [91] "fire_object:type"            "fire_operator"              
##  [93] "fire_rank"                   "foot"                       
##  [95] "footway"                     "ford"                       
##  [97] "forestry"                    "forward"                    
##  [99] "frequency"                   "fuel"                       
## [101] "gauge"                       "golf_cart"                  
## [103] "goods"                       "hazard"                     
## [105] "hazmat"                      "healthcare"                 
## [107] "healthcare:counselling"      "healthcare:speciality"      
## [109] "height"                      "hgv"                        
## [111] "highway"                     "historic"                   
## [113] "horse"                       "hot_water"                  
## [115] "ice_road"                    "incline"                    
## [117] "industrial"                  "inline_skates"              
## [119] "inscription"                 "int_name"                   
## [121] "internet_access"             "junction"                   
## [123] "kerb"                        "landuse"                    
## [125] "lanes"                       "lanes:bus"                  
## [127] "lanes:psv"                   "layer"                      
## [129] "leaf_cycle"                  "leaf_type"                  
## [131] "leisure"                     "lhv"                        
## [133] "lit"                         "loc_name"                   
## [135] "location"                    "male"                       
## [137] "man_made"                    "max_age"                    
## [139] "max_level"                   "maxaxleload"                
## [141] "maxheight"                   "maxlength"                  
## [143] "maxspeed"                    "maxstay"                    
## [145] "maxweight"                   "maxwidth"                   
## [147] "military"                    "min_age"                    
## [149] "min_level"                   "minspeed"                   
## [151] "mofa"                        "moped"                      
## [153] "motor_vehicle"               "motorboat"                  
## [155] "motorcar"                    "motorcycle"                 
## [157] "motorroad"                   "mountain_pass"              
## [159] "mtb:description"             "mtb:scale"                  
## [161] "name"                        "name:left"                  
## [163] "name:right"                  "name_1"                     
## [165] "name_2"                      "narrow"                     
## [167] "nat_name"                    "natural"                    
## [169] "noexit"                      "non_existent_levels"        
## [171] "nudism"                      "office"                     
## [173] "official_name"               "old_name"                   
## [175] "oneway"                      "opening_hours"              
## [177] "opening_hours:drive_through" "operator"                   
## [179] "orientation"                 "oven"                       
## [181] "overtaking"                  "parking"                    
## [183] "parking:condition"           "parking:lane"               
## [185] "passing_places"              "place"                      
## [187] "power"                       "power_supply"               
## [189] "priority"                    "priority_road"              
## [191] "produce"                     "proposed"                   
## [193] "protected_area"              "psv"                        
## [195] "public_transport"            "railway"                    
## [197] "railway:preserved"           "railway:track_ref"          
## [199] "recycling_type"              "ref"                        
## [201] "reg_name"                    "religion"                   
## [203] "rental"                      "residential"                
## [205] "roadtrain"                   "route"                      
## [207] "sac_scale"                   "sauna"                      
## [209] "service"                     "service_times"              
## [211] "shelter_type"                "shop"                       
## [213] "short_name"                  "shower"                     
## [215] "sidewalk"                    "site"                       
## [217] "ski"                         "smoothness"                 
## [219] "social_facility"             "sorting_name"               
## [221] "speed_pedelec"               "start_date"                 
## [223] "step_count"                  "substation"                 
## [225] "surface"                     "tactile_paving"             
## [227] "tank"                        "tidal"                      
## [229] "toilets"                     "toilets:wheelchair"         
## [231] "toll"                        "topless"                    
## [233] "tourism"                     "tracks"                     
## [235] "tracktype"                   "traffic_calming"            
## [237] "traffic_sign"                "trail_visibility"           
## [239] "trailblazed"                 "trailblazed:visibility"     
## [241] "tunnel"                      "turn"                       
## [243] "type"                        "unisex"                     
## [245] "usage"                       "vehicle"                    
## [247] "vending"                     "voltage"                    
## [249] "water"                       "wheelchair"                 
## [251] "wholesale"                   "width"                      
## [253] "winter_road"                 "wood"

The available_tags() function lists out the tags associated with each feature. The tags associated with the feature “amenity” can be retrieved as follows.

available_tags("amenity")
##   [1] "animal_boarding"        "animal_breeding"        "animal_shelter"        
##   [4] "arts_centre"            "atm"                    "baby_hatch"            
##   [7] "baking_oven"            "bank"                   "bar"                   
##  [10] "bbq"                    "bench"                  "bicycle_parking"       
##  [13] "bicycle_rental"         "bicycle_repair_station" "biergarten"            
##  [16] "boat_rental"            "boat_sharing"           "brothel"               
##  [19] "bureau_de_change"       "bus_station"            "cafe"                  
##  [22] "car_rental"             "car_sharing"            "car_wash"              
##  [25] "casino"                 "charging_station"       "childcare"             
##  [28] "cinema"                 "clinic"                 "clock"                 
##  [31] "college"                "community_centre"       "compressed_air"        
##  [34] "conference_centre"      "courthouse"             "crematorium"           
##  [37] "dentist"                "dive_centre"            "doctors"               
##  [40] "dog_toilet"             "dressing_room"          "drinking_water"        
##  [43] "driving_school"         "events_venue"           "fast_food"             
##  [46] "ferry_terminal"         "fire_station"           "food_court"            
##  [49] "fountain"               "fuel"                   "funeral_hall"          
##  [52] "gambling"               "give_box"               "grave_yard"            
##  [55] "grit_bin"               "hospital"               "hunting_stand"         
##  [58] "ice_cream"              "internet_cafe"          "kindergarten"          
##  [61] "kitchen"                "kneipp_water_cure"      "language_school"       
##  [64] "library"                "lounger"                "love_hotel"            
##  [67] "mailroom"               "marketplace"            "monastery"             
##  [70] "motorcycle_parking"     "music_school"           "nightclub"             
##  [73] "nursing_home"           "parcel_locker"          "parking"               
##  [76] "parking_entrance"       "parking_space"          "pharmacy"              
##  [79] "photo_booth"            "place_of_mourning"      "place_of_worship"      
##  [82] "planetarium"            "police"                 "post_box"              
##  [85] "post_depot"             "post_office"            "prison"                
##  [88] "pub"                    "public_bath"            "public_bookcase"       
##  [91] "public_building"        "ranger_station"         "recycling"             
##  [94] "refugee_site"           "restaurant"             "sanitary_dump_station" 
##  [97] "school"                 "shelter"                "shower"                
## [100] "social_centre"          "social_facility"        "stripclub"             
## [103] "studio"                 "swingerclub"            "taxi"                  
## [106] "telephone"              "theatre"                "toilets"               
## [109] "townhall"               "toy_library"            "training"              
## [112] "university"             "vehicle_inspection"     "vending_machine"       
## [115] "veterinary"             "waste_basket"           "waste_disposal"        
## [118] "waste_transfer_station" "water_point"            "watering_place"

Creating a query to download data

Defining the bounding box

The first step in creating an osmdata query is defining the geographical area we want to include in the query. This can be done by defining a bounding box that defines a geographical area by its bounding latitudes and longitudes. The osmdata package provides a function getbb() to retrieve the bounding box of a place using its name. We can now create the bounding box of Lagos, the largest city in Nigeria.

lagos_bb <- getbb("Lagos")
lagos_bb
##        min      max
## x 3.234180 3.554180
## y 6.295058 6.615057

Creating an overpass query

The osmdata package retrieves data from the overpass API, which is a read-only API that serves up custom selected parts of the OSM map data. To retrieve the required features of a place (defined by the bounding box), we have to then create an overpass query. This can be easily done using the opq() function of the osmdata package. Here we use the previously defined bounding box of Lagos to create the overpass query

lagos_bb %>%
  opq()
## $bbox
## [1] "6.2950575,3.2341795,6.6150575,3.5541795"
## 
## $prefix
## [1] "[out:xml][timeout:25];\n(\n"
## 
## $suffix
## [1] ");\n(._;>;);\nout body;"
## 
## $features
## NULL
## 
## attr(,"class")
## [1] "list"           "overpass_query"
## attr(,"nodes_only")
## [1] FALSE

Retrieving the osmdata object

Then, the add_osm_feature() function can be used to add the required features to the query, using the features and tags we explored earlier in this tutorial. This query specified in terms of key-value is used to retreive data on hospitals in Lagos.

There are two primary osmdata functions for obtaining data from a query: osmdata_sf() and osmdata_sp(), which return data in simple features (sf) and spatial (sp) formats, respectively. Here, we use the osmdata_sf() function to obtain a simple feature object of the resultant query.

lagos_hospitals <- lagos_bb %>%
  opq() %>%
  add_osm_feature(key = "amenity", value = "hospital") %>%
  osmdata_sf()

Understanding the osmdata object

The osmdata objects will contain the following components

  • A bounding box used in query
  • The call submitted to the overpass API
  • Meta data about the object such as timestamp and version numbers
  • Spatial data - some of which may be empty depending on the type of data retrieved

The following is the osmdata object retrieved by querying the hospitals in Lagos

lagos_hospitals

Note how each component of the osmdata objects is preceded by a $ symbol and some of them are NULL. This is expected, since we queried for hospitals in Lagos, and they are represented using points and polygons only. We can also print out each of these components and explore each of them for a better understanding of them.

# bounding box used in query
lagos_hospitals$bbox
## [1] "6.2950575,3.2341795,6.6150575,3.5541795"
# metadata
lagos_hospitals$meta
## $timestamp
## [1] "[ Sat 7 Jan 2023 13:09:58 ]"
## 
## $OSM_version
## [1] "0.6"
## 
## $overpass_version
## [1] "Overpass API 0.7.59 e21c39fe"

Note how the Spatial data returned by the query are Simple Feature objects as we requested using theosmdata_sf() function, and how the polygons also contain more details about the hospitals such as their names, websites, wikipedia pages etc. where available.

# osm_points
lagos_hospitals$osm_points
## Simple feature collection with 408 features and 15 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 3.250371 ymin: 6.427734 xmax: 3.522714 ymax: 6.610555
## Geodetic CRS:  WGS 84
## First 10 features:
##                osm_id name access addr.city addr.housenumber addr.postcode
## 815678325   815678325 <NA>   <NA>      <NA>             <NA>          <NA>
## 928544939   928544939 <NA>   <NA>      <NA>             <NA>          <NA>
## 1209198481 1209198481 <NA>   <NA>      <NA>             <NA>          <NA>
## 1209198504 1209198504 <NA>   <NA>      <NA>             <NA>          <NA>
## 1209198747 1209198747 <NA>   <NA>      <NA>             <NA>          <NA>
## 3024218835 3024218835 <NA>   <NA>      <NA>             <NA>          <NA>
## 3845787945 3845787945 <NA>   <NA>      <NA>             <NA>          <NA>
## 3845787949 3845787949 <NA>   <NA>      <NA>             <NA>          <NA>
## 3845787951 3845787951 <NA>   <NA>      <NA>             <NA>          <NA>
## 3971452305 3971452305 <NA>   <NA>      <NA>             <NA>          <NA>
##            addr.street amenity barrier fixme healthcare healthcare.speciality
## 815678325         <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 928544939         <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 1209198481        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 1209198504        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 1209198747        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 3024218835        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 3845787945        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 3845787949        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 3845787951        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
## 3971452305        <NA>    <NA>    <NA>  <NA>       <NA>                  <NA>
##            name.pcm operator operator.type                  geometry
## 815678325      <NA>     <NA>          <NA> POINT (3.303055 6.461788)
## 928544939      <NA>     <NA>          <NA> POINT (3.361348 6.495705)
## 1209198481     <NA>     <NA>          <NA> POINT (3.397627 6.447358)
## 1209198504     <NA>     <NA>          <NA>    POINT (3.397712 6.447)
## 1209198747     <NA>     <NA>          <NA> POINT (3.397854 6.447192)
## 3024218835     <NA>     <NA>          <NA> POINT (3.370959 6.511046)
## 3845787945     <NA>     <NA>          <NA> POINT (3.397503 6.448392)
## 3845787949     <NA>     <NA>          <NA> POINT (3.397583 6.448494)
## 3845787951     <NA>     <NA>          <NA>  POINT (3.397146 6.44864)
## 3971452305     <NA>     <NA>          <NA> POINT (3.386569 6.551142)
# osm_polyogns
lagos_hospitals$osm_polygons
## Simple feature collection with 34 features and 21 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 3.250371 ymin: 6.427734 xmax: 3.522714 ymax: 6.610555
## Geodetic CRS:  WGS 84
## First 10 features:
##              osm_id                                     name addr.city
## 394119616 394119616                 Gbagada General Hospital      <NA>
## 525043228 525043228     National Orthopaedic Hospital Igbobi      <NA>
## 556508170 556508170         Gold Cross Hospital - Bourdillon     Lagos
## 568367654 568367654          Reddington Hospital - Ikeja GRA      <NA>
## 594743340 594743340                      Reddington Hospital      <NA>
## 595195260 595195260   Eye Foundation Hospital & Laser Centre      <NA>
## 643481485 643481485       St. Kizito Clinic & Maternity Ward      <NA>
## 659370489 659370489        Nigerian Police Hospital - Falomo      <NA>
## 672092174 672092174                   General Hospital Lagos      <NA>
## 704991085 704991085 Lagos State University Teaching Hospital      <NA>
##           addr.housenumber          addr.street alt_name  amenity building
## 394119616             <NA>                 <NA>     <NA> hospital     <NA>
## 525043228             <NA>                 <NA>     <NA> hospital     <NA>
## 556508170              17B      Bourdillon Road     <NA> hospital     <NA>
## 568367654             <NA>                 <NA>     <NA> hospital     <NA>
## 594743340             <NA> Idowu Martins Street     <NA> hospital     <NA>
## 595195260             <NA>                 <NA>     <NA> hospital     <NA>
## 643481485             <NA>                 <NA>     <NA> hospital     <NA>
## 659370489             <NA>                 <NA>     <NA> hospital     <NA>
## 672092174             <NA>                 <NA>     <NA> hospital     <NA>
## 704991085             <NA>                 <NA>     <NA> hospital     <NA>
##           emergency health_facility.type healthcare healthcare.speciality level
## 394119616      <NA>                 <NA>   hospital                  <NA>  <NA>
## 525043228      <NA>                 <NA>   hospital          orthopaedics  <NA>
## 556508170      <NA>                 <NA>   hospital                  <NA>  <NA>
## 568367654      <NA>                 <NA>   hospital                  <NA>  <NA>
## 594743340      <NA>                 <NA>   hospital                  <NA>  <NA>
## 595195260      <NA>                 <NA>   hospital                  <NA>  <NA>
## 643481485      <NA>                 <NA>   hospital                  <NA>  <NA>
## 659370489      <NA>                 <NA>   hospital                  <NA>  <NA>
## 672092174      <NA>                 <NA>   hospital                  <NA>  <NA>
## 704991085      <NA>                 <NA>   hospital                  <NA>  <NA>
##           opening_hours operator short_name source start_date
## 394119616          <NA>     <NA>       <NA>   <NA>       <NA>
## 525043228          <NA>     <NA>      NOHIL   <NA>       <NA>
## 556508170          <NA>     <NA>       <NA>   <NA>       <NA>
## 568367654          <NA>     <NA>       <NA>   <NA>       <NA>
## 594743340          <NA>     <NA>       <NA>   <NA>       <NA>
## 595195260          <NA>     <NA>       <NA>   <NA>       <NA>
## 643481485          <NA>     <NA>       <NA>   <NA>       <NA>
## 659370489          <NA>     <NA>       <NA>   <NA>       <NA>
## 672092174          <NA>     <NA>       <NA>   <NA>       1893
## 704991085          <NA>     <NA>     LASUTH   <NA>       <NA>
##                                website  wikidata
## 394119616                         <NA> Q48743188
## 525043228 https://www.nohlagos.gov.ng/ Q23023017
## 556508170                         <NA>      <NA>
## 568367654                         <NA>      <NA>
## 594743340                         <NA>      <NA>
## 595195260                         <NA>      <NA>
## 643481485                         <NA>      <NA>
## 659370489                         <NA>      <NA>
## 672092174    http://www.ghlagos.org.ng Q19874986
## 704991085                         <NA> Q28449098
##                                             wikipedia
## 394119616                 en:Gbagada General Hospital
## 525043228            en:National Orthopaedic Hospital
## 556508170                                        <NA>
## 568367654                                        <NA>
## 594743340                                        <NA>
## 595195260                                        <NA>
## 643481485                                        <NA>
## 659370489                                        <NA>
## 672092174            en:Lagos Island General Hospital
## 704991085 en:Lagos State University Teaching Hospital
##                                 geometry
## 394119616 POLYGON ((3.387942 6.554221...
## 525043228 POLYGON ((3.368175 6.532734...
## 556508170 POLYGON ((3.436958 6.446001...
## 568367654 POLYGON ((3.356857 6.583489...
## 594743340 POLYGON ((3.420182 6.433274...
## 595195260 POLYGON ((3.456025 6.439484...
## 643481485 POLYGON ((3.505188 6.434465...
## 659370489 POLYGON ((3.42908 6.443328,...
## 672092174 POLYGON ((3.39582 6.448513,...
## 704991085 POLYGON ((3.344492 6.590988...

Visualising queried data with ggplot2 and ggmap

We can visualise the retrieved data about hospitals in Lagos using the ggplot2 package. We can easily visualise simple feature objects using the geom_sf() function of ggplot2.

# install.packages("ggplot2")
library(ggplot2)

ggplot() +
  geom_sf(data = lagos_hospitals$osm_polygons)

While the visualisation above provides useful information about the spread of hospitals in Lagos, it would be useful to plot the locations of hospitals overlayed on a map of Lagos. For this we use the ggmap package. To get a map of Lagos, we use the get_map() function provided by the ggmap package. Here we have used the maptype “roadmap”, but the function allows many more map types which can be found here.

library(ggmap)
lagos_map <- get_map(lagos_bb, maptype = "roadmap")

Then we call ggmap() with the background map retreived map using get_map(), and overlay the spatial data of the hospitals on it using geom_sf(). Note how we have changed the colour and outline line size of the polygons, filled them in and also changed the opacity to introduce some transparency to the polygons. We also set inherit.aes = FALSE to use the aesthetic mappings of the spatial object lagos_hospitals$osm_polygons.

ggmap(lagos_map) +
  geom_sf(
    data = lagos_hospitals$osm_polygons,
    inherit.aes = FALSE,
    colour = "#08519c",
    fill = "#08306b",
    alpha = .5,
    size = 1
  ) +
  labs(x = "", y = "")

Examples

The following is a complete example of using the osmdata package to retrieve data of hospitals in Lagos and ggmap to visualise them.

# install.packages(c("osmdata", "ggplot2", "ggmap"))
library(osmdata)
library(ggplot2)
library(ggmap)

# creating bounding box for Lagos
lagos_bb <- getbb("Lagos")

# retrieving data of hospitals in Lagos
lagos_hospitals <- lagos_bb %>%
  opq() %>%
  add_osm_feature(key = "amenity", value = "hospital") %>%
  osmdata_sf()

# retrieving map of lagos
lagos_map <- get_map(lagos_bb, maptype = "roadmap")

# visualising map of lagos overlayed by hospitals in lagos
ggmap(lagos_map) +
  geom_sf(
    data = lagos_hospitals$osm_polygons,
    inherit.aes = FALSE,
    colour = "#08519c",
    fill = "#08306b",
    alpha = .5,
    size = 1
  ) +
  labs(
    title = "Hospitals in Lagos(Nigeria)",
    x = "Latitude",
    y = "Longitude"
  )

The osmdata package can also be used to download other spatial features such as highways and rivers in Lagos, and overlay them on top of each other using ggplot2 to create a map of Lagos. For this we create multiple queries for each feature as follows.

library(osmdata)
library(ggplot2)

# creating bounding box for Lagos
lagos_bb <- getbb("Lagos")

# retrieving data of streets in Lagos
lagos_streets <- lagos_bb %>%
  opq() %>%
  add_osm_feature("highway", c("motorway", "primary", "secondary", "tertiary")) %>%
  osmdata_sf()

# retrieving data of small streets in Lagos
lagos_small_streets <- lagos_bb %>%
  opq() %>%
  add_osm_feature(key = "highway", value = c("residential", "living_street", "unclassified", "service", "footway")) %>%
  osmdata_sf()

# retrieving data of rivers in Lagos
lagos_rivers <- lagos_bb %>%
  opq() %>%
  add_osm_feature(key = "waterway", value = "river") %>%
  osmdata_sf()

# retrieving data of hospitals in Lagos
lagos_hospitals <- lagos_bb %>%
  opq() %>%
  add_osm_feature("amenity", "hospital") %>%
  osmdata_sf()

Then we use the ggplot2 package to visualise each of these features along with the hospitals in Lagos. Note how we have used different colours, line sizes (thicknesses) and opacities to differentiate among the features.

# visualising all retrieved features over each other to form a map of Lagos
ggplot() +
  geom_sf(data = lagos_streets$osm_lines, inherit.aes = FALSE, color = "#ffbe7f", size = .4, alpha = .8) +
  geom_sf(data = lagos_small_streets$osm_lines, inherit.aes = FALSE, color = "#a6a6a6", size = .2, alpha = .8) +
  geom_sf(data = lagos_rivers$osm_lines, inherit.aes = FALSE, color = "#7fc0ff", size = .8, alpha = .5) +
  geom_sf(data = lagos_hospitals$osm_polygons, inherit.aes = FALSE, colour = "#08519c", fill = "#08306b", alpha = .5, size = 1) +
  coord_sf(xlim = c(3.23, 3.56), ylim = c(6.4, 6.629), expand = FALSE) +
  theme_bw() +
  labs(
    title = "Hospitals in Lagos(Nigeria)",
    x = "Latitude",
    y = "Longitude"
  )

We can also use the leaflet package to visualise an interactive map of the hospitals in Lagos.

# install.packages("leaflet")
library(leaflet)

leaflet() %>%
  addTiles() %>%
  addPolygons(
    data = lagos_hospitals$osm_polygons,
    label = lagos_hospitals$osm_polygons$name
  )

References


Last updated: 2023-01-07
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/osm.Rmd

Tutorial was complied using: (click to expand)
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggmap_3.0.0               osmdata_0.1.5            
##  [3] malariaAtlas_1.0.1        ggthemes_4.2.4           
##  [5] here_1.0.1                MODIStsp_2.0.9           
##  [7] nasapower_4.0.7           terra_1.5-17             
##  [9] rnaturalearthhires_0.2.0  rnaturalearth_0.1.0      
## [11] viridis_0.5.1             viridisLite_0.3.0        
## [13] raster_3.5-15             sp_1.4-5                 
## [15] sf_1.0-7                  elevatr_0.4.2            
## [17] kableExtra_1.3.4          rdhs_0.7.2               
## [19] DT_0.17                   forcats_0.5.1            
## [21] stringr_1.4.0             dplyr_1.0.4              
## [23] purrr_0.3.4               readr_2.1.2              
## [25] tidyr_1.1.4               tibble_3.1.6             
## [27] tidyverse_1.3.1           openair_2.9-1            
## [29] leaflet_2.1.1             ggplot2_3.3.5            
## [31] rgeoboundaries_0.0.0.9000
## 
## loaded via a namespace (and not attached):
##   [1] readxl_1.3.1        backports_1.4.1     systemfonts_1.0.4  
##   [4] plyr_1.8.7          selectr_0.4-2       splines_4.0.3      
##   [7] storr_1.2.5         crosstalk_1.2.0     urltools_1.7.3     
##  [10] digest_0.6.27       htmltools_0.5.2     fansi_0.4.2        
##  [13] magrittr_2.0.1      memoise_2.0.1       cluster_2.1.0      
##  [16] gdalUtilities_1.2.1 tzdb_0.3.0          modelr_0.1.8       
##  [19] vroom_1.5.7         xts_0.12.1          svglite_1.2.3.2    
##  [22] prettyunits_1.1.1   jpeg_0.1-9          colorspace_2.0-3   
##  [25] rvest_1.0.2         rappdirs_0.3.3      hoardr_0.5.2       
##  [28] haven_2.5.0         xfun_0.30           rgdal_1.5-23       
##  [31] crayon_1.5.1        jsonlite_1.8.0      hexbin_1.28.2      
##  [34] progressr_0.10.1    zoo_1.8-8           countrycode_1.2.0  
##  [37] glue_1.6.2          gtable_0.3.0        webshot_0.5.2      
##  [40] maps_3.4.0          scales_1.1.1        DBI_1.1.2          
##  [43] Rcpp_1.0.7          progress_1.2.2      units_0.8-0        
##  [46] bit_4.0.4           mapproj_1.2.8       htmlwidgets_1.5.4  
##  [49] httr_1.4.2          RColorBrewer_1.1-2  wk_0.5.0           
##  [52] ellipsis_0.3.2      pkgconfig_2.0.3     farver_2.1.0       
##  [55] sass_0.4.0          dbplyr_2.1.1        utf8_1.1.4         
##  [58] crul_1.2.0          tidyselect_1.1.0    labeling_0.4.2     
##  [61] rlang_1.0.2         munsell_0.5.0       cellranger_1.1.0   
##  [64] tools_4.0.3         cachem_1.0.6        cli_3.2.0          
##  [67] generics_0.1.2      broom_0.8.0         evaluate_0.15      
##  [70] fastmap_1.1.0       yaml_2.2.1          knitr_1.33         
##  [73] bit64_4.0.5         fs_1.5.2            s2_1.0.7           
##  [76] RgoogleMaps_1.4.5.3 nlme_3.1-149        xml2_1.3.2         
##  [79] compiler_4.0.3      rstudioapi_0.13     curl_4.3.2         
##  [82] png_0.1-7           e1071_1.7-4         reprex_2.0.1       
##  [85] bslib_0.3.1         stringi_1.5.3       highr_0.9          
##  [88] gdtools_0.2.4       lattice_0.20-41     Matrix_1.2-18      
##  [91] classInt_0.4-3      vctrs_0.3.8         slippymath_0.3.1   
##  [94] pillar_1.7.0        lifecycle_1.0.1     triebeard_0.3.0    
##  [97] jquerylib_0.1.4     data.table_1.14.2   bitops_1.0-7       
## [100] R6_2.5.0            latticeExtra_0.6-29 KernSmooth_2.23-17 
## [103] gridExtra_2.3       codetools_0.2-16    MASS_7.3-53        
## [106] assertthat_0.2.1    rjson_0.2.20        rprojroot_2.0.2    
## [109] withr_2.5.0         httpcode_0.3.0      mgcv_1.8-33        
## [112] parallel_4.0.3      hms_1.1.1           grid_4.0.3         
## [115] class_7.3-17        rmarkdown_2.11      lubridate_1.8.0

Corrections: If you see mistakes or want to suggest additions or modifications, please create an issue on the source repository or submit a pull request
Reuse: Text and figures are licensed under Creative Commons Attribution CC BY 4.0.