Skip to content

Instantly share code, notes, and snippets.

@tomschenkjr
Created November 7, 2012 22:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tomschenkjr/4035138 to your computer and use it in GitHub Desktop.
Save tomschenkjr/4035138 to your computer and use it in GitHub Desktop.
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=crimes, 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.
@finestjava
Copy link

Regions defined for each Polygons
Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, :
object 'crimes' not found

Any ideas what causes this R error?

OK figured the issue out

crime <- read.csv("http://data.cityofchicago.org/api/views/qnrb-dui6/rows.csv")
geom_point(data=crimes, aes(x=X.Coordinate, y=Y.Coordinate))

Also confusing was the two new directories without explanation - Neighborhoods_2012/Neighborhoods_2012b
I just dumped all the shape files in the original - (unzip to your working directory and rename "Chicago Neighborhoods")

I did get a map out of it - but it is 95% black - No real way to evaluate
But it was a good R tutorial for error searching code.

Calling it crime in one line - Referencing it as crimes in the second.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment