If you need dplyr v0.3, uncomment the code below.

# library(devtools)
# install_github("hadley/lazyeval")
# install_github("hadley/dplyr")

Load the libraries.

library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(rgdal)
## Loading required package: sp
## rgdal: version: 0.9-1, (SVN revision 518)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.11.1, released 2014/09/24
## Path to GDAL shared files: /usr/local/Cellar/gdal/1.11.1/share/gdal
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
## Path to PROJ.4 shared files: (autodetected)
library(mullenMisc)
library(tidyr)
library(stringr)

Load the shapefiles.

county_1840 <- readOGR("shp_out/", "US_county_1840_conflated")
## OGR data source with driver: ESRI Shapefile 
## Source: "shp_out/", layer: "US_county_1840_conflated"
## with 1285 features and 18 fields
## Feature type: wkbPolygon with 2 dimensions
county_1850 <- readOGR("shp_out/", "US_county_1850_conflated")
## OGR data source with driver: ESRI Shapefile 
## Source: "shp_out/", layer: "US_county_1850_conflated"
## with 1632 features and 18 fields
## Feature type: wkbPolygon with 2 dimensions

Plot the shapefiles to make sure they work.

1840:

plot(county_1840)

plot of chunk unnamed-chunk-4

1850:

plot(county_1850)

plot of chunk unnamed-chunk-5

Fortify the objects from the sp package into a data frame that ggplot2 can use.

county_1840_df <- fortify(county_1840, region = "GISJOIN")
## Loading required package: rgeos
## rgeos version: 0.3-8, (SVN revision 460)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Polygon checking: TRUE
county_1850_df <- fortify(county_1850, region = "GISJOIN")

Plot each of those in ggplot. (Notice the arguments to coord_map() which produce an Albers equal area projection.)

ggplot(data = county_1840_df,
       aes(x = long, y = lat, group = group)) +
  geom_map(map = county_1840_df, aes(map_id = id)) +
  coord_map(projection = "albers", at0 = 45.5, lat1 = 29.5)

plot of chunk unnamed-chunk-7

ggplot(data = county_1850_df,
       aes(x = long, y = lat, group = group)) +
  geom_map(map = county_1850_df, aes(map_id = id)) +
  coord_map(projection = "albers", at0 = 45.5, lat1 = 29.5)

plot of chunk unnamed-chunk-8

Now read in the data and codebooks.

data_1840 <- read.csv("csv/nhgis0034_ds7_1840_county.csv",
                      stringsAsFactors = FALSE)
data_1850 <- read.csv("csv/nhgis0034_ds10_1850_county.csv",
                      stringsAsFactors = FALSE)
code_1840 <- parse_nhgis_codebook("csv/nhgis0034_ds7_1840_county_codebook.txt")
code_1850 <- parse_nhgis_codebook("csv/nhgis0034_ds10_1850_county_codebook.txt")
county_1840_df<-county_1840_df %>%
  left_join(data_1840, by=c("id" = "GISJOIN"))%>%
  mutate(slave_percentage = ACS003 /(ACS001+ACS002+ ACS003))
# str(county_1840_df)
plot1 <- ggplot(data = county_1840_df,
       aes(x = long, y = lat, group = group, fill=slave_percentage)) +
  geom_map(map = county_1840_df, aes(map_id = id)) +
  coord_map(projection = "albers", at0 = 45.5, lat1 = 29.5)
county_1850_df<-county_1850_df %>%
  left_join(data_1850, by=c("id" = "GISJOIN"))%>%
  mutate(slave_percentage = AE6003 /(AE6001+AE6002+ AE6003))
# str(county_1840_df)
plot2 <- ggplot(data = county_1850_df,
       aes(x = long, y = lat, group = group, fill=slave_percentage)) +
  geom_map(map = county_1850_df, aes(map_id = id)) +
  coord_map(projection = "albers", at0 = 45.5, lat1 = 29.5)
require(gridExtra)
## Loading required package: gridExtra
## Loading required package: grid
grid.arrange(plot1, plot2, ncol=1)

plot of chunk unnamed-chunk-14