2015

Column

DAY WISE CRIME

MONTH WISE CRIME

OFG

STREET WISE CRIME

Column

UCR

2016

Column

DAY WISE CRIME

MONTH WISE CRIME

OFG

STREET WISE CRIME

Column

2017

Column

DAY

MONTH

OFG

STREET

Column

2018

Column

DAY

MONTH

OFG

STREET

Column

Boston_MAP

Column

2016

2017

2018

About

Column

Conclusions:

¶ In summary, this EDA shows:

Larceny is by far the most common type of serious crime. Serious crimes are most likely to occur in the afternoon and evening. Serious crimes are most likely to occur on Friday and least likely to occur on Sunday. Serious crimes are most likely to occur in the summer and early fall, and least likely to occur in the winter (with the exeption of January, which has a crime rate more similar to the summer). There is no obvious connection between major holidays and crime rates. Serious crimes are most common in the city center, especially districts A1 and D4. This EDA just scratches the surface of the dataset. Further analyses could explore how different types of crimes vary in time and space. I didn’t even consider the less serious UCR Part Two and Part Three crimes, which are far more common than Part One crimes, but include interesting categories such as drug crimes. Another interesting direction would be to combine this with other data about Boston, such as demography or even the weather, to investigate what factors predict crime rates across time and space.

---
title: "Boston Crime" 
# author: "Suman ghorai"
#Mail : ghorai77@gmail.com
# date: "August 13, 2019"
output: 
  flexdashboard::flex_dashboard:
    source_code: embed
    vertical_layout: scroll
    theme: yeti
---


``` {js}
// Inverse color of navigation bar.
$('.navbar-inverse').removeClass('navbar-inverse').addClass('navbar-default');
```

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
library(plotly)
library(tidyverse)
library(plotly)
library(readr)
library(highcharter)
library(leaflet)
```

```{r include=FALSE}
# Read in data. 
data <- read_csv("C://Users//hi//Documents//crime.csv")
data$OFFENSE_CODE=as.factor(data$OFFENSE_CODE)
data$OFFENSE_CODE_GROUP=as.factor(data$OFFENSE_CODE_GROUP)
data$OFFENSE_DESCRIPTION=as.factor(data$OFFENSE_DESCRIPTION)
data$UCR_PART =as.factor(data$UCR_PART)
data$STREET=as.factor(data$STREET)
data$MONTH =as.factor(data$MONTH)
data$DAY_OF_WEEK=data$DAY_OF_WEEK
data$YEAR=as.factor(data$YEAR)
data$DISTRICT=as.factor(data$DISTRICT)
```

```{r include=FALSE}
# Create a custom theme for the plots. 
custom_theme <- hc_theme(
  colors = c('#5CACEE', 'green', 'red'),
  chart = list(
         backgroundColor = '#FAFAFA', 
         plotBorderColor = "black"),
  xAxis = list(
         gridLineColor = "E5E5E5", 
         labels = list(style = list(color = "#333333")), 
         lineColor = "#E5E5E5", 
         minorGridLineColor = "#E5E5E5", 
         tickColor = "#E5E5E5", 
         title = list(style = list(color = "#333333"))), 
  yAxis = list(
         gridLineColor = "#E5E5E5", 
         labels = list(style = list(color = "#333333")), 
         lineColor = "#E5E5E5", 
         minorGridLineColor = "#E5E5E5", 
         tickColor = "#E5E5E5", 
         tickWidth = 1, 
         title = list(style = list(color = "#333333"))),   
  title = list(style = list(color = '#333333', fontFamily = "Lato")),
  subtitle = list(style = list(color = '#666666', fontFamily = "Lato")),
  legend = list(
         itemStyle = list(color = "#333333"), 
         itemHoverStyle = list(color = "#FFF"), 
         itemHiddenStyle = list(color = "#606063")), 
  credits = list(style = list(color = "#666")),
  itemHoverStyle = list(color = 'gray'))
```




2015
=======================================================================

 Column {.tabset .tabset-fade data-width=700 .colored }
-----------------------------------------------------------------------

### DAY WISE  CRIME {.no-padding}
```{r fig.height=5}
date_2015_DAY <-data %>% filter(data$YEAR==2015) %>% select(DAY_OF_WEEK) %>% group_by(DAY_OF_WEEK) %>% summarize(COUNT=n())
hchart(date_2015_DAY, "column", hcaes(x = DAY_OF_WEEK, y = COUNT))

```

### MONTH WISE CRIME {.no-padding}
```{r fig.height=5.14}
date_2015_MONTH <- data %>% filter(data$YEAR==2015) %>% select(MONTH) %>% group_by(MONTH) %>% summarize(COUNT=n()) %>%mutate(Name=month.abb[as.numeric(MONTH)])
hchart(date_2015_MONTH,"line", hcaes(x=Name,y=COUNT))
```

### OFG  {.no-padding}
```{r fig.height=5.13}
date_2015_OFG <- data %>% filter(data$YEAR==2015) %>% select(OFFENSE_CODE_GROUP) %>% group_by(OFFENSE_CODE_GROUP) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
hchart(date_2015_OFG, "column", hcaes(x = OFFENSE_CODE_GROUP, y = COUNT))
```

### STREET WISE CRIME {.no-padding}
```{r fig.height=5.13}
date_2015_STREET <- data %>% filter(data$YEAR==2015) %>% select(STREET) %>% group_by(STREET) %>% filter(!all(is.na(STREET))) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
date_2015_STREET1 <- head(date_2015_STREET,50)
hchart(date_2015_STREET1, "column", hcaes(x = STREET, y = COUNT))
```

Column {data-width=300}
-----------------------------------------------------------------------



### UCR {.no-title .no-padding .colored }
```{r fig.height=1.9}
date_2015_UCR <- data %>% select(UCR_PART)  %>% filter(data$YEAR==2015) %>% group_by(UCR_PART) %>% summarize(CR=n()) %>% mutate(PR=(CR/sum(CR))*100) 
# Create pie chart 
highchart() %>% 
  hc_add_series(date_2015_UCR, hcaes(x = UCR_PART, y = PR
                               ), type = "pie") %>%
  hc_tooltip(borderWidth = 1.5, headerFormat = "", pointFormat = paste("UCR: {point.CR} ({point.percentage:.1f}%)")) %>%  
  hc_title(text = "UCR Catagory", style = (list(fontSize = '14px'))) %>% 
  hc_subtitle(text = "2015", style = (list(fontSize = '10px'))) %>%
  hc_plotOptions(pie = list(dataLabels = list(distance = 5, 
                                              style = list(fontSize = 10)), 
                            size = 130)) %>% 
  hc_add_theme(custom_theme)
```





2016
========================================================================

Column {.tabset .tabset-fade data-width=700 .colored }
-----------------------------------------------------------------------
###  DAY WISE CRIME {.no-padding}
```{r fig.height=5}
date_2016_DAY <-data %>% filter(data$YEAR==2016) %>% select(DAY_OF_WEEK) %>% group_by(DAY_OF_WEEK) %>% summarize(COUNT=n())
hchart(date_2016_DAY, "column", hcaes(x = DAY_OF_WEEK, y = COUNT))
```

### MONTH WISE CRIME {.no-padding}
```{r fig.height=5.14}
date_2016_MONTH <- data %>% filter(data$YEAR==2016) %>% select(MONTH) %>% group_by(MONTH) %>% summarize(COUNT=n()) %>%mutate(Name=month.abb[as.numeric(MONTH)])
hchart(date_2016_MONTH,"line", hcaes(x=Name,y=COUNT))
```

### OFG {.no-padding}
```{r fig.height=5.13}
date_2016_OFG <- data %>% filter(data$YEAR==2016) %>% select(OFFENSE_CODE_GROUP) %>% group_by(OFFENSE_CODE_GROUP) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
hchart(date_2016_OFG, "column", hcaes(x = OFFENSE_CODE_GROUP, y = COUNT))
```

### STREET WISE CRIME {.no-padding}
```{r fig.height=5.13}
date_2016_STREET <- data %>% filter(data$YEAR==2016) %>% select(STREET) %>% group_by(STREET) %>% filter(!all(is.na(STREET))) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
date_2016_STREET1 <- head(date_2015_STREET,10)
hchart(date_2016_STREET1, "column", hcaes(x = STREET, y = COUNT))
```

Column {data-width=300}
-----------------------------------------------------------------------

```{r fig.height=1.9}
date_2016_UCR <- data %>% select(UCR_PART)  %>% filter(data$YEAR==2016) %>% group_by(UCR_PART) %>% summarize(CR=n()) %>% mutate(PR=(CR/sum(CR))*100) 
# Create pie chart 
highchart() %>% 
  hc_add_series(date_2016_UCR, hcaes(x = UCR_PART, y = PR
                               ), type = "pie") %>%
  hc_tooltip(borderWidth = 1.5, headerFormat = "", pointFormat = paste("UCR: {point.CR} ({point.percentage:.1f}%)")) %>%  
  hc_title(text = "UCR Catagory", style = (list(fontSize = '14px'))) %>% 
  hc_subtitle(text = "2016", style = (list(fontSize = '10px'))) %>%
  hc_plotOptions(pie = list(dataLabels = list(distance = 5, 
                                              style = list(fontSize = 10)), 
                            size = 130)) %>% 
  hc_add_theme(custom_theme)
```

2017
========================================================================

Column {.tabset .tabset-fade data-width=700 .colored }
-----------------------------------------------------------------------
###  DAY {.no-padding}
```{r fig.height=5}
date_2017_DAY <-data %>% filter(data$YEAR==2017) %>% select(DAY_OF_WEEK) %>% group_by(DAY_OF_WEEK) %>% summarize(COUNT=n())
hchart(date_2017_DAY, "column", hcaes(x = DAY_OF_WEEK, y = COUNT))
```

### MONTH {.no-padding}
```{r fig.height=5.14}
date_2017_MONTH <- data %>% filter(data$YEAR==2017) %>% select(MONTH) %>% group_by(MONTH) %>% summarize(COUNT=n()) %>%mutate(Name=month.abb[as.numeric(MONTH)])
hchart(date_2017_MONTH,"line", hcaes(x=Name,y=COUNT))
```

### OFG {.no-padding}
```{r fig.height=5.13}
date_2017_OFG <- data %>% filter(data$YEAR==2017) %>% select(OFFENSE_CODE_GROUP) %>% group_by(OFFENSE_CODE_GROUP) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
hchart(date_2017_OFG, "column", hcaes(x = OFFENSE_CODE_GROUP, y = COUNT))
```

### STREET {.no-padding}
```{r fig.height=5.13}
date_2017_STREET <- data %>% filter(data$YEAR==2017) %>% select(STREET) %>% group_by(STREET) %>% filter(!all(is.na(STREET))) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
date_2017_STREET1 <- head(date_2017_STREET,10)
hchart(date_2017_STREET1, "column", hcaes(x = STREET, y = COUNT))
```

Column {data-width=300}
-----------------------------------------------------------------------

```{r fig.height=1.9}
date_2017_UCR <- data %>% select(UCR_PART)  %>% filter(data$YEAR==2017) %>% group_by(UCR_PART) %>% summarize(CR=n()) %>% mutate(PR=(CR/sum(CR))*100)
# Create pie chart 
highchart() %>% 
  hc_add_series(date_2017_UCR, hcaes(x = UCR_PART, y = PR
                               ), type = "pie") %>%
  hc_tooltip(borderWidth = 1.5, headerFormat = "", pointFormat = paste("UCR: {point.CR} ({point.percentage:.1f}%)")) %>%  
  hc_title(text = "UCR Catagory", style = (list(fontSize = '14px'))) %>% 
  hc_subtitle(text = "2017", style = (list(fontSize = '10px'))) %>%
  hc_plotOptions(pie = list(dataLabels = list(distance = 5, 
                                              style = list(fontSize = 10)), 
                            size = 130)) %>% 
  hc_add_theme(custom_theme)
