Skip to content

Instantly share code, notes, and snippets.

@jcody
Last active December 16, 2015 21:28
Show Gist options
  • Save jcody/5499442 to your computer and use it in GitHub Desktop.
Save jcody/5499442 to your computer and use it in GitHub Desktop.
Visualization of SF Crime over last 3 months
# Visualization of SF Crime over last 3 months
# Data courtesy of https://data.sfgov.org/Public-Safety/SFPD-Incidents-Previous-Three-Months/tmnf-yvry
# Joey Cody - 5/1/13
library(googleVis)
sfcrime <- read.csv("~/Desktop/sfcrime.csv")
attach(sfcrime)
dcount <- as.data.frame(table(DayOfWeek))
tcount <- as.data.frame(table(Category))
day <- gvisColumnChart(dcount, options=list(title="Number of Crimes per Day (past 3 Months)", hAxis="{title: 'Day of Week'}", vAxis="{title: 'Number of Crimes'}"))
type <- gvisPieChart(tcount, numvar = "10", options=list(title="Type of Crime"))
#plot(day)
#plot(type)
allcrime <- gvisMerge(day, type, horizontal = FALSE, tableOptions = "cellspacing=5")
plot(allcrime)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MergedIDd1c5c08164f</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<style type="text/css">
body {
color: #444444;
font-family: Arial,Helvetica,sans-serif;
font-size: 75%;
}
a {
color: #4D87C7;
text-decoration: none;
}
</style>
</head>
<body>
<!-- ColumnChart generated in R 2.15.0 by googleVis 0.4.2 package -->
<!-- Wed May 1 17:43:49 2013 -->
<!-- jsHeader -->
<script type="text/javascript">
// jsData
function gvisDataColumnChartIDd1c3119320 () {
var data = new google.visualization.DataTable();
var datajson =
[
[
"Friday",
4139
],
[
"Monday",
3416
],
[
"Saturday",
3914
],
[
"Sunday",
3597
],
[
"Thursday",
3902
],
[
"Tuesday",
3829
],
[
"Wednesday",
3871
]
];
data.addColumn('string','DayOfWeek');
data.addColumn('number','Freq');
data.addRows(datajson);
return(data);
}
// jsData
function gvisDataPieChartIDd1c76d21b72 () {
var data = new google.visualization.DataTable();
var datajson =
[
[
"ARSON",
69
],
[
"ASSAULT",
2084
],
[
"BAD CHECKS",
6
],
[
"BURGLARY",
1132
],
[
"DISORDERLY CONDUCT",
133
],
[
"DRIVING UNDER THE INFLUENCE",
98
],
[
"DRUG/NARCOTIC",
1581
],
[
"DRUNKENNESS",
156
],
[
"EMBEZZLEMENT",
17
],
[
"EXTORTION",
4
],
[
"FAMILY OFFENSES",
6
],
[
"FORGERY/COUNTERFEITING",
87
],
[
"FRAUD",
473
],
[
"GAMBLING",
4
],
[
"KIDNAPPING",
76
],
[
"LARCENY/THEFT",
6848
],
[
"LIQUOR LAWS",
47
],
[
"LOITERING",
22
],
[
"MISSING PERSON",
900
],
[
"NON-CRIMINAL",
2951
],
[
"OTHER OFFENSES",
3489
],
[
"PROSTITUTION",
77
],
[
"RECOVERED VEHICLE",
175
],
[
"ROBBERY",
901
],
[
"RUNAWAY",
69
],
[
"SEX OFFENSES, FORCIBLE",
107
],
[
"SEX OFFENSES, NON FORCIBLE",
1
],
[
"SUICIDE",
16
],
[
"SUSPICIOUS OCC",
631
],
[
"TRESPASS",
201
],
[
"VANDALISM",
1367
],
[
"VEHICLE THEFT",
1200
],
[
"WARRANTS",
1495
],
[
"WEAPON LAWS",
245
]
];
data.addColumn('string','Category');
data.addColumn('number','Freq');
data.addRows(datajson);
return(data);
}
// jsDrawChart
function drawChartColumnChartIDd1c3119320() {
var data = gvisDataColumnChartIDd1c3119320();
var options = {};
options["allowHtml"] = true;
options["title"] = "Number of Crimes per Day (past 3 Months)";
options["hAxis"] = {title: 'Day of Week'};
options["vAxis"] = {title: 'Number of Crimes'};
var chart = new google.visualization.ColumnChart(
document.getElementById('ColumnChartIDd1c3119320')
);
chart.draw(data,options);
}
// jsDrawChart
function drawChartPieChartIDd1c76d21b72() {
var data = gvisDataPieChartIDd1c76d21b72();
var options = {};
options["allowHtml"] = true;
options["title"] = "Type of Crime";
var chart = new google.visualization.PieChart(
document.getElementById('PieChartIDd1c76d21b72')
);
chart.draw(data,options);
}
// jsDisplayChart
(function() {
var pkgs = window.__gvisPackages = window.__gvisPackages || [];
var callbacks = window.__gvisCallbacks = window.__gvisCallbacks || [];
var chartid = "corechart";
// Manually see if chartid is in pkgs (not all browsers support Array.indexOf)
var i, newPackage = true;
for (i = 0; newPackage && i < pkgs.length; i++) {
if (pkgs[i] === chartid)
newPackage = false;
}
if (newPackage)
pkgs.push(chartid);
// Add the drawChart function to the global list of callbacks
callbacks.push(drawChartColumnChartIDd1c3119320);
})();
function displayChartColumnChartIDd1c3119320() {
var pkgs = window.__gvisPackages = window.__gvisPackages || [];
var callbacks = window.__gvisCallbacks = window.__gvisCallbacks || [];
window.clearTimeout(window.__gvisLoad);
// The timeout is set to 100 because otherwise the container div we are
// targeting might not be part of the document yet
window.__gvisLoad = setTimeout(function() {
var pkgCount = pkgs.length;
google.load("visualization", "1", { packages:pkgs, callback: function() {
if (pkgCount != pkgs.length) {
// Race condition where another setTimeout call snuck in after us; if
// that call added a package, we must not shift its callback
return;
}
while (callbacks.length > 0)
callbacks.shift()();
} });
}, 100);
}
// jsDisplayChart
(function() {
var pkgs = window.__gvisPackages = window.__gvisPackages || [];
var callbacks = window.__gvisCallbacks = window.__gvisCallbacks || [];
var chartid = "corechart";
// Manually see if chartid is in pkgs (not all browsers support Array.indexOf)
var i, newPackage = true;
for (i = 0; newPackage && i < pkgs.length; i++) {
if (pkgs[i] === chartid)
newPackage = false;
}
if (newPackage)
pkgs.push(chartid);
// Add the drawChart function to the global list of callbacks
callbacks.push(drawChartPieChartIDd1c76d21b72);
})();
function displayChartPieChartIDd1c76d21b72() {
var pkgs = window.__gvisPackages = window.__gvisPackages || [];
var callbacks = window.__gvisCallbacks = window.__gvisCallbacks || [];
window.clearTimeout(window.__gvisLoad);
// The timeout is set to 100 because otherwise the container div we are
// targeting might not be part of the document yet
window.__gvisLoad = setTimeout(function() {
var pkgCount = pkgs.length;
google.load("visualization", "1", { packages:pkgs, callback: function() {
if (pkgCount != pkgs.length) {
// Race condition where another setTimeout call snuck in after us; if
// that call added a package, we must not shift its callback
return;
}
while (callbacks.length > 0)
callbacks.shift()();
} });
}, 100);
}
// jsFooter
</script>
<!-- jsChart -->
<script type="text/javascript" src="https://www.google.com/jsapi?callback=displayChartColumnChartIDd1c3119320"></script>
<!-- jsChart -->
<script type="text/javascript" src="https://www.google.com/jsapi?callback=displayChartPieChartIDd1c76d21b72"></script>
<table cellspacing=5>
<tr>
<td>
<!-- divChart -->
<div id="ColumnChartIDd1c3119320"
style="width: 600px; height: 500px;">
</div>
</td>
</tr>
<tr>
<td>
<!-- divChart -->
<div id="PieChartIDd1c76d21b72"
style="width: 600px; height: 500px;">
</div>
</td>
</tr>
</table>
<div><span>Data: various &#8226; Chart ID: <a href="Chart_MergedIDd1c5c08164f.html">MergedIDd1c5c08164f</a></span><br />
<!-- htmlFooter -->
<span>
R version 2.15.0 (2012-03-30) &#8226; <a href="http://code.google.com/p/google-motion-charts-with-r/">googleVis-0.4.2</a>
&#8226; <a href="https://developers.google.com/terms/">Google Terms of Use</a> &#8226; Data Policy: See individual charts
</span></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment