Last Updated: May 06, 2020

Use leaflet and our previously used Baltimore crime dataset to make an interactive data map of Baltimore Crime.

  1. Use this piece of code to download and prepare data for use in project
library(tidyverse)
library(stringr)

arrest_tab <- read_csv("http://www.hcbravo.org/IntroDataSci/misc/BPD_Arrests.csv")
dat <- arrest_tab %>% 
  filter(!is.na(`Location 1`)) %>%
  separate(`Location 1`, c("lat","lng"), sep=",") %>%
  mutate(lat=as.numeric(str_replace(lat, "\\(", ""))) %>%
  mutate(lng=as.numeric(str_replace(lng, "\\)", ""))) %>%
  sample_n(2000)
dat
## # A tibble: 2,000 x 16
##    arrest   age race  sex   arrestDate arrestTime arrestLocation incidentOffense
##     <dbl> <dbl> <chr> <chr> <chr>      <time>     <chr>          <chr>          
##  1 1.26e7    18 B     M     11/22/2012 17:55      600 N Carroll… 87-Narcotics   
##  2 1.24e7    39 B     M     02/17/2012 10:54      800 Vine St    87-Narcotics   
##  3 1.26e7    27 B     M     12/15/2012 00:20      400 W Fayette… 87-Narcotics   
##  4 1.26e7    21 W     F     09/17/2012 11:00      2300 E Presto… 87-Narcotics   
##  5 1.24e7    28 B     M     01/21/2012 19:15      2600 E Monume… 97-Search & Se…
##  6 1.13e7    20 B     M     09/01/2011 18:46      600 Mlk Blvd   6J-Larceny- Ot…
##  7 1.24e7    54 B     M     01/18/2012 19:00      1900 N Roseda… 97-Search & Se…
##  8 1.14e7    27 B     F     10/31/2011 13:00      600 N Dukelan… 24-Towed Vehic…
##  9 1.25e7    40 B     F     06/21/2012 01:35      4600 Edmondso… Unknown Offense
## 10 1.25e7    24 B     M     08/25/2012 02:45      1500 Poplar G… 79-Other       
## # … with 1,990 more rows, and 8 more variables: incidentLocation <chr>,
## #   charge <chr>, chargeDescription <chr>, district <chr>, post <dbl>,
## #   neighborhood <chr>, lat <dbl>, lng <dbl>

Note the attributes lat and lng which indicate geographical location as latitude (lat) and longitude (lng).

  1. Use the leaflet package to create an interactive map of Baltimore
library(leaflet)

balto_map <- leaflet(dat) %>%
  addTiles() %>%
  setView(lat=39.29, lng=-76.61, zoom=11)
balto_map

You can find more information about leaflet here: https://rstudio.github.io/leaflet/

  1. Add graphical elements to display the data. For instance, add circles, with colors indicating sex. Or circles with colors indicating race. Or anything else that strikes your fancy.

These will be useful:

  1. Embed your map in your Rmarkdown file, knit to HTML this time (not PDF) and submit the HTML file to ELMS.

Submission

Prepare and knit an Rmarkdown file that includes: (a) code to carry out each of the steps above, (b) output showing the result of your code (in this case the interactive map), and (c) a short prose description of your interactive map (i.e., what are you showing with this data and map). Remember, the writeup you are preparing is intended to communicate your data analysis effectively. Thoughtlessly showing large amounts of output in your writeup defeats that purpose.

Python

You can use the ipyleaflet widget https://github.com/jupyter-widgets/ipyleaflet to implement this in a Jupyter notebook. To submit, export your notebook as HTML and submit the resulting html to ELMS.

Code to prepare data:

import pandas as pd

arrest_tab = pd.read_csv("http://www.hcbravo.org/IntroDataSci/misc/BPD_Arrests.csv")

dat = arrest_tab[arrest_tab['Location 1'].notna()].copy()
dat['lat'] = dat['Location 1'].str.extract("\(([0-9\.\-]*).*\)", expand=False)
dat['lng'] = dat['Location 1'].str.extract("\(.*,(.*)\)", expand=False)
dat = dat.sample(n=2000)
dat
##            arrest  age  ...            lat              lng
## 21079  11228179.0   43  ...  39.3041941662   -76.5825949197
## 25385  11251688.0   33  ...  39.2838826652   -76.6526803509
## 90588  12550510.0   29  ...  39.2913058814   -76.6109759020
## 80633  12510228.0   47  ...  39.2832840876   -76.6415732194
## 41236  11342236.0   34  ...  39.3285689687   -76.5379470973
## ...           ...  ...  ...            ...              ...
## 46617  11367459.0   17  ...  39.3175130326   -76.5989345586
## 59950  12426639.0   28  ...  39.3022106822   -76.5853818828
## 29524  11276843.0   38  ...  39.3188357266   -76.5871991736
## 29617  11277189.0   37  ...  39.2822231310   -76.5301553611
## 52889  12395946.0   38  ...  39.3048082251   -76.6291432190
## 
## [2000 rows x 17 columns]