1

I am using ggplot2 to plot a map of the United States and Canada, and I want to display outlines of the US States and the Canadian Provinces. I was able to outline the US States using map_data("state") within geom_polygon() function, but get an error that map_data("Province") or map_data("Canada"), or anything associated with Canada, is not an exported object within maps.

The attached photo is my current output - I would like it to look exactly the same but with the Canadian Province outlines. My current code is below.

library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)
library(raster)

#State/Province outlines
states <- map_data("state")
provinces <- map_data("province")

ggplot(data = world) + 
  geom_sf() +
  xlab("Longitude") +
  ylab("Latitude") +
  geom_polygon(data = states, aes(x = long, y = lat, group = group), color = "black", fill = "antiquewhite") +
  coord_sf(xlim = c(-130, -65), ylim = c(25,58.5), expand = FALSE) + 
  annotate(geom = "text", x = -100, y = 40, label = "United States", fontface = "italic", color = "grey22", size = 3) +
  annotate(geom = "text", x = -100, y = 52.5, label = "Canada", fontface = "italic", color = "grey22", size = 3) +
  theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), 
        panel.background = element_rect(fill = "paleturquoise1"))

Current Output

Ryan
  • 19
  • 1
  • 5
  • 2
    Related: https://stackoverflow.com/questions/29421436/adding-provinces-to-canadian-map-in-r – MrFlick Feb 12 '20 at 16:27
  • 1
    Do did you try `map_data("world", "Canada")`? From here: https://stackoverflow.com/questions/22242038/color-grid-cells-in-the-united-states-and-canada – MrFlick Feb 12 '20 at 16:27
  • 1
    Also here: https://stackoverflow.com/questions/24072075/mapping-specific-states-and-provinces-in-r – MrFlick Feb 12 '20 at 16:28
  • 1
    And here: https://stackoverflow.com/questions/10763421/r-creating-a-map-of-selected-canadian-provinces-and-u-s-states – MrFlick Feb 12 '20 at 16:28

1 Answers1

2

The maps package (which holds the "state" map) doesn't have an equivalent for Canada. But it is relatively easy to retrieve such a map from different sources. I see you alrteady have rnaturalearth loaded, so you can try

state_prov <- rnaturalearth::ne_states(c("united states of america", "canada"))

should give you a map state-level map of USA and Canada together. Alternatively, you could try GADM:

library(raster)
states <- getData(country="USA", level=1)
provinces <- getData(country="Canada", level=1)

It's best to use the same source for both countries, that way the borders will match.

Alex Deckmyn
  • 1,017
  • 6
  • 11