ggplot2 Workshop Homework for Maps
# TITLE: ggplot2 Workshop Homework for Maps # AUTHOR: Tom Schenk Jr. # DATE CREATED: November 7, 2012 # DATE MODIFIED: None # PURPOSE: Create maps using R and ggplot2 library. # LIBRARIES: ggplot2, maptools library(ggplot2) library(maptools) setwd("") # Enter the location of your working directory. # Let's place a map of crime on Chicago's boundaries. Thus, we need to load the shapefiles and crime data for Chicago. # First, load Chicago's shapefile. You can find Chicago's boundaries here (unzip to your working directory and rename "Chicago Neighborhoods"): https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-City/q38j-zgre chicago <- readShapePoly("Neighborhoods_2012/Neighborhoods_2012b") # Read the shapefile (from the 'maptools' library). chicago <- fortify(chicago) # Organizes data into a neat data frame. ggplot(chicago) + geom_polygon(aes(x=long, y=lat, group=group)) # "Filled" map ggplot(chicago) + geom_path(aes(x=long, y=lat, group=group)) # "Outline" map # Notice that the original city data is not in traditional latitutde and longitudes, # instead, it is coded as Universal Transverse Mercator (UTM). # Now, load city crime data. crime <- read.csv("http://data.cityofchicago.org/api/views/qnrb-dui6/rows.csv") # Simple plot of crime and wards # Use X.Coordinate and Y.Coordinate from crime data set since they're the UTM coordinates. ggplot(chicago) + geom_path(aes(x=long, y=lat, group=group)) + geom_point(data=crime, aes(x=X.Coordinate, y=Y.Coordinate)) # Some challenges: # (1) Find patterns in crimes across the city. # (2) Load another shapefile from the city to display with crime and neighborhood boundaries: https://data.cityofchicago.org/browse?category=Facilities+%26+Geographic+Boundaries&q=shapefiles&sortBy=relevance # (3) Derive some hypothesis about what can cause crime, see if you can find data at http://data.cityofchicago.org (or elsewhere) and map it.