Adding bold borders between the US and Canada in r.

I am currently working with geographic data to map them. I have two different shapefiles. I want to do this to add a bold border between Canada and the US. I do not know how to do that.

# Load packages ----------------------------------------------------------------
library(dplyr)
library(readr)
library(ggplot2)
library(rgdal)
library(ggmap)

# Load data --------------------------------------------------------------------
canada <- readOGR(dsn = "00-raw/gcd_000b11a_e/", layer = "canada") %>% fortify()
us <- readOGR(dsn = "00-raw/usmaps/us/", layer = "co99_d90") %>% fortify()

# Maps -------------------------------------------------------------------------
gg <- ggplot() + geom_map(data=canada, map=canada,
                    aes(long, lat, map_id=id),
                    size=0.1, fill=NA, color = "black") + 
    coord_map("stereographic", xlim=c(-120, -60)) +
    geom_map(data=us, map=us,
             aes(long, lat, map_id=id),
             size=0.1, fill=NA, color = "black") +
    theme_nothing(legend = T)

      

enter image description here

+3


source to share


1 answer


Use the package's geospatial statistics function sf

to find the "shape" boundary. Then just sketch over it.



library(sf)
border <- st_intersection(us, canada)
gg + geom_map(data=border, map=us,
         aes(long, lat,
         size=0.1, fill=NA, color = "red")

      

+3


source







All Articles