I want to plot a geospatial map using sf
and plotly
package. However, I don't have country-level data, only region-level data (Asia, Europe, Latin America, etc.). This is the mock-up of the dataset I will be working on, I'm looking for a way to lookup the longitude, latitude, and geometry. I plan to collect all the data in Excel first, and later move them to R
for analysis
I've looked for several packages/datasets e.g., rnaturalearth
, maps::map
, etc, but I couldn't find the correct latitude/longitude/coordinates or the geometry of the regions. Where can I find them? Thanks in advance
here you have some thoughs about your question:
.gpkg
format.Please find here how to produce a dataset as the one you mention.
I would use here the geographic regions classification of the UN. This is available on the countrycode
package so basically I map each ISO-3 country code to the corresponding region. After, I group the countries by region so I can have a single geometry by region. By last, I extract the bounding boxes and append them to the dataset:
library(giscoR)
library(countrycode)
library(sf)
world <- gisco_get_countries()
# Add the subregion
world$region <- countrycode(world$ISO3_CODE,
origin = "iso3c",
destination = "un.regionsub.name")
#> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: ATA, CPT, XA, XB, XC, XD, XE, XF, XG, XH, XI, XL, XM, XN, XO, XU, XV
unique(world$region)
#> [1] "Latin America and the Caribbean" "Polynesia"
#> [3] "Western Europe" NA
#> [5] "Southern Europe" "Western Asia"
#> [7] "Southern Asia" "Sub-Saharan Africa"
#> [9] "Australia and New Zealand" "Eastern Europe"
#> [11] "Northern America" "South-eastern Asia"
#> [13] "Eastern Asia" "Micronesia"
#> [15] "Northern Europe" "Melanesia"
#> [17] "Central Asia" "Northern Africa"
# Group regions
library(dplyr)
library(ggplot2)
subworld <- world %>%
group_by(region) %>%
# Mock the data field
summarise(data=n())
ggplot(subworld) +
geom_sf(aes(fill=region))
# Create final table
bbox <- lapply(st_geometry(subworld), st_bbox) %>%
bind_rows() %>%
mutate(across(where(is.numeric), as.double))
# Final dataset
end <- subworld %>% bind_cols(bbox) %>%
select(region, data, xmin, xmax, ymin, ymax)
head(end)
#> Simple feature collection with 6 features and 6 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -180 ymin: -59.51912 xmax: 180 ymax: 81.85799
#> Geodetic CRS: WGS 84
#> # A tibble: 6 x 7
#> region data xmin xmax ymin ymax geometry
#> <chr> <int> <dbl> <dbl> <dbl> <dbl> <MULTIPOLYGON [°]>
#> 1 Australia and New ~ 6 -178. 179. -53.2 -9.21 (((143.5617 -13.77157, 1~
#> 2 Central Asia 5 46.6 87.3 35.2 55.4 (((50.3399 45.05235, 50.~
#> 3 Eastern Asia 7 73.6 146. 18.2 53.5 (((120.8954 22.336, 120.~
#> 4 Eastern Europe 10 -180 180. 41.2 81.9 (((68.7333 76.49168, 68.~
#> 5 Latin America and ~ 48 -118. 3.43 -59.5 32.7 (((-64.01772 -54.74885, ~
#> 6 Melanesia 5 -180 180. -22.7 -0.826 (((178.2331 -17.34448, 1~
Created on 2022-03-23 by the reprex package (v2.0.1)