I'm trying to use the maps and mapdata packages with ggplot, but I can't seem to extract the coordinates out of the data. Specifically, I'm trying to plot certain US states and Canadian provinces.
library(maps)
library(mapdata)
library(ggplot2)
library(ggthemes)
library(raster)
us <- getData("GADM", country = "USA", level = 1)
canada <- getData("GADM", country = "CAN", level = 1)
states <- c('connecticut', 'new york', 'new jersey', 'massachusetts')
provinces <- c('Ontario', 'Alberta', 'Quebec')
us.states <- us[us$NAME_1 %in% states, ]
ca.provinces <- canada[canada$NAME_1 %in% provinces, ]
ggplot(us.states, aes(x = longitude, y = latitude, group = group)) +
geom_path()+
geom_path(data = ca.provinces)+
coord_map()
and I get an error:
Regions defined for each Polygons
Regions defined for each Polygons
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) :
invalid graphics state
Any help here? I'm new to mapping in R.