library(sp)
library(rgdal)
## 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(rgeos)
## rgeos version: 0.3-8, (SVN revision 460)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Polygon checking: TRUE
library(maptools)
## Checking rgeos availability: TRUE
library(ggmap)
## Loading required package: ggplot2
library(ggplot2)
map_sp <- readOGR("/Users/rikku8221/Desktop/Mapping/nhgis-shp/","state_1860")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/rikku8221/Desktop/Mapping/nhgis-shp/", layer: "state_1860"
## with 41 features and 7 fields
## Feature type: wkbPolygon with 2 dimensions
plot(map_sp)
title("Map of United States")

plot of chunk unnamed-chunk-2

class(map_sp)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"

Since I wasn’t sure what to map first, I began with places that I have lived over the years. These are listed below in the cities dataframe.

cities <- data.frame(name = c("Dyersburg,TN",
                    "Murfreesboro,TN",
                    "Lake Mary, FL",
                    "Oviedo, FL",
                    "Alexandria, VA"),
           stringsAsFactors = FALSE)

Now we can get the geocodes for these cities.

cities_geocoded <- geocode(cities$name)
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Dyersburg,TN&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Murfreesboro,TN&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Lake+Mary,+FL&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Oviedo,+FL&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Alexandria,+VA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
cities <- cbind(cities, cities_geocoded)
ggplot(cities, aes(x = lon, y = lat)) +
  geom_point() +
  geom_text(aes(label = name), vjust = -1) +
  coord_map()

plot of chunk unnamed-chunk-4

map_df <- fortify(map_sp, region = "GISJOIN")
map_1860 <- ggplot() + 
  geom_map(data = map_df,
           map = map_df,
           aes(x = long, y = lat, group = group, map_id = id),
           fill = "white",
           color = "black",
           size = 0.2) +
  coord_map() +
  theme_minimal()

map_1860

plot of chunk unnamed-chunk-5

map_1860 +
  geom_point(data = cities, aes(x = lon, y = lat),
             color = "blue", size = 3) +
  geom_text(data = cities, aes(x = lon, y = lat, label = name),
            vjust = -1)

plot of chunk unnamed-chunk-5 Now I’m going to map the cities where I lived on an actual map!

map_1860 +
  geom_point(data = cities,
             aes(x = lon, y = lat, size = 1),
             color = "blue", shape = 1) +
  theme(legend.position = "bottom") +
  scale_size(range = c(2, 8)) + 
  ggtitle("Cities Where Anne Lived")

plot of chunk unnamed-chunk-6 This one shows the density of where I lived, rather than just locations.

map_1860 +
  geom_density2d(data = cities,
             aes(x = lon, y = lat)) +
  theme(legend.position = "bottom") +
  ggtitle("Cities Where Anne Lived")

plot of chunk unnamed-chunk-7 Now that I have figured it out with relatively meaningless information, I decided to map some of the early game development hotspots in the United States. Although the map is old, and it does not have all of the states, it gives an idea of where they were.

cities <- data.frame(name = c("Cambridge, MA",
                    "Salt Lake City, UT",
                    "Sunnyvale, CA",
                    "Nashua, NH"),
           stringsAsFactors = FALSE)
cities_geocoded <- geocode(cities$name)
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Cambridge,+MA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Salt+Lake+City,+UT&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Sunnyvale,+CA&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Nashua,+NH&sensor=false
## Google Maps API Terms of Service : http://developers.google.com/maps/terms
cities <- cbind(cities, cities_geocoded)
ggplot(cities, aes(x = lon, y = lat)) +
  geom_point() +
  geom_text(aes(label = name), vjust = -1) +
  coord_map()

plot of chunk unnamed-chunk-9

map_df <- fortify(map_sp, region = "GISJOIN")
map_1860 <- ggplot() + 
  geom_map(data = map_df,
           map = map_df,
           aes(x = long, y = lat, group = group, map_id = id),
           fill = "white",
           color = "black",
           size = 0.2) +
  coord_map() +
  theme_minimal()

map_1860

plot of chunk unnamed-chunk-11

map_1860 +
  geom_point(data = cities, aes(x = lon, y = lat),
             color = "red", size = 3) +
  geom_text(data = cities, aes(x = lon, y = lat, label = name),
            vjust = -1)

plot of chunk unnamed-chunk-11

map_1860 +
  geom_point(data = cities,
             aes(x = lon, y = lat, size = 1),
             color = "red", shape = 1) +
  theme(legend.position = "bottom") +
  scale_size(range = c(2, 8)) + 
  ggtitle("Early Game Development")

plot of chunk unnamed-chunk-12