Skip to content

Instantly share code, notes, and snippets.

@josecarlosgonz
Last active August 29, 2015 14:01
Show Gist options
  • Save josecarlosgonz/695be393d4b3627b7f50 to your computer and use it in GitHub Desktop.
Save josecarlosgonz/695be393d4b3627b7f50 to your computer and use it in GitHub Desktop.
Interactive map of ENVIPE 2010, 2011 and 2012
codigo abreviatura estado pre2012 inc2012 ins2013 pre2011 inc2011 ins2012 pre2010 inc2010 ins2011
0 NAC Estados Unidos Mexicanos 27337.4485 35139.1491 72.3225 24498.7900 29371.6382 66.5960 23956.3198 30489.7318 69.5278
1 AGU Aguascalientes 27224.9272 32367.8941 51.8074 25664.5485 25063.6886 46.4720 36387.0272 56496.1919 60.8487
2 BCN Baja California 36579.4858 39296.7319 51.4879 30587.5627 29450.6796 51.2355 30753.1578 31681.8288 58.2504
3 BCS Baja California Sur 27043.3987 31048.8702 30.1101 27019.4837 29046.8332 24.4692 23327.5309 25794.1442 37.0541
4 CAM Campeche 21990.9153 29097.3200 56.7092 17280.1448 21911.4052 44.3895 17321.3007 20972.7013 51.1301
5 COA Coahuila de Zaragoza 19540.2952 17870.3716 81.9856 22944.6316 26555.2597 74.6358 25249.8623 29253.2642 64.5432
6 COL Colima 24907.6595 25168.6396 71.1305 22193.0164 23890.1270 70.3144 17137.9042 17413.4404 65.9663
7 CHP Chiapas 12620.1394 12826.9272 50.9569 12620.9902 13830.6775 37.9796 12855.7293 15089.8930 38.3134
8 CHH Chihuahua 32567.2758 35951.5759 78.0482 30454.8601 30681.1505 82.7319 34826.0201 40416.6489 89.3864
9 DIF Distrito Federal 31675.2513 49198.2163 73.0066 33256.1026 40632.7352 70.7934 32495.3773 44443.4349 75.4563
10 DUR Durango 24231.9322 27630.8647 77.7808 20110.7648 21803.3012 81.8797 20598.2986 23791.7576 88.0964
11 GUA Guanajuato 28860.7590 34391.4496 58.8301 24752.6364 27116.1618 54.6635 20889.7257 23103.6300 54.3436
12 GRO Guerrero 26789.3681 33761.7180 86.9146 20496.9853 27326.2968 74.7178 20172.3074 33500.2647 72.7057
13 HID Hidalgo 19451.3613 21873.6052 56.3478 21821.1301 25851.7338 48.6052 20699.1963 23254.7379 54.7825
14 JAL Jalisco 31861.4402 49083.0901 75.8582 26180.8412 30087.6090 61.4983 24452.2640 32846.7507 71.1487
15 MEX Estado de México 41048.0570 56751.9880 90.6968 31018.1470 39904.9835 84.5817 27907.7742 32886.1943 83.9186
16 MIC Michoacán de Ocampo 20354.4986 24362.4509 80.2772 21039.6587 25052.4214 76.0145 14930.7038 15215.2136 76.0166
17 MOR Morelos 29310.3821 35750.0498 86.4684 22982.3598 25886.5127 81.1938 21925.4243 29026.8660 80.7972
18 NAY Nayarit 19168.6575 26005.8939 56.6158 20876.7610 28952.1984 72.8553 23223.6623 31932.7778 79.1571
19 NLE Nuevo León 31315.5870 37076.3756 80.1563 24990.7595 28508.1424 86.6804 27310.1122 36825.3165 84.4660
20 OAX Oaxaca 14335.2264 18008.5742 66.8658 18129.4908 22209.7040 60.5131 16262.3734 25202.0833 73.2398
21 PUE Puebla 22732.7957 27317.7753 63.3206 23052.5954 30061.5804 57.7643 21706.9968 24116.0585 64.4638
22 QUE Querétaro 24001.7500 27197.2871 41.1049 21575.8593 23527.1867 28.8391 19675.4690 19694.4345 29.6547
23 QROO Quintana Roo 29702.8369 40279.3858 69.9520 26829.2959 38105.5541 59.9136 28740.1014 41461.3688 63.8804
24 SLP San Luis Potosí 26809.5931 35123.6939 77.8433 24847.1931 34065.5387 59.9901 22636.3059 31082.2189 76.5300
25 SIN Sinaloa 26140.9365 33231.0264 77.6846 23947.8145 30030.9280 77.5021 26373.4332 34547.5600 80.8666
26 SON Sonora 29130.7582 34126.1071 52.4615 29973.7405 38760.9287 42.1321 29285.2089 47909.2987 46.1239
27 TAB Tabasco 20836.8469 24368.2869 83.4370 19612.0855 21860.6838 71.3813 25073.9092 32830.8256 77.8167
28 TAM Tamaulipas 21406.7371 25254.5817 84.0643 18125.7921 20543.6026 83.2869 22647.3174 27155.5029 83.4155
29 TLA Tlaxcala 18150.2041 18529.8011 52.0783 18362.2387 22675.0312 40.0717 20720.8407 26337.2310 41.4889
30 VER Veracruz de Ignacio de la Llave 18732.6329 23411.4529 75.3749 17820.3334 22829.4465 70.0783 16436.9540 19165.3686 65.1335
31 YUC Yucatán 19547.9813 22945.0175 32.2172 14831.6556 16620.4984 19.1970 17825.9397 38634.7035 26.3991
32 ZAC Zacatecas 20336.8394 20505.9804 84.6907 21146.2913 19790.1586 80.1473 22747.0273 30087.0360 83.0420
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='http://d3js.org/topojson.v1.min.js' type='text/javascript'></script>
<script src='http://datamaps.github.io/scripts/datamaps.all.min.js' type='text/javascript'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.min.js' type='text/javascript'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
margin-left: auto;
margin-right: auto;
width: 950px;
height: 400px;
}
.container {
margin-top: 20px;
}
</style>
</head>
<body ng-app ng-controller='rChartsCtrl'>
<div class='container'>
<div class="btn-group" >
<button ng-repeat="value in years" ng-click='updateYear(value)'
class="btn btn-default" type="button"
ng-model="value" btn-radio="value">
{{ value }}
</button>
</div>
<div id='chart_1' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope){
$scope.years = [2010, 2011, 2012]
$scope.year = $scope.years[0]
$scope.updateYear = function(x){
$scope.year = x
}
$scope.$watch('year', function(newYear){
mapchart_1.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>
<script id='popup-template' type='text/x-handlebars-template'>
</script>
<script>
var chartParams = {
"dom": "chart_1",
"width": 800,
"height": 400,
"geographyConfig": {
"dataUrl": "mx_states.json",
"popupTemplate": function(geography, data) { //this function should just return a string
return '<div class=hoverinfo><strong>' + geography.properties.name + '</strong></div>';
}
},
"scope": "states",
"labels": true,
"bodyattrs": "ng-app ng-controller='rChartsCtrl'",
"setProjection": function( element, options ) {
var projection, path;
projection = d3.geo.mercator()
.center([-90, 24])
.scale(element.offsetWidth)
.translate([element.offsetWidth / 2, element.offsetHeight / 2]);
path = d3.geo.path()
.projection( projection );
return {path: path, projection: projection};
} ,
"fills": {
"[12592,18289]": "#FFFFB2",
"(18289,23986]": "#FECC5C",
"(23986,29683]": "#FD8D3C",
"(29683,35380]": "#F03B20",
"(35380,41076]": "#BD0026",
"defaultFill": "#BD0026"
},
"data": {
"Aguascalientes": {
"state_code": 1,
"short_name": "AGU",
"name": "Aguascalientes",
"year": 2010,
"prevalencia": 36387,
"fillKey": "(35380,41076]"
},
"Baja California": {
"state_code": 2,
"short_name": "BCN",
"name": "Baja California",
"year": 2010,
"prevalencia": 30753,
"fillKey": "(29683,35380]"
},
"Baja California Sur": {
"state_code": 3,
"short_name": "BCS",
"name": "Baja California Sur",
"year": 2010,
"prevalencia": 23328,
"fillKey": "(18289,23986]"
},
"Campeche": {
"state_code": 4,
"short_name": "CAM",
"name": "Campeche",
"year": 2010,
"prevalencia": 17321,
"fillKey": "[12592,18289]"
},
"Coahuila de Zaragoza": {
"state_code": 5,
"short_name": "COA",
"name": "Coahuila de Zaragoza",
"year": 2010,
"prevalencia": 25250,
"fillKey": "(23986,29683]"
},
"Colima": {
"state_code": 6,
"short_name": "COL",
"name": "Colima",
"year": 2010,
"prevalencia": 17138,
"fillKey": "[12592,18289]"
},
"Chiapas": {
"state_code": 7,
"short_name": "CHP",
"name": "Chiapas",
"year": 2010,
"prevalencia": 12856,
"fillKey": "[12592,18289]"
},
"Chihuahua": {
"state_code": 8,
"short_name": "CHH",
"name": "Chihuahua",
"year": 2010,
"prevalencia": 34826,
"fillKey": "(29683,35380]"
},
"Distrito Federal": {
"state_code": 9,
"short_name": "DIF",
"name": "Distrito Federal",
"year": 2010,
"prevalencia": 32495,
"fillKey": "(29683,35380]"
},
"Durango": {
"state_code": 10,
"short_name": "DUR",
"name": "Durango",
"year": 2010,
"prevalencia": 20598,
"fillKey": "(18289,23986]"
},
"Guanajuato": {
"state_code": 11,
"short_name": "GUA",
"name": "Guanajuato",
"year": 2010,
"prevalencia": 20890,
"fillKey": "(18289,23986]"
},
"Guerrero": {
"state_code": 12,
"short_name": "GRO",
"name": "Guerrero",
"year": 2010,
"prevalencia": 20172,
"fillKey": "(18289,23986]"
},
"Hidalgo": {
"state_code": 13,
"short_name": "HID",
"name": "Hidalgo",
"year": 2010,
"prevalencia": 20699,
"fillKey": "(18289,23986]"
},
"Jalisco": {
"state_code": 14,
"short_name": "JAL",
"name": "Jalisco",
"year": 2010,
"prevalencia": 24452,
"fillKey": "(23986,29683]"
},
"Estado de México": {
"state_code": 15,
"short_name": "MEX",
"name": "Estado de México",
"year": 2010,
"prevalencia": 27908,
"fillKey": "(23986,29683]"
},
"Michoacán de Ocampo": {
"state_code": 16,
"short_name": "MIC",
"name": "Michoacán de Ocampo",
"year": 2010,
"prevalencia": 14931,
"fillKey": "[12592,18289]"
},
"Morelos": {
"state_code": 17,
"short_name": "MOR",
"name": "Morelos",
"year": 2010,
"prevalencia": 21925,
"fillKey": "(18289,23986]"
},
"Nayarit": {
"state_code": 18,
"short_name": "NAY",
"name": "Nayarit",
"year": 2010,
"prevalencia": 23224,
"fillKey": "(18289,23986]"
},
"Nuevo León": {
"state_code": 19,
"short_name": "NLE",
"name": "Nuevo León",
"year": 2010,
"prevalencia": 27310,
"fillKey": "(23986,29683]"
},
"Oaxaca": {
"state_code": 20,
"short_name": "OAX",
"name": "Oaxaca",
"year": 2010,
"prevalencia": 16262,
"fillKey": "[12592,18289]"
},
"Puebla": {
"state_code": 21,
"short_name": "PUE",
"name": "Puebla",
"year": 2010,
"prevalencia": 21707,
"fillKey": "(18289,23986]"
},
"Querétaro": {
"state_code": 22,
"short_name": "QUE",
"name": "Querétaro",
"year": 2010,
"prevalencia": 19675,
"fillKey": "(18289,23986]"
},
"Quintana Roo": {
"state_code": 23,
"short_name": "QROO",
"name": "Quintana Roo",
"year": 2010,
"prevalencia": 28740,
"fillKey": "(23986,29683]"
},
"San Luis Potosí": {
"state_code": 24,
"short_name": "SLP",
"name": "San Luis Potosí",
"year": 2010,
"prevalencia": 22636,
"fillKey": "(18289,23986]"
},
"Sinaloa": {
"state_code": 25,
"short_name": "SIN",
"name": "Sinaloa",
"year": 2010,
"prevalencia": 26373,
"fillKey": "(23986,29683]"
},
"Sonora": {
"state_code": 26,
"short_name": "SON",
"name": "Sonora",
"year": 2010,
"prevalencia": 29285,
"fillKey": "(23986,29683]"
},
"Tabasco": {
"state_code": 27,
"short_name": "TAB",
"name": "Tabasco",
"year": 2010,
"prevalencia": 25074,
"fillKey": "(23986,29683]"
},
"Tamaulipas": {
"state_code": 28,
"short_name": "TAM",
"name": "Tamaulipas",
"year": 2010,
"prevalencia": 22647,
"fillKey": "(18289,23986]"
},
"Tlaxcala": {
"state_code": 29,
"short_name": "TLA",
"name": "Tlaxcala",
"year": 2010,
"prevalencia": 20721,
"fillKey": "(18289,23986]"
},
"Veracruz de Ignacio de la Llave": {
"state_code": 30,
"short_name": "VER",
"name": "Veracruz de Ignacio de la Llave",
"year": 2010,
"prevalencia": 16437,
"fillKey": "[12592,18289]"
},
"Yucatán": {
"state_code": 31,
"short_name": "YUC",
"name": "Yucatán",
"year": 2010,
"prevalencia": 17826,
"fillKey": "[12592,18289]"
},
"Zacatecas": {
"state_code": 32,
"short_name": "ZAC",
"name": "Zacatecas",
"year": 2010,
"prevalencia": 22747,
"fillKey": "(18289,23986]"
}
},
"legend": true,
"id": "chart_1",
"newData": {
"2010": {
"Aguascalientes": {
"state_code": 1,
"short_name": "AGU",
"name": "Aguascalientes",
"year": 2010,
"prevalencia": 36387,
"fillKey": "(35380,41076]"
},
"Baja California": {
"state_code": 2,
"short_name": "BCN",
"name": "Baja California",
"year": 2010,
"prevalencia": 30753,
"fillKey": "(29683,35380]"
},
"Baja California Sur": {
"state_code": 3,
"short_name": "BCS",
"name": "Baja California Sur",
"year": 2010,
"prevalencia": 23328,
"fillKey": "(18289,23986]"
},
"Campeche": {
"state_code": 4,
"short_name": "CAM",
"name": "Campeche",
"year": 2010,
"prevalencia": 17321,
"fillKey": "[12592,18289]"
},
"Coahuila de Zaragoza": {
"state_code": 5,
"short_name": "COA",
"name": "Coahuila de Zaragoza",
"year": 2010,
"prevalencia": 25250,
"fillKey": "(23986,29683]"
},
"Colima": {
"state_code": 6,
"short_name": "COL",
"name": "Colima",
"year": 2010,
"prevalencia": 17138,
"fillKey": "[12592,18289]"
},
"Chiapas": {
"state_code": 7,
"short_name": "CHP",
"name": "Chiapas",
"year": 2010,
"prevalencia": 12856,
"fillKey": "[12592,18289]"
},
"Chihuahua": {
"state_code": 8,
"short_name": "CHH",
"name": "Chihuahua",
"year": 2010,
"prevalencia": 34826,
"fillKey": "(29683,35380]"
},
"Distrito Federal": {
"state_code": 9,
"short_name": "DIF",
"name": "Distrito Federal",
"year": 2010,
"prevalencia": 32495,
"fillKey": "(29683,35380]"
},
"Durango": {
"state_code": 10,
"short_name": "DUR",
"name": "Durango",
"year": 2010,
"prevalencia": 20598,
"fillKey": "(18289,23986]"
},
"Guanajuato": {
"state_code": 11,
"short_name": "GUA",
"name": "Guanajuato",
"year": 2010,
"prevalencia": 20890,
"fillKey": "(18289,23986]"
},
"Guerrero": {
"state_code": 12,
"short_name": "GRO",
"name": "Guerrero",
"year": 2010,
"prevalencia": 20172,
"fillKey": "(18289,23986]"
},
"Hidalgo": {
"state_code": 13,
"short_name": "HID",
"name": "Hidalgo",
"year": 2010,
"prevalencia": 20699,
"fillKey": "(18289,23986]"
},
"Jalisco": {
"state_code": 14,
"short_name": "JAL",
"name": "Jalisco",
"year": 2010,
"prevalencia": 24452,
"fillKey": "(23986,29683]"
},
"Estado de México": {
"state_code": 15,
"short_name": "MEX",
"name": "Estado de México",
"year": 2010,
"prevalencia": 27908,
"fillKey": "(23986,29683]"
},
"Michoacán de Ocampo": {
"state_code": 16,
"short_name": "MIC",
"name": "Michoacán de Ocampo",
"year": 2010,
"prevalencia": 14931,
"fillKey": "[12592,18289]"
},
"Morelos": {
"state_code": 17,
"short_name": "MOR",
"name": "Morelos",
"year": 2010,
"prevalencia": 21925,
"fillKey": "(18289,23986]"
},
"Nayarit": {
"state_code": 18,
"short_name": "NAY",
"name": "Nayarit",
"year": 2010,
"prevalencia": 23224,
"fillKey": "(18289,23986]"
},
"Nuevo León": {
"state_code": 19,
"short_name": "NLE",
"name": "Nuevo León",
"year": 2010,
"prevalencia": 27310,
"fillKey": "(23986,29683]"
},
"Oaxaca": {
"state_code": 20,
"short_name": "OAX",
"name": "Oaxaca",
"year": 2010,
"prevalencia": 16262,
"fillKey": "[12592,18289]"
},
"Puebla": {
"state_code": 21,
"short_name": "PUE",
"name": "Puebla",
"year": 2010,
"prevalencia": 21707,
"fillKey": "(18289,23986]"
},
"Querétaro": {
"state_code": 22,
"short_name": "QUE",
"name": "Querétaro",
"year": 2010,
"prevalencia": 19675,
"fillKey": "(18289,23986]"
},
"Quintana Roo": {
"state_code": 23,
"short_name": "QROO",
"name": "Quintana Roo",
"year": 2010,
"prevalencia": 28740,
"fillKey": "(23986,29683]"
},
"San Luis Potosí": {
"state_code": 24,
"short_name": "SLP",
"name": "San Luis Potosí",
"year": 2010,
"prevalencia": 22636,
"fillKey": "(18289,23986]"
},
"Sinaloa": {
"state_code": 25,
"short_name": "SIN",
"name": "Sinaloa",
"year": 2010,
"prevalencia": 26373,
"fillKey": "(23986,29683]"
},
"Sonora": {
"state_code": 26,
"short_name": "SON",
"name": "Sonora",
"year": 2010,
"prevalencia": 29285,
"fillKey": "(23986,29683]"
},
"Tabasco": {
"state_code": 27,
"short_name": "TAB",
"name": "Tabasco",
"year": 2010,
"prevalencia": 25074,
"fillKey": "(23986,29683]"
},
"Tamaulipas": {
"state_code": 28,
"short_name": "TAM",
"name": "Tamaulipas",
"year": 2010,
"prevalencia": 22647,
"fillKey": "(18289,23986]"
},
"Tlaxcala": {
"state_code": 29,
"short_name": "TLA",
"name": "Tlaxcala",
"year": 2010,
"prevalencia": 20721,
"fillKey": "(18289,23986]"
},
"Veracruz de Ignacio de la Llave": {
"state_code": 30,
"short_name": "VER",
"name": "Veracruz de Ignacio de la Llave",
"year": 2010,
"prevalencia": 16437,
"fillKey": "[12592,18289]"
},
"Yucatán": {
"state_code": 31,
"short_name": "YUC",
"name": "Yucatán",
"year": 2010,
"prevalencia": 17826,
"fillKey": "[12592,18289]"
},
"Zacatecas": {
"state_code": 32,
"short_name": "ZAC",
"name": "Zacatecas",
"year": 2010,
"prevalencia": 22747,
"fillKey": "(18289,23986]"
}
},
"2011": {
"Aguascalientes": {
"state_code": 1,
"short_name": "AGU",
"name": "Aguascalientes",
"year": 2011,
"prevalencia": 25665,
"fillKey": "(23986,29683]"
},
"Baja California": {
"state_code": 2,
"short_name": "BCN",
"name": "Baja California",
"year": 2011,
"prevalencia": 30588,
"fillKey": "(29683,35380]"
},
"Baja California Sur": {
"state_code": 3,
"short_name": "BCS",
"name": "Baja California Sur",
"year": 2011,
"prevalencia": 27019,
"fillKey": "(23986,29683]"
},
"Campeche": {
"state_code": 4,
"short_name": "CAM",
"name": "Campeche",
"year": 2011,
"prevalencia": 17280,
"fillKey": "[12592,18289]"
},
"Coahuila de Zaragoza": {
"state_code": 5,
"short_name": "COA",
"name": "Coahuila de Zaragoza",
"year": 2011,
"prevalencia": 22945,
"fillKey": "(18289,23986]"
},
"Colima": {
"state_code": 6,
"short_name": "COL",
"name": "Colima",
"year": 2011,
"prevalencia": 22193,
"fillKey": "(18289,23986]"
},
"Chiapas": {
"state_code": 7,
"short_name": "CHP",
"name": "Chiapas",
"year": 2011,
"prevalencia": 12621,
"fillKey": "[12592,18289]"
},
"Chihuahua": {
"state_code": 8,
"short_name": "CHH",
"name": "Chihuahua",
"year": 2011,
"prevalencia": 30455,
"fillKey": "(29683,35380]"
},
"Distrito Federal": {
"state_code": 9,
"short_name": "DIF",
"name": "Distrito Federal",
"year": 2011,
"prevalencia": 33256,
"fillKey": "(29683,35380]"
},
"Durango": {
"state_code": 10,
"short_name": "DUR",
"name": "Durango",
"year": 2011,
"prevalencia": 20111,
"fillKey": "(18289,23986]"
},
"Guanajuato": {
"state_code": 11,
"short_name": "GUA",
"name": "Guanajuato",
"year": 2011,
"prevalencia": 24753,
"fillKey": "(23986,29683]"
},
"Guerrero": {
"state_code": 12,
"short_name": "GRO",
"name": "Guerrero",
"year": 2011,
"prevalencia": 20497,
"fillKey": "(18289,23986]"
},
"Hidalgo": {
"state_code": 13,
"short_name": "HID",
"name": "Hidalgo",
"year": 2011,
"prevalencia": 21821,
"fillKey": "(18289,23986]"
},
"Jalisco": {
"state_code": 14,
"short_name": "JAL",
"name": "Jalisco",
"year": 2011,
"prevalencia": 26181,
"fillKey": "(23986,29683]"
},
"Estado de México": {
"state_code": 15,
"short_name": "MEX",
"name": "Estado de México",
"year": 2011,
"prevalencia": 31018,
"fillKey": "(29683,35380]"
},
"Michoacán de Ocampo": {
"state_code": 16,
"short_name": "MIC",
"name": "Michoacán de Ocampo",
"year": 2011,
"prevalencia": 21040,
"fillKey": "(18289,23986]"
},
"Morelos": {
"state_code": 17,
"short_name": "MOR",
"name": "Morelos",
"year": 2011,
"prevalencia": 22982,
"fillKey": "(18289,23986]"
},
"Nayarit": {
"state_code": 18,
"short_name": "NAY",
"name": "Nayarit",
"year": 2011,
"prevalencia": 20877,
"fillKey": "(18289,23986]"
},
"Nuevo León": {
"state_code": 19,
"short_name": "NLE",
"name": "Nuevo León",
"year": 2011,
"prevalencia": 24991,
"fillKey": "(23986,29683]"
},
"Oaxaca": {
"state_code": 20,
"short_name": "OAX",
"name": "Oaxaca",
"year": 2011,
"prevalencia": 18129,
"fillKey": "[12592,18289]"
},
"Puebla": {
"state_code": 21,
"short_name": "PUE",
"name": "Puebla",
"year": 2011,
"prevalencia": 23053,
"fillKey": "(18289,23986]"
},
"Querétaro": {
"state_code": 22,
"short_name": "QUE",
"name": "Querétaro",
"year": 2011,
"prevalencia": 21576,
"fillKey": "(18289,23986]"
},
"Quintana Roo": {
"state_code": 23,
"short_name": "QROO",
"name": "Quintana Roo",
"year": 2011,
"prevalencia": 26829,
"fillKey": "(23986,29683]"
},
"San Luis Potosí": {
"state_code": 24,
"short_name": "SLP",
"name": "San Luis Potosí",
"year": 2011,
"prevalencia": 24847,
"fillKey": "(23986,29683]"
},
"Sinaloa": {
"state_code": 25,
"short_name": "SIN",
"name": "Sinaloa",
"year": 2011,
"prevalencia": 23948,
"fillKey": "(18289,23986]"
},
"Sonora": {
"state_code": 26,
"short_name": "SON",
"name": "Sonora",
"year": 2011,
"prevalencia": 29974,
"fillKey": "(29683,35380]"
},
"Tabasco": {
"state_code": 27,
"short_name": "TAB",
"name": "Tabasco",
"year": 2011,
"prevalencia": 19612,
"fillKey": "(18289,23986]"
},
"Tamaulipas": {
"state_code": 28,
"short_name": "TAM",
"name": "Tamaulipas",
"year": 2011,
"prevalencia": 18126,
"fillKey": "[12592,18289]"
},
"Tlaxcala": {
"state_code": 29,
"short_name": "TLA",
"name": "Tlaxcala",
"year": 2011,
"prevalencia": 18362,
"fillKey": "(18289,23986]"
},
"Veracruz de Ignacio de la Llave": {
"state_code": 30,
"short_name": "VER",
"name": "Veracruz de Ignacio de la Llave",
"year": 2011,
"prevalencia": 17820,
"fillKey": "[12592,18289]"
},
"Yucatán": {
"state_code": 31,
"short_name": "YUC",
"name": "Yucatán",
"year": 2011,
"prevalencia": 14832,
"fillKey": "[12592,18289]"
},
"Zacatecas": {
"state_code": 32,
"short_name": "ZAC",
"name": "Zacatecas",
"year": 2011,
"prevalencia": 21146,
"fillKey": "(18289,23986]"
}
},
"2012": {
"Aguascalientes": {
"state_code": 1,
"short_name": "AGU",
"name": "Aguascalientes",
"year": 2012,
"prevalencia": 27225,
"fillKey": "(23986,29683]"
},
"Baja California": {
"state_code": 2,
"short_name": "BCN",
"name": "Baja California",
"year": 2012,
"prevalencia": 36579,
"fillKey": "(35380,41076]"
},
"Baja California Sur": {
"state_code": 3,
"short_name": "BCS",
"name": "Baja California Sur",
"year": 2012,
"prevalencia": 27043,
"fillKey": "(23986,29683]"
},
"Campeche": {
"state_code": 4,
"short_name": "CAM",
"name": "Campeche",
"year": 2012,
"prevalencia": 21991,
"fillKey": "(18289,23986]"
},
"Coahuila de Zaragoza": {
"state_code": 5,
"short_name": "COA",
"name": "Coahuila de Zaragoza",
"year": 2012,
"prevalencia": 19540,
"fillKey": "(18289,23986]"
},
"Colima": {
"state_code": 6,
"short_name": "COL",
"name": "Colima",
"year": 2012,
"prevalencia": 24908,
"fillKey": "(23986,29683]"
},
"Chiapas": {
"state_code": 7,
"short_name": "CHP",
"name": "Chiapas",
"year": 2012,
"prevalencia": 12620,
"fillKey": "[12592,18289]"
},
"Chihuahua": {
"state_code": 8,
"short_name": "CHH",
"name": "Chihuahua",
"year": 2012,
"prevalencia": 32567,
"fillKey": "(29683,35380]"
},
"Distrito Federal": {
"state_code": 9,
"short_name": "DIF",
"name": "Distrito Federal",
"year": 2012,
"prevalencia": 31675,
"fillKey": "(29683,35380]"
},
"Durango": {
"state_code": 10,
"short_name": "DUR",
"name": "Durango",
"year": 2012,
"prevalencia": 24232,
"fillKey": "(23986,29683]"
},
"Guanajuato": {
"state_code": 11,
"short_name": "GUA",
"name": "Guanajuato",
"year": 2012,
"prevalencia": 28861,
"fillKey": "(23986,29683]"
},
"Guerrero": {
"state_code": 12,
"short_name": "GRO",
"name": "Guerrero",
"year": 2012,
"prevalencia": 26789,
"fillKey": "(23986,29683]"
},
"Hidalgo": {
"state_code": 13,
"short_name": "HID",
"name": "Hidalgo",
"year": 2012,
"prevalencia": 19451,
"fillKey": "(18289,23986]"
},
"Jalisco": {
"state_code": 14,
"short_name": "JAL",
"name": "Jalisco",
"year": 2012,
"prevalencia": 31861,
"fillKey": "(29683,35380]"
},
"Estado de México": {
"state_code": 15,
"short_name": "MEX",
"name": "Estado de México",
"year": 2012,
"prevalencia": 41048,
"fillKey": "(35380,41076]"
},
"Michoacán de Ocampo": {
"state_code": 16,
"short_name": "MIC",
"name": "Michoacán de Ocampo",
"year": 2012,
"prevalencia": 20354,
"fillKey": "(18289,23986]"
},
"Morelos": {
"state_code": 17,
"short_name": "MOR",
"name": "Morelos",
"year": 2012,
"prevalencia": 29310,
"fillKey": "(23986,29683]"
},
"Nayarit": {
"state_code": 18,
"short_name": "NAY",
"name": "Nayarit",
"year": 2012,
"prevalencia": 19169,
"fillKey": "(18289,23986]"
},
"Nuevo León": {
"state_code": 19,
"short_name": "NLE",
"name": "Nuevo León",
"year": 2012,
"prevalencia": 31316,
"fillKey": "(29683,35380]"
},
"Oaxaca": {
"state_code": 20,
"short_name": "OAX",
"name": "Oaxaca",
"year": 2012,
"prevalencia": 14335,
"fillKey": "[12592,18289]"
},
"Puebla": {
"state_code": 21,
"short_name": "PUE",
"name": "Puebla",
"year": 2012,
"prevalencia": 22733,
"fillKey": "(18289,23986]"
},
"Querétaro": {
"state_code": 22,
"short_name": "QUE",
"name": "Querétaro",
"year": 2012,
"prevalencia": 24002,
"fillKey": "(23986,29683]"
},
"Quintana Roo": {
"state_code": 23,
"short_name": "QROO",
"name": "Quintana Roo",
"year": 2012,
"prevalencia": 29703,
"fillKey": "(29683,35380]"
},
"San Luis Potosí": {
"state_code": 24,
"short_name": "SLP",
"name": "San Luis Potosí",
"year": 2012,
"prevalencia": 26810,
"fillKey": "(23986,29683]"
},
"Sinaloa": {
"state_code": 25,
"short_name": "SIN",
"name": "Sinaloa",
"year": 2012,
"prevalencia": 26141,
"fillKey": "(23986,29683]"
},
"Sonora": {
"state_code": 26,
"short_name": "SON",
"name": "Sonora",
"year": 2012,
"prevalencia": 29131,
"fillKey": "(23986,29683]"
},
"Tabasco": {
"state_code": 27,
"short_name": "TAB",
"name": "Tabasco",
"year": 2012,
"prevalencia": 20837,
"fillKey": "(18289,23986]"
},
"Tamaulipas": {
"state_code": 28,
"short_name": "TAM",
"name": "Tamaulipas",
"year": 2012,
"prevalencia": 21407,
"fillKey": "(18289,23986]"
},
"Tlaxcala": {
"state_code": 29,
"short_name": "TLA",
"name": "Tlaxcala",
"year": 2012,
"prevalencia": 18150,
"fillKey": "[12592,18289]"
},
"Veracruz de Ignacio de la Llave": {
"state_code": 30,
"short_name": "VER",
"name": "Veracruz de Ignacio de la Llave",
"year": 2012,
"prevalencia": 18733,
"fillKey": "(18289,23986]"
},
"Yucatán": {
"state_code": 31,
"short_name": "YUC",
"name": "Yucatán",
"year": 2012,
"prevalencia": 19548,
"fillKey": "(18289,23986]"
},
"Zacatecas": {
"state_code": 32,
"short_name": "ZAC",
"name": "Zacatecas",
"year": 2012,
"prevalencia": 20337,
"fillKey": "(18289,23986]"
}
}
}
}
chartParams.element = document.getElementById('chart_1')
var mapchart_1 = new Datamap(chartParams);
// draw a bubble map if specified
if (chartParams.bubbles) {
var bubbles = chartParams.bubbles
mapchart_1.bubbles(bubbles)
}
if (chartParams.labels){
mapchart_1.labels()
}
if (chartParams.legend){
mapchart_1.legend()
}
setProjection = function( element, options ) {
var projection, path;
projection = d3.geo.albersUsa()
.scale(element.offsetWidth)
.translate([element.offsetWidth / 2, element.offsetHeight / 2]);
path = d3.geo.path()
.projection( projection );
return {path: path, projection: projection};
}
</script>
<style>
.datamaps {
position: relative;
}
.datamaps-legend dt{
font-weight: normal;
}
</style>
<script></script>
</body>
</html>
# Interactive map for ENVIPE 2011, 2012, 2013
# Author: Jose Gonzalez
# Based on: http://rmaps.github.io/blog/posts/animated-choropleths/index.html and
# http://bl.ocks.org/diegovalle/8967565
# Load data
#####
require(reshape2)
require(devtools)
#install_github('ramnathv/rCharts@dev')
install_github(repo='rCharts',username='ramnathv',ref="dev")
install_github(repo='rMaps',username='ramnathv',ref="master")
envipe <- read.csv("envipe2011-2013.csv", encoding= "utf8",stringsAsFactors=F)
# Subset states
envipe <- subset(envipe, envipe$codigo != 0)
# Subset prevalencia delictiva
names(envipe)
prev <- envipe[,c(1:4,7,10)]
names(prev)
# Melt dataframe
mprev <- melt(data=prev,id.vars=c(1:3))
head(mprev); tail(mprev)
# Change names
names(mprev) <- c("state_code","short_name","name","year","prevalencia")
# Delete "pre" from year
mprev$year <- gsub("pre","", mprev$year)
mprev$year <- as.numeric(mprev$year)
table(mprev$year) # 32 obs for each variable
head(mprev); tail(mprev)
# Interactve map: produces an index.html file
# Requries alocal server to be visualized. In Mac use python -m SimpleHTTPServer 8888
#####
# Separate prevalencia in five intervals
dat <- transform(mprev,
fillKey = cut(prevalencia, breaks = 5, dig.lab = 5, include.lowest=T, right=T)
)
dat
keyNames <- levels(dat$fillKey)
# Set fill colors
fills = setNames(
c(RColorBrewer::brewer.pal(5, 'YlOrRd'),'white'),
c(levels(dat$fillKey),'defaultfill')
)
fills
fills = setNames(
c(RColorBrewer::brewer.pal(5, 'YlOrRd'), '#BD0026'),
c(levels(dat$fillKey), 'defaultFill')
)
str(fills)
# Set payload for rMaps
dat2 <- plyr::dlply(na.omit(dat), "year", function(x){
y = rCharts::toJSONArray2(x, json = F)
names(y) = lapply(y, '[[', 'name')
return(y)
})
# Since rMaps ichoropleth funciton has a bug on the interval's maximum values, use
#boiler plate provided by Diego Valle-Jones
d1 <- Datamaps$new()
d1$set(
geographyConfig = list(
dataUrl = "mx_states.json",
popupTemplate = "#! function(geography, data) { //this function should just return a string
return '<div class=hoverinfo><strong>' + geography.properties.name + '</strong></div>';
} !#"
),
dom = 'chart_1',
scope = 'states',
labels = TRUE,
bodyattrs = "ng-app ng-controller='rChartsCtrl'",
setProjection = '#! function( element, options ) {
var projection, path;
projection = d3.geo.mercator()
.center([-90, 24])
.scale(element.offsetWidth)
.translate([element.offsetWidth / 2, element.offsetHeight / 2]);
path = d3.geo.path()
.projection( projection );
return {path: path, projection: projection};
} !#',
fills = fills,
data = dat2[[1]],
legend = TRUE,
labels = TRUE
)
d1$save("index.html", cdn = TRUE)
#####
#### Map with slider with slider
#####
d1$addAssets(
jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
)
d1$setTemplate(chartDiv = "
<div id = 'chart_1' class = 'rChart datamaps'>
<input id='slider' type='range' min=2010 max=2012 ng-model='year' width=200>
<span ng-bind='year'></span>
<script>
function rChartsCtrl($scope){
$scope.year = '2012';
$scope.$watch('year', function(newYear){
mapchart_1.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>
</div> "
)
d1$set(newData = dat2)
d1$save("index.html", cdn = TRUE)
#####
### Map with drop down menu and default year to 2012
#####
d1$addAssets(
jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
)
d1$setTemplate(chartDiv = "
<div class='container'>
<div class='col-md-2'>
<select ng-model='year' class='form-control'
ng-options='year for year in [2010, 2011, 2012]'>
{{ year }}
</select>
</div>
<div id = 'chart_1' class = 'rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope){
$scope.year = 2012;
$scope.$watch('year', function(newYear){
mapchart_1.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>
</div> "
)
d1$set(newData = dat2)
d1$save("index2.html", cdn = TRUE)
#####
### Map with button groups
#####
d1$addAssets(
jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
)
d1$setTemplate(chartDiv = "
<div class='container'>
<div class= 'btn-group' >
<button ng-repeat="value in years" ng-click='updateYear(value)'
class="btn btn-default" type="button"
ng-model="value" btn-radio="value">
{{ value }}
</button>
</div>
<div id='chart_1' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope){
$scope.years = [1960, 1980, 2000]
$scope.year = $scope.years[0]
$scope.updateYear = function(x){
$scope.year = x
}
$scope.$watch('year', function(newYear){
mapchart_1.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>"
)
d1$set(newData = dat2)
d1$save("victimizacion.html", cdn = TRUE)
.PHONY: all clean
all: estados.zip mx_states.json clean
estados.zip:
curl -o estados.zip http://mapserver.inegi.org.mx/MGN/mge2010v5_0.zip
mx_states.json: estados.zip
unzip -o estados.zip
ogr2ogr states.shp Entidades_2010_5.shp -t_srs "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
## id-property needed so that DataMaps knows how to color the map
topojson -o mx_states.json -s 1e-7 -q 1e5 states.shp -p state_code=+CVE_ENT,name=NOM_ENT --id-property NOM_ENT
clean:
-rm -f *.zip *.csv.gz Estados*
Display the source blob
Display the rendered blob
Raw
Loading
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