---
title: "Crime Rates in USA -- Increasing or Decreasing?"
author: Brian Rathod (s3760875) and Syed Wajahath (s3750039)
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
source_code: embed
---
```{r setup, include=FALSE}
rm(list = ls())
library(flexdashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(forcats)
library(tidyr)
library(highcharter)
library(multilevel)
library(treemap)
library(usmap)
library(stringr)
library(rsconnect)
setwd("C:/Users/Wajahath/Desktop/MS/Sem 2/Data Vis/Assignment 3/crime-rates")
crimes <- read.csv("report.csv")
crimes <- crimes[!grepl("United States", crimes$agency_jurisdiction),]
us_states <- read.csv("statelatlong.csv")
x<- read.csv("statelatlong.csv")
#Following are links to the data source considered. These are included in the references as well.
#Data sources:
#[1] www.kaggle.com. (2019). marshallproject. [online] Available at: https://www.kaggle.com/marshallproject/crime-rates [Accessed 26 Oct. 2019].
#[2] www.kaggle.com. (2019). usa-latlong-for-state-abbreviations. [online] Available at: https://www.kaggle.com/washimahmed/usa-latlong-for-state-abbreviations [Accessed 26 Oct. 2019].
#[3] library(usmap)
```
Column {data-width=600}
-----------------------------------------------------------------------
### The Marshall Project is a non-profit organization focusing on criminal justice related issues in the United States. It has published 40 years of data about the major crime categories - assaults, robberies, rapes and homicides. We use this data to obtain insights into the prevailing crime trends.
Crimes in USA have fared differently over the years. Do the spikes in the graph suggest increase in crimes?
The trends suggest there have been drastic fluctuations over time.
```{r}
crimesvsyear <- crimes %>% group_by(report_year) %>%
summarise(violent_crimes = sum(violent_crimes, na.rm = TRUE),
homicides =sum(homicides, na.rm = TRUE),
rapes =sum(rapes, na.rm = TRUE),
assaults =sum(assaults, na.rm = TRUE),
robberies =sum(robberies, na.rm = TRUE))
plot1 <- ggplot(crimesvsyear, aes(x= report_year, y = y, color = Crime))+
geom_line(aes(y =crimesvsyear$violent_crimes, col = "Violent_Crimes")) + geom_line(aes(y = crimesvsyear$homicides, col = "Homicides")) +
geom_line(aes(y = crimesvsyear$rapes, col = "Rapes")) +
geom_line(aes(y = crimesvsyear$assaults, col = "Assaults")) +
geom_line(aes(y = crimesvsyear$robberies, col = "Robberies")) +
xlim(1975,2015) + theme_bw() +
ggtitle("Crime Trends over four decades") +
xlab("Years (1975----2015)") + ylab("Crime Count")
ggplotly(plot1)
```
### Which state of USA has the highest number of crimes?(New York --9180208)
```{r include=FALSE}
crimevsjurisdiction <- crimes %>% group_by(agency_jurisdiction) %>%
summarise(violent_crimes = sum(violent_crimes, na.rm = TRUE),
homicides =sum(homicides, na.rm = TRUE),
rapes =sum(rapes, na.rm = TRUE),
assaults =sum(assaults, na.rm = TRUE),
robberies =sum(robberies, na.rm = TRUE),
Totalcrimes = sum(violent_crimes+homicides+rapes+assaults+robberies, na.rm = TRUE))
crimevsjurisdiction$place<- str_sub(crimevsjurisdiction$agency_jurisdiction,1, -5)
crimevsjurisdiction$state<- str_sub(crimevsjurisdiction$agency_jurisdiction,-2)
crimesperstate <- crimevsjurisdiction %>% group_by(state) %>%
summarise(violent_crimes = sum(violent_crimes, na.rm = TRUE),
homicides =sum(homicides, na.rm = TRUE),
rapes =sum(rapes, na.rm = TRUE),
assaults =sum(assaults, na.rm = TRUE),
robberies =sum(robberies, na.rm = TRUE),
Totalcrimes = sum(violent_crimes+homicides+rapes+assaults+robberies, na.rm = TRUE))
crimesperstate<- crimesperstate[-5,]
cps<- crimesperstate %>% left_join(x, by = c("state" = "State"))
states <- map_data("state")
cps$City <- tolower(cps$City)
states2 <- left_join(states, cps, by = c('region' = 'City'))
```
```{r warning=FALSE}
crimeperstateplot<- ggplot(data = states2,
aes(x = long, y = lat,fill= Totalcrimes, group = group,
label = state)) +
geom_polygon(color = "white") + coord_fixed(1.3) + theme_void() +
ggtitle("Crimes per States") +xlab("Longitude of States") +
ylab("Latitude of States")
ggplotly(crimeperstateplot)
```
Column {.tabset data-width=400}
-----------------------------------------------------------------------
### The larger cities experience more crimes per capita as exhibited in the following graph.
```{r include=FALSE}
crimevsjurisdiction <- crimes %>% group_by(agency_jurisdiction) %>%
summarise(violent_crimes = mean(crimes_percapita, na.rm = TRUE),
homicides =mean(homicides_percapita, na.rm = TRUE),
rapes =mean(rapes_percapita, na.rm = TRUE),
assaults =mean(assaults_percapita, na.rm = TRUE),
robberies =mean(robberies_percapita, na.rm = TRUE),
Totalcrimes = sum(violent_crimes+homicides+rapes+assaults+robberies,na.rm = TRUE))
city <- crimevsjurisdiction$agency_jurisdiction
city <- gsub("\\,.*","",city)
agency <- c(city)
crime_count <- c(crimevsjurisdiction$Totalcrimes)
crime_count <- round(crime_count, digits = 1)
agency_crime <- data.frame(agency,crime_count)
crimesperagency <- treemap(agency_crime, index = c("agency"),
vSize = "crime_count", vColor = "crime_count",
type = "value", palette = "RdYlBu", n=60)
mytheme <- hc_theme(colors = c("#4ef5dc", "#0f07f7", "#53f707"),
chart = list(backgroundColor = "transparent",
style = list(fontFamily = "PT Serif"),
fontSize= 0.5),
xAxis = list(gridLineWidth = 1))
```
```{r}
highchart() %>% hc_add_series_treemap(crimesperagency,allowDrillToNode = TRUE,
layoutAlgorithm = "squarified") %>%
hc_title(text = "PerCapita Crime Count") %>% hc_add_theme(mytheme)
```
### The average count of crimes in different decades are about the same. However, off late, there is an increase in Violent crimes and Rapes which could be curbed.(Click here for Barplot)
```{r}
crimesperyear <- crimes %>% group_by(report_year) %>%
summarise(violent_crimes = sum(violent_crimes, na.rm = TRUE),
homicides =sum(homicides, na.rm = TRUE),
rapes =sum(rapes, na.rm = TRUE),
assaults =sum(assaults, na.rm = TRUE),
robberies =sum(robberies, na.rm = TRUE))
cp1 <- crimesperyear[c(1:10),]
cp2 <- crimesperyear[c(11:20),]
cp3 <- crimesperyear[c(21:30),]
cp4 <- crimesperyear[c(31:40),]
cpdf <- data.frame(cp1, cp2, cp3, cp4)
decade <- c("1975-84" ,"1985-94" ,"1995-04","2004-14")
crimes11 <- rep(c("Violent" , "Homicides" , "Rapes", "Assaults", "Robberies") , 4)
value <- c(cp1$violent_crimes, cp1$homicides, cp1$rapes, cp1$assaults, cp1$robberies,
cp2$violent_crimes, cp2$homicides, cp2$rapes, cp2$assaults, cp2$robberies,
cp3$violent_crimes, cp3$homicides, cp3$rapes, cp3$assaults, cp3$robberies,
cp4$violent_crimes, cp4$homicides, cp4$rapes, cp4$assaults, cp4$robberies)
crimesperdecade <- data.frame(decade,crimes11,value)
crimes_decade_plot <- ggplot(crimesperdecade, aes(fill=crimes11, y=value, x=decade)) +
geom_bar(position="dodge", stat="identity") +
labs(x = "Over the Decades", y = "Count") + theme_dark() +
ggtitle("Count of Crimes in different Decades") +
scale_fill_manual(values=c("#e66101","#fdb863","#f7f7f7","#b2abd2","#5e3c99"))
ggplotly(crimes_decade_plot)
```
```{r eval=FALSE, include=FALSE}
References
[1] www.kaggle.com. (2019). marshallproject. [online] Available at: https://www.kaggle.com/marshallproject/crime-rates [Accessed 26 Oct. 2019].
[2] www.kaggle.com. (2019). usa-latlong-for-state-abbreviations. [online] Available at: https://www.kaggle.com/washimahmed/usa-latlong-for-state-abbreviations [Accessed 26 Oct. 2019].
[3] Sthda.com. (2019). ggplot2 line plot : Quick start guide - R software and data visualization - Easy Guides - Wiki - STHDA. [online] Available at: http://www.sthda.com/english/wiki/ggplot2-line-plot-quick-start-guide-r-software-and-data-visualization [Accessed 26 Oct. 2019].
[4] Socviz.co. (2019). Data Visualization. [online] Available at: https://socviz.co/maps.html [Accessed 26 Oct. 2019].
[5] DataCamp Community. (2019). Data Visualization with Highcharter in R. [online] Available at: https://www.datacamp.com/community/tutorials/data-visualization-highcharter-r [Accessed 26 Oct. 2019].
[6] Ggplot2.tidyverse.org. (2019). Points — geom_point. [online] Available at: https://ggplot2.tidyverse.org/reference/geom_point.html [Accessed 26 Oct. 2019].
[7] DataCamp Community. (2019). Data Visualization with Highcharter in R. [online] Available at: https://www.datacamp.com/community/tutorials/data-visualization-highcharter-r [Accessed 26 Oct. 2019].
[8] Dark-star-161610.appspot.com. (2019). Sign in – Google accounts. [online] Available at: https://dark-star-161610.appspot.com/secured/_book/dashboards.html#flexdashboard [Accessed 26 Oct. 2019].
[9] Rmarkdown.rstudio.com. (2019). Using flexdashboard. [online] Available at: https://rmarkdown.rstudio.com/flexdashboard/using.html [Accessed 26 Oct. 2019].
```