Last active
August 29, 2015 14:06
-
-
Save AnotherContext/28c7632e2289724e8a39 to your computer and use it in GitHub Desktop.
Domestic abuse: assistance calls by region comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dom_abuse <- read.csv(file = "domestic_abuse.csv") | |
#names(dom_abuse) | |
da1 <- dom_abuse[c("Area.Name", "Region.Name", "Number.of.calls.for.assistance", "Total.crime", "calls.for.assistance.with.DA.marker", "crime.with.a.DA.marker" )] | |
#head(da1) | |
#col names | |
#[1] "Area.Name" | |
#[2] "Region.Name" | |
###~~~~~~~~~~~~~~~~~~~~~~~ | |
### general calls | |
###~~~~~~~~~~~~~~~~~~~~~~~ | |
### 1. general assitance calls by region | |
### 2. general assitance calls by area | |
#[3] "Number.of.calls.for.assistance" | |
#[8] "Total.crime" | |
#[4] "calls.for.assistance.with.DA.marker" | |
#[6] "Calls.for.assistance.from.repeat.victims" :: excluded for now | |
#[9] "crime.with.a.DA.marker" | |
###~~~~~~~~~~~~~~~~~~~~~~~~ | |
### 1. region comparison | |
###~~~~~~~~~~~~~~~~~~~~~~~~ | |
da1_reg_sum <- aggregate(.~ Region.Name, data = da1, FUN= sum ) | |
da1_data <- melt(da1_reg_sum, id= c("Area.Name", "Region.Name")) #variable, value | |
da1_plot <- as.data.frame(da1_data) | |
#x axis - region, option- variable | |
p1.1a <- nPlot(value ~ Region.Name, group = 'variable', | |
data = da1_data, | |
type = 'multiBarChart' | |
) | |
p1.1a$set(dom = 'chart1', width = 1200) | |
p1.1a | |
#p1.1a$publish('Domestic abuse: assistance calls, region comparison', host='gist') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype HTML> | |
<meta charset = 'utf-8'> | |
<html> | |
<head> | |
<link rel='stylesheet' href='http://nvd3.org/assets/css/nv.d3.css'> | |
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'></script> | |
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script> | |
<script src='http://timelyportfolio.github.io/rCharts_nvd3_tests/libraries/widgets/nvd3/js/nv.d3.min-new.js' type='text/javascript'></script> | |
<script src='http://nvd3.org/assets/lib/fisheye.js' type='text/javascript'></script> | |
<style> | |
.rChart { | |
display: block; | |
margin-left: auto; | |
margin-right: auto; | |
width: 1200px; | |
height: 400px; | |
} | |
</style> | |
</head> | |
<body > | |
<div id = 'chart1' class = 'rChart nvd3'></div> | |
<script type='text/javascript'> | |
$(document).ready(function(){ | |
drawchart1() | |
}); | |
function drawchart1(){ | |
var opts = { | |
"dom": "chart1", | |
"width": 1200, | |
"height": 400, | |
"x": "Region.Name", | |
"y": "value", | |
"group": "variable", | |
"type": "multiBarChart", | |
"id": "chart1" | |
}, | |
data = [ | |
{ | |
"Area.Name": 107, | |
"Region.Name": "East Midlands Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 1841510 | |
}, | |
{ | |
"Area.Name": 99, | |
"Region.Name": "East of England Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 1980273 | |
}, | |
{ | |
"Area.Name": 51, | |
"Region.Name": "London Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 5283157 | |
}, | |
{ | |
"Area.Name": 48, | |
"Region.Name": "North East Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 851526 | |
}, | |
{ | |
"Area.Name": 71, | |
"Region.Name": "North West Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 2744545 | |
}, | |
{ | |
"Area.Name": 151, | |
"Region.Name": "South East Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 2414414 | |
}, | |
{ | |
"Area.Name": 39, | |
"Region.Name": "South West Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 2141191 | |
}, | |
{ | |
"Area.Name": 89, | |
"Region.Name": "Wales", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 1059248 | |
}, | |
{ | |
"Area.Name": 158, | |
"Region.Name": "West Midlands Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 1819397 | |
}, | |
{ | |
"Area.Name": 133, | |
"Region.Name": "Yorkshire and the Humber Region", | |
"variable": "Number.of.calls.for.assistance", | |
"value": 1049847 | |
}, | |
{ | |
"Area.Name": 107, | |
"Region.Name": "East Midlands Region", | |
"variable": "Total.crime", | |
"value": 246707 | |
}, | |
{ | |
"Area.Name": 99, | |
"Region.Name": "East of England Region", | |
"variable": "Total.crime", | |
"value": 313845 | |
}, | |
{ | |
"Area.Name": 51, | |
"Region.Name": "London Region", | |
"variable": "Total.crime", | |
"value": 749813 | |
}, | |
{ | |
"Area.Name": 48, | |
"Region.Name": "North East Region", | |
"variable": "Total.crime", | |
"value": 138189 | |
}, | |
{ | |
"Area.Name": 71, | |
"Region.Name": "North West Region", | |
"variable": "Total.crime", | |
"value": 439074 | |
}, | |
{ | |
"Area.Name": 151, | |
"Region.Name": "South East Region", | |
"variable": "Total.crime", | |
"value": 452106 | |
}, | |
{ | |
"Area.Name": 39, | |
"Region.Name": "South West Region", | |
"variable": "Total.crime", | |
"value": 255292 | |
}, | |
{ | |
"Area.Name": 89, | |
"Region.Name": "Wales", | |
"variable": "Total.crime", | |
"value": 179160 | |
}, | |
{ | |
"Area.Name": 158, | |
"Region.Name": "West Midlands Region", | |
"variable": "Total.crime", | |
"value": 426149 | |
}, | |
{ | |
"Area.Name": 133, | |
"Region.Name": "Yorkshire and the Humber Region", | |
"variable": "Total.crime", | |
"value": 249484 | |
}, | |
{ | |
"Area.Name": 107, | |
"Region.Name": "East Midlands Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 79584 | |
}, | |
{ | |
"Area.Name": 99, | |
"Region.Name": "East of England Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 82199 | |
}, | |
{ | |
"Area.Name": 51, | |
"Region.Name": "London Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 226616 | |
}, | |
{ | |
"Area.Name": 48, | |
"Region.Name": "North East Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 58012 | |
}, | |
{ | |
"Area.Name": 71, | |
"Region.Name": "North West Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 143257 | |
}, | |
{ | |
"Area.Name": 151, | |
"Region.Name": "South East Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 120282 | |
}, | |
{ | |
"Area.Name": 39, | |
"Region.Name": "South West Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 60282 | |
}, | |
{ | |
"Area.Name": 89, | |
"Region.Name": "Wales", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 46771 | |
}, | |
{ | |
"Area.Name": 158, | |
"Region.Name": "West Midlands Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 123123 | |
}, | |
{ | |
"Area.Name": 133, | |
"Region.Name": "Yorkshire and the Humber Region", | |
"variable": "calls.for.assistance.with.DA.marker", | |
"value": 70351 | |
}, | |
{ | |
"Area.Name": 107, | |
"Region.Name": "East Midlands Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 19396 | |
}, | |
{ | |
"Area.Name": 99, | |
"Region.Name": "East of England Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 26655 | |
}, | |
{ | |
"Area.Name": 51, | |
"Region.Name": "London Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 53540 | |
}, | |
{ | |
"Area.Name": 48, | |
"Region.Name": "North East Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 12604 | |
}, | |
{ | |
"Area.Name": 71, | |
"Region.Name": "North West Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 34144 | |
}, | |
{ | |
"Area.Name": 151, | |
"Region.Name": "South East Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 35105 | |
}, | |
{ | |
"Area.Name": 39, | |
"Region.Name": "South West Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 21712 | |
}, | |
{ | |
"Area.Name": 89, | |
"Region.Name": "Wales", | |
"variable": "crime.with.a.DA.marker", | |
"value": 13100 | |
}, | |
{ | |
"Area.Name": 158, | |
"Region.Name": "West Midlands Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 32125 | |
}, | |
{ | |
"Area.Name": 133, | |
"Region.Name": "Yorkshire and the Humber Region", | |
"variable": "crime.with.a.DA.marker", | |
"value": 21317 | |
} | |
] | |
if(!(opts.type==="pieChart" || opts.type==="sparklinePlus" || opts.type==="bulletChart")) { | |
var data = d3.nest() | |
.key(function(d){ | |
//return opts.group === undefined ? 'main' : d[opts.group] | |
//instead of main would think a better default is opts.x | |
return opts.group === undefined ? opts.y : d[opts.group]; | |
}) | |
.entries(data); | |
} | |
if (opts.disabled != undefined){ | |
data.map(function(d, i){ | |
d.disabled = opts.disabled[i] | |
}) | |
} | |
nv.addGraph(function() { | |
var chart = nv.models[opts.type]() | |
.width(opts.width) | |
.height(opts.height) | |
if (opts.type != "bulletChart"){ | |
chart | |
.x(function(d) { return d[opts.x] }) | |
.y(function(d) { return d[opts.y] }) | |
} | |
d3.select("#" + opts.id) | |
.append('svg') | |
.datum(data) | |
.transition().duration(500) | |
.call(chart); | |
nv.utils.windowResize(chart.update); | |
return chart; | |
}); | |
}; | |
</script> | |
<script></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment