This seems a bit cumbersome, but it's a starter. I hope someone drops by and improves on this solution, or, well, half of a solution.
For the region boundaries in Macedonia, you will need to look at the data table and try to come up with a better subsetting, but this is a good start.
---
title: "Adm 1 Poly Makedonia"
author: "M_AcostaCH"
date: "`r Sys.Date()`"
thread: https://community.rstudio.com/t/how-put-the-shape-in-the-exactly-position-of-a-country/163639
output: html_document
runtime: shiny
---
```{r}
library(leaflet)
library(osmdata)
library(tidyverse)
library(ggmap)
# trying a working example with OSM data and stamen background map,
# because ggmap cannot get OSM tiles for some reason
# get the bbox. this looks ok
mkd_bb <- getbb("Macedonia", featuretype = "country")
# query overpass api. ok.
mkd_bb %>% opq()
# get the hospitals in Macedonia. ok so far
makedonia_hospitals <- mkd_bb %>%
opq() %>%
add_osm_feature(key = "amenity", value = "hospital") %>%
osmdata_sf()
makedonia_hospitals
# extract the hospital polygons. still ok.
mypoly <- makedonia_hospitals$osm_polygons
# plot without map background
ggplot() +
geom_sf(data = makedonia_hospitals$osm_polygons)
# get the map background
mkmap <- get_map(mkd_bb, source = "stamen", maptype = "toner-hybrid")
ggmap(mkmap) +
geom_sf(
data = mypoly,
inherit.aes = FALSE,
colour = "#125421",
fill = "#329832",
alpha = .5,
size = 1
) +
labs(x = "", y = "")
# looks gorgeous! Placement is just fine
leaflet() %>% addTiles() %>% addPolygons(data = mypoly)
# now to something completely different
# let's look at the available features. "boundary" is one of them
available_features()
# what tags are in boundary?
available_tags("boundary")
# this follows the exact same structure
# DO NOT RUN THIS, IT WILL DOWNLOAD 3.5 GIGABYTES OF DATA
makedonia_boundaries <- mkd_bb %>%
opq() %>%
add_osm_feature(key = "boundary", value = "administrative") %>%
osmdata_sf()
# this tries to limit the amount of downloaded data
# it still downloads over 800 MB of data, which is unnecessary
# For Northern Macedonia:
# admin_level=2 is supposedly the right admin level for the country
# admin_level=4 are regions and
# admin_level=7 are municipalities
adm <- mkd_bb %>%
opq() %>%
add_osm_feature(key = "boundary", value = "administrative") %>%
add_osm_feature(key = "admin_level",value = "4") %>%
osmdata_sf()
# out <- osmdata_sf(adm)
adm_poly <- adm$osm_polygons
adm_points <- adm$osm_points
adm_lines <- adm$osm_lines
# although the bbox is the same as above, this is weird
ggplot() +
geom_sf(data = adm_poly)
# and here we can see why
leaflet() %>% addTiles() %>% addPolygons(data = adm_poly)
# this is better
leaflet() %>% addTiles() %>% addPolylines(data = adm_lines)
# subset the rows to just get Macedonia borders
# this explores how to filter the lines by two different columns with
# a colon character (:) in them, which is not trivial. One would expect
# an " around the column names, but instead, you need to use backticks, `.
lkj <- subset(adm_lines, `left:country` == "North Macedonia" | `right:country` == "North Macedonia")
# display in map
leaflet() %>% addTiles() %>% addPolylines(data = lkj)