<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/crime-analysis-denver-part-1/"></div></div><div><a data-url="https://www.r-bloggers.com/crime-analysis-denver-part-1/" data-counturl="https://www.r-bloggers.com/crime-analysis-denver-part-1/" data-count="horizontal" data-text="Crime Analysis – Denver-Part 1" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://stoltzmaniac.com/crime-analysis-denver-part-1/"> R-Projects - Stoltzmaniac</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h4>Project Background</h4>
<p>As we all know, Colorado is considered one of the scariest places on earth. Denver, CO has had an enormous influx of people over the last decade and it is still ramping up. </p>
<p>So why did I pick Denver?</p>
<p>That’s simple, I have lived in Colorado for the majority of my life and want to know more about my capital city.</p>
<p><strong>Exploration of Data</strong> <br>
Data provided by <a target="_blank" rel="nofollow" href="https://www.denvergov.org/opendata/dataset/city-and-county-of-denver-crime">https://www.denvergov.org/opendata/dataset/city-and-county-of-denver-crime</a></p>
<p><strong>What we’ll do in this post</strong></p>
<ul>
<li>Import the crime.csv data set</li>
<li>Format the data</li>
<li>Plot the total number of incidents reported by year</li>
</ul>
<p>Let’s dive in! </p>
<p><strong>Import the necessary libraries</strong></p>
<pre><code>library(dplyr)  
library(lubridate)  
library(ggplot2)  
options(&quot;stringsAsFactors&quot; = TRUE)  
</code></pre>
<p><strong>Load the crime data set provided</strong> <br>
It is possible to load the data straight from the URL, however, it’s over 80MB in size, so I simply downloaded it. It is updated regularly, so this post may need to be refreshed from time to time. </p>
<pre><code>####
# Data from: http://data.denvergov.org/dataset/city-and-county-of-denver-crime
# File name: crime.csv
CWD = getwd()  
data = read.csv(paste(CWD,'/data/crime.csv',sep=''))  
####
</code></pre>
<p><strong>Format the data</strong> <br>
I added columns for year, month, day and hour into the dataframe in order to simplify life. It takes a bit more time and ram upfront but I prefer to see it that way. </p>
<pre><code>#Format FIRST_OCCURRENCE_DATE as.Date and use as crime date (for now)
data$date = as.Date(data$FIRST_OCCURRENCE_DATE)

#Create new columns for grouping
data$year = year(data$date)  
data$month = month(data$date)  
data$day = day(data$date)  
data$hour = hour(data$FIRST_OCCURRENCE_DATE)

print(colnames(data))  
</code></pre>
<p><strong>Basic Plotting</strong> <br>
ggplot2 will provide a decent chart to show us the number of incidents each year. </p>
<pre><code>#Sum up all incidents IS_CRIME AND IS_TRAFFIC
maxYear = max(data$year)  
maxMonthYTD = max(data$month[data$year==maxYear])

df = data %>%  
  group_by(year,month) %>%
  filter(month &lt; maxMonthYTD) %>%
  summarise(incidents = sum(IS_CRIME) + sum(IS_TRAFFIC)) %>%
  arrange(month)

p = ggplot(df)  
p + geom_bar(aes(x = factor(year), weight = incidents)) + ggtitle('Incidents Reported by Year') + xlab('Year') + ylab('Incidents') + theme(plot.title = element_text(hjust = 0.5))
</code></pre>
<p>
<img alt="barplotCOLOR" src="https://i2.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-1/crime-analysis-denver-part-1-plot1.png?w=456" data-recalc-dims="1"></p>
<p><strong>Adding Some Color</strong> <br>
Looking at the same plot but adding in colors for each month of the year.</p>
<pre><code>#Stack bars in colors to view individual months
p = ggplot(df,aes(x=factor(year),y=incidents,fill=factor(month)))  
p + geom_bar(stat='identity') + ggtitle('Incidents Reported by Year') + xlab('Year') + ylab('Incidents') + theme(plot.title = element_text(hjust = 0.5)) + guides(fill = guide_legend(title='Month'))  
</code></pre>
<p><img title="" alt="barplotCOLOR" src="https://i0.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-1/crime-analysis-denver-part-1-plot2.png?w=456" data-recalc-dims="1"> </p>
<p><strong>How much is labeled as TRAFFIC or CRIME</strong> <br>
Looking at the same plot but separating out IS<em>TRAFFIC from IS</em>CRIME</p>
<h1>Stack bars in colors to view individual months</h1>
<pre><code>tmp= data  
tmp$crimeType[tmp$IS_CRIME == 1] = 'Crime'  
tmp$crimeType[tmp$IS_CRIME == 0] = 'Traffic'  
tmp$crimeType = factor(tmp$crimeType) 

df = tmp %>%  
  group_by(year,crimeType) %>%
  filter(month &lt; maxMonthYTD) %>%
  summarise(crimeIncidents = sum(IS_CRIME) + sum(IS_TRAFFIC)) %>%
  arrange(year)

p = ggplot(df,aes(x=factor(year),y=crimeIncidents,fill=crimeType))  
p + geom_bar(stat='identity') + ggtitle('Incidents Reported by Year') + xlab('Year') + ylab('Incidents') + theme(plot.title = element_text(hjust = 0.5)) + guides(fill = guide_legend(title='Incident Type'))  
</code></pre>
<p><img alt="barplotCOLOR" src="https://i1.wp.com/stoltzmaniac.com/content/images/2016/11/crime-analysis-denver-part-1/crime-analysis-denver-part-1-plot3.png?w=456" data-recalc-dims="1"></p>
<p><strong>Initial Impressions</strong> <br>
Having isolated only months that have occurred in each year, we’ve seen volume increase most years. The most rapid growth seemed to occur between 2012 – 2014. It appears as if traffic violations seem to be roughly flat and the growth in crimes is much higher. I’ll have to dig into those years in order to see if there’s evidence of a change in crime rate or if something else is hiding in the data.</p>
<p><strong>What I’ll do in the next crime posts</strong></p>
<ul>
<li>Determine year-over-year differences in crime</li>
<li>Dig into the apparent crime rate growth from 2012 – 2014</li>
<li>Look for patterns by location</li>
<li>Answer the question: What types of crimes have grown the most in the last 5 years?</li>
</ul>
<p>Code used in this post is on my <a target="_blank" rel="nofollow" href="https://github.com/stoltzmaniac/Denver-Crime-Analysis.git">GitHub</a></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/crime-analysis-denver-part-1/"></div></div><div><a data-url="https://www.r-bloggers.com/crime-analysis-denver-part-1/" data-counturl="https://www.r-bloggers.com/crime-analysis-denver-part-1/" data-count="vertical" data-text="Crime Analysis – Denver-Part 1" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://stoltzmaniac.com/crime-analysis-denver-part-1/"> R-Projects - Stoltzmaniac</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/XsGL-CAGgKk"></a></div>