-1

I am trying to rbind the US and Canada census maps using ggplot2.

us <- readOGR(dsn = "00-raw/usmaps/us/", layer = "co99_d90")
canada <- readOGR(dsn = "00-raw/gcd_000b11a_e/", layer = "canada")

canada$id <- as.numeric(canada$id)
us$id <- as.numeric(us$id)

canada$id <- canada$id + length(unique(us$id))
na <- rbind(canada, us)

p <- ggplot() +
    geom_polygon(data = na, aes(x = long, y = lat, group = group, fill = pop),
                 color = "black", size = 0.25) +
    theme_nothing(legend = TRUE)

But there is some weird line.

enter image description here

The sources of the shapefiles are the following:

http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gcd_000b11a_e.zip

www2.census.gov/geo/tiger/PREVGENZ/co/co90shp/co99_d90_shp.zip

I really need those shapefiles since I want my borders to represent the counties for the US and the census division for Canada.

  • Can you add the source of the shapefiles? – Alex Jul 04 '16 at 22:34
  • Or, perhaps identify what you are actually trying to accomplish as there may be far more optimal ways than using two seemingly random shapefiles. – hrbrmstr Jul 04 '16 at 23:08

1 Answers1

0

I made simplified versions of both shapefiles and put them into individual geojson files here.

You don't need to bind data to the fortified data frames if you use geom_map(). I have no idea if you need just ConUS or all US states & territories, so I split the diff and included Alaska. YMMV.

I simulated some data since we also don't have that from you.

library(rgdal)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggthemes)
library(viridis)

canada <- readOGR("canada.geojson", "OGRGeoJSON", 
                  verbose=FALSE, stringsAsFactors=FALSE)
usa <- readOGR("usacounties.geojson", "OGRGeoJSON", 
               verbose=FALSE, stringsAsFactors=FALSE)

ca_map <- fortify(canada, region="CDUID")
us_map <- fortify(usa, region="CO99_D90_I")

set.seed(1492)
ca_pop <- data.frame(id=unique(canada$CDUID),
                     val=sample(100000, length(unique(canada$CDUID))),
                     stringsAsFactors=FALSE)
us_pop <- data.frame(id=unique(usa$CO99_D90_I),
                     val=sample(100000, length(unique(usa$CO99_D90_I))),
                     stringsAsFactors=FALSE)

gg <- ggplot()
gg <- gg + geom_map(data=ca_map, map=ca_map,
                    aes(long, lat, map_id=id),
                    size=0.1, fill=NA, color="#2b2b2b")
gg <- gg + geom_map(data=ca_pop, map=ca_map,
                    aes(fill=val, map_id=id))
gg <- gg + geom_map(data=us_map, map=us_map,
                    aes(long, lat, map_id=id),
                    size=0.1, fill=NA, color="#2b2b2b")
gg <- gg + geom_map(data=us_pop, map=us_map,
                    aes(fill=val, map_id=id))
gg <- gg + scale_fill_viridis(name="Population")
gg <- gg + coord_map(xlim=c(-170, -55), ylim=c(23.2, 80))
gg <- gg + theme_map()
gg

enter image description here

I'm 99% sure you're trying to show too much information but we don't really know what you're trying to do. If you are attempting to show population by US county and Canadian census id's then I'm 100% sure you're trying to show too much info.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Wow thank you so much. I am a research assistant for a group who is replicating the Chetty et al. 2014 paper about social mobility. The population on the first map was only a generated sample. – Charles_de_Montigny Jul 05 '16 at 03:33