MapChi is a collection of tools for making high quality maps of Chicago. The functions in MapChi generally serve two purposes:

  1. Geocoding addresses in batch
  2. Making maps

This vignette focuses on making maps.

Basic Maps

The most basic mapping function in MapChi is called, fittingly, map_chi(). The function returns an object of class 'ggplot,' which can be further modified using ggplot2. There are five arguments:

library(MapChi)
library(ggplot2)

district_map <- map_chi(background = "grey", lines = "blue", regions = "districts", title_size = 13)
district_map

data(hom_14)

# Plot homicides and add a title. 
district_map + geom_point(data = hom_14, aes(X.Coordinate, Y.Coordinate), size = .1) + ggtitle("Homicides in Chicago: 2014")

Heat Maps

Maps are commonly used to illustrate regional variation along a dimension of interest. For example, a map of violent crime rates in Chicago neighborhoods might color high crime neighborhoods with a dark blue and low crime neighborhoods with light blue. The basic workflow for making heat maps in MapChi is straightforward:

  1. Create a data frame that summarizes the variation of interest. In the above example the table would be 77 X 2 (one column to store the community areas--77 in total--and one to store the violent crime rates).

  2. Use heat_map_continuous() or heat_map_discrete() to create the map. Making a discrete heat map typically requires the intermediate step of grouping the outcome into finite bins.

library(dplyr)

# Create summary table. 
hom_sum <- dplyr::summarise(group_by(hom_14, Community.Area), homicides = n())

heat_map_continuous(regions = "CAs", summary_df = hom_sum, regions_var = "Community.Area", fill_var = "homicides", 
                    legend_name = "Homicides", na_replace = 0, title = "Homicides by Community Area: 2014", title_size = 13)

Most of the parameters are self explanatory, but a few aren't:

heat_map_continuous(regions = "CAs", summary_df = hom_sum, regions_var = "Community.Area", fill_var = "homicides", legend_name = "Homicides", title = "Homicides by Community Area: 2014", title_size = 13)

In my experience, discrete heat maps are usually a bit easier to interpret than continuous heat maps. Making a discrete map is a more labor intensive than making a continuous map, but the additional steps are pretty straightforward.

  1. After creating the summary table, you need to divide the outcome of interest into bins. For example, we might discretize the homicide variable by grouping community areas into ranges: 0-5 homicides, 6-10, 11-15, 16-20, 21-30, 31-40.

  2. You need to specify a set of discrete colors to match the bins. I recommend using RColorBrewer for this step.

Also, there is no NA replace option, so if missing values are an issue they need to be handled before calling heat_map_distrete()

```r library(RColorBrewer)

Manually add community areas with zero homicides

zero_homs <- data.frame(Community.Area = c(1:77)[!c(1:77) %in% hom_sum$Community.Area], homicides = 0) hom_sum <- rbind(hom_sum, zero_homs)

Group into bins

hom_sum$range <- hom_sum$range <- cut(hom_sum$homicides, c(0, 5, 10, 15, 20, 30, 40), include.lowest = TRUE, labels = c("0-5", "6-10", "11-15", "16-20", "21-30", "31-40"))

Define palette.

colors <- brewer.pal(n = 6, name = "BuPu") heat_map_discrete(regions = "CAs", summary_df = hom_sum, regions_var = "Community.Area", fill_var = "range", palette = colors, legend_name = "Homicides", title = "Homicides by Community Area: 2014", title_size = 13)



dmwelgus/MapChi documentation built on May 15, 2019, 9:38 a.m.