Skip to content

Instantly share code, notes, and snippets.

@bradoyler

bradoyler/.block

Last active Jun 29, 2020
Embed
What would you like to do?
crime rates in US cities
license: mit

Crime rates in US cities

By using the number of murders in large cities (over 250k) combined with population, we can determine the murder rate in those cities.

Data

NAME STATE latitude longitude POP_2010
Buffalo NY 42.88644680000 -78.87836890000 261310.00000000000
Boston MA 42.35843080000 -71.05977320000 617594.00000000000
Chicago IL 41.85003300000 -87.65005230000 2695598.00000000000
Toledo OH 41.66393830000 -83.55521200000 287208.00000000000
Cleveland OH 41.49949540000 -81.69540880000 396815.00000000000
Philadelphia PA 39.95233500000 -75.16378900000 1526006.00000000000
Pittsburgh PA 40.44062480000 -79.99588640000 305704.00000000000
Baltimore MD 39.29038480000 -76.61218930000 620961.00000000000
St. Louis MO 38.62727330000 -90.19788890000 319294.00000000000
Wichita KS 37.69223610000 -97.33754480000 382368.00000000000
New Orleans LA 29.95464820000 -90.07507200000 343829.00000000000
Newark NJ 40.73565700000 -74.17236670000 277140.00000000000
Milwaukee WI 43.03890250000 -87.90647360000 594833.00000000000
Columbus OH 39.96117550000 -82.99879420000 787033.00000000000
Cincinnati OH 39.11466309570 -84.52968641490 296943.00000000000
Virginia Beach VA 36.85292630000 -75.97798500000 437994.00000000000
Detroit MI 42.33142700000 -83.04575380000 713777.00000000000
Washington DC 38.89511180000 -77.03636580000 601723.00000000000
Minneapolis MN 44.97996540000 -93.26383610000 382578.00000000000
Fort Wayne IN 41.13060410000 -85.12885970000 253691.00000000000
New York NY 40.76149275830 -73.98143111790 8175133.00000000000
Kansas City MO 39.09972660000 -94.57856670000 459787.00000000000
St. Paul MN 44.94441010000 -93.09327420000 285068.00000000000
Omaha NE 41.25860960000 -95.93779200000 408958.00000000000
Lincoln NE 40.80000110000 -96.66695990000 258379.00000000000
Greensboro NC 36.07263550000 -79.79197540000 269666.00000000000
Atlanta GA 33.74899540000 -84.38798240000 420003.00000000000
Raleigh NC 35.77209600000 -78.63861450000 403892.00000000000
Charlotte NC 35.22708690000 -80.84312680000 731424.00000000000
Jacksonville FL 30.33218380000 -81.65565100000 821784.00000000000
Miami FL 25.77426580000 -80.19365890000 399457.00000000000
Lexington KY 38.04979960000 -84.45854850000 295803.00000000000
Memphis TN 35.14953430000 -90.04898010000 646889.00000000000
Tampa FL 27.94752160000 -82.45842790000 335709.00000000000
Denver CO 39.73915360000 -104.98470340000 600158.00000000000
Aurora CO 39.72943190000 -104.83191950000 325078.00000000000
Colorado Springs CO 38.83388160000 -104.82136340000 416427.00000000000
Bakersfield CA 35.37329210000 -119.01871250000 347483.00000000000
Albuquerque NM 35.08449090000 -106.65113670000 545852.00000000000
Anaheim CA 33.83529320000 -117.91450360000 336265.00000000000
Arlington TX 32.73568700000 -97.10806560000 365438.00000000000
Dallas TX 32.78305560000 -96.80666670000 1197816.00000000000
Austin TX 30.26715300000 -97.74306080000 790390.00000000000
Corpus Christi TX 27.80058280000 -97.39638100000 305215.00000000000
Portland OR 45.52345150000 -122.67620710000 583776.00000000000
Oakland CA 37.80437220000 -122.27080260000 390724.00000000000
Fresno CA 36.74772720000 -119.77236610000 494665.00000000000
Henderson NV 36.03969880000 -114.98193680000 257729.00000000000
Las Vegas NV 36.17497050000 -115.13722300000 583756.00000000000
Oklahoma City OK 35.47150760060 -97.54021068850 579999.00000000000
Long Beach CA 33.76696230000 -118.18923480000 462257.00000000000
Los Angeles CA 34.05223420000 -118.24368490000 3792621.00000000000
Riverside CA 33.95334870000 -117.39615640000 303871.00000000000
Mesa AZ 33.42226850000 -111.82264020000 439041.00000000000
Phoenix AZ 33.44837710000 -112.07403730000 1445632.00000000000
Plano TX 33.01984310000 -96.69888560000 259841.00000000000
Fort Worth TX 32.72540900000 -97.32084960000 741206.00000000000
El Paso TX 31.75871980000 -106.48693140000 649121.00000000000
Houston TX 29.76328360000 -95.36327150000 2099451.00000000000
Seattle WA 47.60620950000 -122.33207080000 608660.00000000000
Sacramento CA 38.58157190000 -121.49439960000 466488.00000000000
Stockton CA 37.95770160000 -121.29077960000 291707.00000000000
San Francisco CA 37.77492950000 -122.41941550000 805235.00000000000
San Jose CA 37.33938570000 -121.89495550000 945942.00000000000
Tulsa OK 36.15792900060 -96.01655808850 391906.00000000000
Santa Ana CA 33.74557310000 -117.86783380000 324528.00000000000
San Diego CA 32.71532920000 -117.15725510000 1307402.00000000000
Tucson AZ 32.22174290000 -110.92647900000 520116.00000000000
San Antonio TX 29.42412190000 -98.49362820000 1327407.00000000000
Anchorage AK 61.21805560000 -149.90027780000 291826.00000000000
San Juan PR 18.46633380000 -66.10572170000 381931.00000000000
Indianapolis IN 39.76837650000 -86.15804230000 820445.00000000000
Louisville KY 38.25423760000 -85.75940700000 597337.00000000000
Nashville TN 36.16588990000 -86.78444320000 601222.00000000000
The Bronx NY 40.84992814000 -73.86617941940 1408473.00000000000
Manhattan NY 40.78346165380 -73.96618566080 1619090.00000000000
Brooklyn NY 40.65009286450 -73.94956366460 2565635.00000000000
Queens NY 40.75016214070 -73.79744534870 2272771.00000000000
Staten Island NY 40.58328072030 -74.14981237850 470728.00000000000
city pop_2015 violent_crime murder murder_rate
Chicago 2728695 24663 478 17.51753127
New York 8550861 50088 352 4.116544521
Baltimore 621252 9542 344 55.37205514
Houston 2275221 21994 303 13.31738763
Detroit 673225 11846 295 43.81893126
Los Angeles 3962726 25156 282 7.116313366
Philadelphia 1567810 16132 280 17.85930693
St. Louis 317095 5762 188 59.28822593
New Orleans 393447 3736 164 41.68286961
Washington 672228 8084 162 24.09896642
Indianapolis 863675 11124 148 17.13607549
Milwaukee 600400 9583 145 24.15056629
Dallas 1301977 9038 136 10.44565303
Memphis 657936 11449 135 20.51871307
Las Vegas 1562134 14383 127 8.129904349
Phoenix 1559744 9261 112 7.180665545
Kansas City 473373 6709 109 23.02623935
Jacksonville 867258 5622 97 11.1846763
Atlanta 464710 5203 94 20.22766887
San Antonio 1463586 8594 94 6.422581249
Oakland 419481 6051 85 20.26313468
Louisville 680550 4300 81 11.90213798
Columbus 847745 4631 77 9.082919982
Miami 437969 4473 75 17.12449968
Oklahoma City 630621 4828 73 11.57589107
Nashville 658029 7245 72 10.941767
Cincinnati 298478 2761 66 22.11218247
Charlotte 877817 5948 61 6.94905658
Pittsburgh 306870 2167 57 18.57464073
Fort Worth 829731 4359 56 6.749175335
Tulsa 401520 3628 55 13.6979478
Denver 682418 4599 53 7.766500884
San Francisco 863782 6710 53 6.135807414
Stockton 304890 4122 49 16.07137
Omaha 452252 2329 48 10.61355174
Minneapolis 413479 4395 47 11.3669618
Albuquerque 559721 5406 43 7.682398909
Sacramento 489717 3611 43 8.780581438
Buffalo 258096 2887 41 15.88556196
Fresno 520837 2871 39 7.487947285
Boston 665258 4702 38 5.712069603
San Diego 1400467 5582 37 2.641975855
Long Beach 476318 2766 36 7.557975974
Durham 257911 2185 34 13.18284214
Tampa 364383 2298 34 9.33084145
Orlando 268438 2525 32 11.92081598
Tucson 529675 3472 31 5.85264549
San Jose 1031458 3400 30 2.908504273
Jersey City 265159 1383 27 10.1825697
Wichita 389824 3839 27 6.926202594
Anchorage 301239 3226 26 8.631020552
Greensboro 285950 1707 26 9.092498689
Colorado Springs 452410 1983 25 5.525960965
Fort Wayne 259712 984 25 9.626047314
Aurora 360237 1660 24 6.662280665
Mobile 250346 1529 24 9.586731963
Toledo 279552 3156 24 8.585164835
Austin 938728 3497 23 2.450123998
Seattle 683700 4093 23 3.364048559
Bakersfield 373887 1810 22 5.884130767
Virginia Beach 452797 626 19 4.196140875
Anaheim 349471 1271 18 5.15064197
Corpus Christi 324326 2092 17 5.241639585
El Paso 686077 2515 17 2.477855984
Mesa 471034 1972 16 3.396782398
St. Paul 300721 2115 16 5.320546287
Honolulu 999307 2437 15 1.501040221
Lexington 314077 1044 15 4.775898904
St. Petersburg 255821 1898 14 5.472576528
Santa Ana 337304 1626 12 3.557621611
Riverside 323064 1441 10 3.09536191
Arlington 387565 1946 8 2.064169881
Laredo 256280 972 8 3.121585766
Chula Vista 265215 705 6 2.26231548
Henderson 282554 476 4 1.4156586
Plano 282968 433 4 1.413587402
Irvine 258198 144 2 0.7745993385
Chandler 258875 490 1 0.386286818
Lincoln 276585 1025 1 0.3615525065
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src='//d3js.org/d3.v4.min.js'></script>
<script src='//d3js.org/topojson.v1.min.js'></script>
<script src='https://cdn.rawgit.com/Caged/d3-tip/v0.8.0-alpha.1/index.js'></script>
<style>
.highlight {
fill:none;
stroke-width: 3px;
stroke-opacity: .5;
}
.city-label {
font: 10px sans-serif;
}
.states {
pointer-events: none;
fill: #ccc;
stroke: #fff;
stroke-width: 1px;
stroke-linejoin: round;
}
#panel {
font: 14px sans-serif;
height: 20px;
}
.text {
font-size: 12px;
color: #eee;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
</head>
<body>
<div class='svg-container'></div>
<div id='panel'></div>
<script src='map.js'></script>
</body>
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return d.NAME+ ': '+
d.murder +' ('+ Math.round(d.murder_rate)+' per 100k)'+
'<div>2015 Population: '+ d.pop_2015+ '</div>';
});
var svg = d3.select('.svg-container').append('svg')
.attr('xmlns', 'http://www.w3.org/2000/svg')
.attr('version', '1.1')
.attr('viewBox', '0 0 900 500')
.attr('preserveAspectRatio', 'xMidYMid meet');
svg.call(tip);
var projection = d3.geoAlbersUsa();
var path = d3.geoPath().projection(projection);
d3.queue()
.defer(d3.csv, 'cities-over-250k.csv')
.defer(d3.json, 'us-states-simplified.json')
.defer(d3.csv, 'crime-in-us-2015.csv')
.await(dataReady);
var _cities = {}, _states = {}, _crime = {};
const color = d3.scaleThreshold()
.domain([1,4,8,15,20,30,40,50,60])
.range([
'rgb(255,245,240)',
'rgb(254,224,210)',
'rgb(252,187,161)',
'rgb(252,146,114)',
'rgb(251,106,74)',
'rgb(239,59,44)',
'rgb(203,24,29)',
'rgb(165,15,21)',
'rgb(103,0,13)',
]);
function dataReady(error, cities, states, crime) {
if (error) throw error;
_crime = crime;
_cities = cities;
_states = states;
cities.map(function (row) {
var crimeCity = crime.find(function (item) {
return row.NAME === item.city;
});
if (crimeCity) {
row.pop_2015 = crimeCity.pop_2015;
row.murder = crimeCity.murder;
row.murder_rate = crimeCity.murder_rate;
}
return row;
});
renderMap(_cities, _states, _crime);
}
function drawMapCircles(cities) {
var circleGroup = svg.selectAll('circle')
.data(cities)
.enter()
.append('g')
.attr('transform', function(d) {
return 'translate('+ projection([d.longitude, d.latitude])+')';
})
.attr('class', 'city');
circleGroup
.append('circle')
.style('fill', d => color(d.murder_rate))
.style("stroke", "grey")
.style('fill-opacity', .8)
.attr('r', function (d) {
return d.murder_rate ? d.murder_rate * 0.5 : 0;
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.append('title')
.text(function(d) {
return d.NAME+ ' -'+
'\nMurders: '+d.murder+' ('+Math.round(d.murder_rate)+' for every 100k),'+
'\n2015 Population: '+ d.pop_2015;
});
}
function renderMap(cities, states, crime) {
svg.selectAll('*').remove(); // clear for re-render
cities = cities.filter(function(d) {
return projection([d.longitude, d.latitude]);
});
svg.selectAll('.states')
.data(topojson.feature(states, states.objects.states).features)
.enter()
.append('path')
.attr('class', 'states')
.attr('d', path)
drawMapCircles(cities);
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.