One neighbour

Contiguity analysis
R
spatial
Author

Michaël

Published

2023-03-26

Modified

2024-12-06

Today I saw this :

A Mastodon post : Portugal 🇵🇹 borders only one country: Spain 🇪🇸

So, Portugal has only one terrestrial neighbour. Opencage lists the other countries with only one neighbour.

Can we get this by ourselves ?

library(tidyverse)
library(rnaturalearth)
library(spdep)

# get data from Natural Earth
countries <- ne_countries(scale = 10, returnclass = "sf") |> 
  st_make_valid() |> 
  group_by(sov_a3, sovereignt) |> 
  summarise(.groups = "drop")

# for joining codes and names later
lookup <- countries |> 
  st_drop_geometry()

# get neighbours with {spdep}
neighbours <- countries |> 
  poly2nb()

# we only keep countries with 1 neighbour
# and add names
neighbours |> 
  set_names(countries$sov_a3) |> 
  keep(\(x) length(x) == 1) |> 
  enframe("sov_a3", "nb") |> 
  unnest(nb) |> 
  filter(nb != 0) |> 
  mutate(nb = countries$sov_a3[nb]) |> 
  left_join(lookup, by = "sov_a3") |> 
  left_join(lookup, by = c("nb" = "sov_a3"), suffix = c("", "_nb")) |> 
  relocate(sovereignt, .after = 1)
# A tibble: 16 × 4
   sov_a3 sovereignt         nb    sovereignt_nb           
   <chr>  <chr>              <chr> <chr>                   
 1 BRN    Brunei             MYS   Malaysia                
 2 CAN    Canada             US1   United States of America
 3 DN1    Denmark            DEU   Germany                 
 4 DOM    Dominican Republic HTI   Haiti                   
 5 GMB    Gambia             SEN   Senegal                 
 6 HTI    Haiti              DOM   Dominican Republic      
 7 IRL    Ireland            GB1   United Kingdom          
 8 KOR    South Korea        PRK   North Korea             
 9 LSO    Lesotho            ZAF   South Africa            
10 MCO    Monaco             FR1   France                  
11 PNG    Papua New Guinea   IDN   Indonesia               
12 PRT    Portugal           ESP   Spain                   
13 QAT    Qatar              SAU   Saudi Arabia            
14 SMR    San Marino         ITA   Italy                   
15 TLS    East Timor         IDN   Indonesia               
16 VAT    Vatican            ITA   Italy                   

I miss Bahrain / Saudi Arabia because the Natural Earth dataset is not detailed enough…

Map of King Fahd Passport Island

King Fahd Passport Island

And the « new » Canada / Denmark border is also not taken into account…

While we are at it, which country has more neighbors?

neighbours |> 
  set_names(countries$sov_a3) |> 
  unclass() |> 
  enframe("sov_a3", "nb") |> 
  unnest(nb) |> 
  count(sov_a3, sort = TRUE) |> 
  left_join(lookup, by = "sov_a3") 
# A tibble: 209 × 3
   sov_a3     n sovereignt                      
   <chr>  <int> <chr>                           
 1 CH1       15 China                           
 2 RUS       14 Russia                          
 3 BRA       11 Brazil                          
 4 FR1       11 France                          
 5 COD        9 Democratic Republic of the Congo
 6 DEU        9 Germany                         
 7 AUT        8 Austria                         
 8 SDN        8 Sudan                           
 9 SRB        8 Republic of Serbia              
10 TUR        8 Turkey                          
# ℹ 199 more rows

So, China…

However it includes the disputed part of « Kashmir », claimed by India and China, so it’s 14 neighbours, like Russia.