```

2018
========================================================================

Column {.tabset .tabset-fade data-width=700 .colored }
-----------------------------------------------------------------------
### DAY {.no-padding}
```{r fig.height=5}
date_2018_DAY <-data %>% filter(data$YEAR==2018) %>% select(DAY_OF_WEEK) %>% group_by(DAY_OF_WEEK) %>% summarize(COUNT=n())
hchart(date_2018_DAY, "column", hcaes(x = DAY_OF_WEEK, y = COUNT))
```

### MONTH {.no-padding}
```{r fig.height=5.14}
date_2018_MONTH <- data %>% filter(data$YEAR==2018) %>% select(MONTH) %>% group_by(MONTH) %>% summarize(COUNT=n()) %>%mutate(Name=month.abb[as.numeric(MONTH)])
hchart(date_2016_MONTH,"line", hcaes(x=Name,y=COUNT))
```

### OFG {.no-padding}
```{r fig.height=5.13}
date_2018_OFG <- data %>% filter(data$YEAR==2018) %>% select(OFFENSE_CODE_GROUP) %>% group_by(OFFENSE_CODE_GROUP) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
hchart(date_2018_OFG, "column", hcaes(x = OFFENSE_CODE_GROUP, y = COUNT))
```

### STREET {.no-padding}
```{r fig.height=5.13}
date_2018_STREET <- data %>% filter(data$YEAR==2018) %>% select(STREET) %>% group_by(STREET) %>% filter(!all(is.na(STREET))) %>% summarize(COUNT=n()) %>% arrange(desc(COUNT))
date_2018_STREET1 <- head(date_2015_STREET,10)
hchart(date_2018_STREET1, "column", hcaes(x = STREET, y = COUNT))
```

Column {data-width=300}
-----------------------------------------------------------------------

```{r fig.height=1.9}
date_2018_UCR <- data %>% select(UCR_PART)  %>% filter(data$YEAR==2018) %>% group_by(UCR_PART) %>% summarize(CR=n()) %>% mutate(PR=(CR/sum(CR))*100) 
# Create pie chart 
highchart() %>% 
  hc_add_series(date_2018_UCR, hcaes(x = UCR_PART, y = PR
                               ), type = "pie") %>%
  hc_tooltip(borderWidth = 1.5, headerFormat = "", pointFormat = paste("UCR: {point.CR} ({point.percentage:.1f}%)")) %>%  
  hc_title(text = "UCR Catagory", style = (list(fontSize = '14px'))) %>% 
  hc_subtitle(text = "2018", style = (list(fontSize = '10px'))) %>%
  hc_plotOptions(pie = list(dataLabels = list(distance = 5, 
                                              style = list(fontSize = 10)), 
                            size = 130)) %>% 
  hc_add_theme(custom_theme)

```





Boston_MAP
========================================================================

Column {.tabset .tabset-fade data-width=1000 .colored }
-----------------------------------------------------------------------

### 2016  {.no-padding}
``````{r fig.height=5}
g_2016 <- data %>% select(OFFENSE_CODE_GROUP,Lat,Long,YEAR) %>%filter(YEAR==2016) %>% filter(OFFENSE_CODE_GROUP %in% c('Homicide', 'Residential Burglary', 'Robbery')) %>% group_by(OFFENSE_CODE_GROUP)
g_2016$ratingcol <- ifelse(g_2016$OFFENSE_CODE_GROUP == "Homicide", "orange",
ifelse(g_2016$OFFENSE_CODE_GROUP == "Residential Burglary", "green",
                                 ifelse(g_2016$OFFENSE_CODE_GROUP == "Robbery" , "red", "black")))



g_2016 <- g_2016[-c(which(grepl(-1,g_2016$Long))), ]

leaflet() %>% 
  addTiles() %>% 
   addCircleMarkers(g_2016$Long, 
                   g_2016$Lat, 
                   color = g_2016$ratingcol, 
                   radius = 0.5, 
                   fill = T,
                   fillOpacity = 0.2,
                   opacity = 0.4,
                   popup = paste(g_2016$OFFENSE_CODE_GROUP,g_2016$Lat,g_2016$Long,sep = "")) %>%
  addLegend("bottomleft", 
            colors = c("orange","green", "red", "black"),
            labels = c("Homicide",
                       "Residential Burglary",
                       "Robbery","no crime"), 
            opacity = 0.8)

```

### 2017 {.no-padding}
```{r fig.height=4.65}
g_2017 <- data %>% select(OFFENSE_CODE_GROUP,Lat,Long,YEAR) %>%filter(YEAR==2017) %>% filter(OFFENSE_CODE_GROUP %in% c('Homicide', 'Residential Burglary', 'Robbery')) %>% group_by(OFFENSE_CODE_GROUP)
g_2017$ratingcol <- ifelse(g_2017$OFFENSE_CODE_GROUP == "Homicide", "orange",
ifelse(g_2017$OFFENSE_CODE_GROUP == "Residential Burglary", "green",
                                 ifelse(g_2017$OFFENSE_CODE_GROUP == "Robbery" , "red", "black")))



g_2017 <- g_2017[-c(which(grepl(-1,g_2017$Long))), ]

leaflet() %>% 
  addTiles() %>% 
   addCircleMarkers(g_2017$Long, 
                   g_2017$Lat, 
                   color = g_2016$ratingcol, 
                   radius = 0.5, 
                   fill = T,
                   fillOpacity = 0.2,
                   opacity = 0.4,
                   popup = paste(g_2017$OFFENSE_CODE_GROUP,g_2017$Lat,g_2017$Long,sep = "")) %>%
  addLegend("bottomleft", 
            colors = c("orange","green", "red", "black"),
            labels = c("Homicide",
                       "Residential Burglary",
                       "Robbery","no crime"), 
            opacity = 0.8)

```

### 2018  {.no-padding}
```{r fig.height=4.65}
g_2018 <- data %>% select(OFFENSE_CODE_GROUP,Lat,Long,YEAR) %>%filter(YEAR==2018) %>% filter(OFFENSE_CODE_GROUP %in% c('Homicide', 'Residential Burglary', 'Robbery')) %>% group_by(OFFENSE_CODE_GROUP)
g_2018$ratingcol <- ifelse(g_2018$OFFENSE_CODE_GROUP == "Homicide", "orange",
                           ifelse(g_2018$OFFENSE_CODE_GROUP == "Residential Burglary", "green",
                                  ifelse(g_2018$OFFENSE_CODE_GROUP == "Robbery" , "red", "black")))



g_2018 <- g_2018[-c(which(grepl(-1,g_2018$Long))), ]

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(g_2018$Long, 
                   g_2018$Lat, 
                   color = g_2018$ratingcol, 
                   radius = 0.5, 
                   fill = T,
                   fillOpacity = 0.2,
                   opacity = 0.4,
                   popup = paste(g_2018$OFFENSE_CODE_GROUP,g_2018$Lat,g_2018$Long,sep = "")) %>%
  addLegend("bottomleft", 
            colors = c("orange","green", "red", "black"),
            labels = c("Homicide",
                       "Residential Burglary",
                       "Robbery","no crime"), 
            opacity = 0.8)

```


About {data-icon="fa-info-circle"}
=======================================================================

Column {data-width=600}
-----------------------------------------------------------------------

### { .colored }





**Conclusions:**  

¶
In summary, this EDA shows:

Larceny is by far the most common type of serious crime.
Serious crimes are most likely to occur in the afternoon and evening.
Serious crimes are most likely to occur on Friday and least likely to occur on Sunday.
Serious crimes are most likely to occur in the summer and early fall, and least likely to occur in the winter (with the exeption of January, which has a crime rate more similar to the summer).
There is no obvious connection between major holidays and crime rates.
Serious crimes are most common in the city center, especially districts A1 and D4.
This EDA just scratches the surface of the dataset. Further analyses could explore how different types of crimes vary in time and space. I didn't even consider the less serious UCR Part Two and Part Three crimes, which are far more common than Part One crimes, but include interesting categories such as drug crimes. Another interesting direction would be to combine this with other data about Boston, such as demography or even the weather, to investigate what factors predict crime rates across time and space.