Skip to content

Instantly share code, notes, and snippets.

Created April 26, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/49626dc35fb5850f93cb195bb1b1fc93 to your computer and use it in GitHub Desktop.
Save anonymous/49626dc35fb5850f93cb195bb1b1fc93 to your computer and use it in GitHub Desktop.
---
title: "Denver Police Department Activity Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
##Including all of the libraries necessary for this analysis.
#Make sure the packages are installed before calling the library.
library(flexdashboard)
library(knitr)
library(dplyr)
library(ggplot2)
library(shiny)
library(ggmap)
library(shiny)
library(shinydashboard)
library(lubridate)
```
DPD 911 Dispatches
=====================================
Column {data-width=350}
-----------------------------------------------------------------------
### Dispatch Time of Day by Day of Week
```{r}
#The text above the blue ====== line is the tab label.
#The text to the right to ### is the title of the dashboard componenet.
#There are 3 datasets used in this in this dashboard.
#This is the first dataset stored in my setwd() location.
df911<-readRDS("R 911 Dataset.rds")
#I used ggplot and dplyr to create the visuals on this dashboard.
#The fill is Time.of.Day and the fill is what you are trying to group-by.
df911 %>%
ggplot(aes(x = DOW, fill = Time.of.Day)) +
geom_bar()
```
### Top 5 "Problems" by Time of Day of Dispatches
```{r}
#There were too many "problems" to fit on one visual so I decided to use the top 5 by frequency.
#This first chunck of code uses dyplr to determien what the top 5 problems are.
d2 <- df911 %>%
count(Problem) %>%
top_n(5) %>%
arrange(n, Problem) %>%
mutate(Problem = factor(Problem, levels = unique(Problem)))
#This second chunk of code uses dplyr and ggplot to apply the top 5 problems to create the visual.
df911 %>%
filter(Problem %in% d2$Problem) %>%
mutate(Problem = factor(Problem, levels = levels(d2$Problem))) %>%
ggplot(aes(x = Problem, fill = Time.of.Day)) +
geom_bar()
```
DPD Arrests
=====================================
Column {data-width=350}
-----------------------------------------------------------------------
### Arrest Reasons by Month
```{r}
#The second tab uses a different dataset, so we have to load in this other datset.
#This dataset is also located in my setwd() location.
dfAR <-read.csv("ArrestData.csv", header=TRUE)
g<-ggplot(dfAR,aes(as.factor(MONTH)))
g+geom_bar(aes(fill=X.ARREST_REASON.))
```
### Top 5 Arrest Zones by Month
```{r}
d3 <- dfAR %>%
count(X.ZONE) %>%
top_n(5) %>%
arrange(n, X.ZONE) %>%
mutate(X.ZONE = factor(X.ZONE, levels = unique(X.ZONE)))
dfAR %>%
filter(X.ZONE %in% d3$X.ZONE) %>%
mutate(X.ZONE = factor(X.ZONE, levels = levels(d3$X.ZONE))) %>%
ggplot(aes(x = as.factor(MONTH), fill = X.ZONE)) +
geom_bar()
```
DPD Offenses
=====================================
Column {data-width=350}
-----------------------------------------------------------------------
### Top 5 Offenses by District
```{r}
#The third tab uses a different dataset, so we have to load in this other datset.
#This dataset is also located in my setwd() location.
dfOD<-readRDS("OffensesData.rds")
d4 <- dfOD %>%
count(Offense.Desc) %>%
top_n(5) %>%
arrange(n, Offense.Desc) %>%
mutate(Offense.Desc = factor(Offense.Desc, levels = unique(Offense.Desc)))
dfOD %>%
filter(Offense.Desc %in% d4$Offense.Desc) %>%
mutate(Offense.Desc = factor(Offense.Desc, levels = levels(d4$Offense.Desc))) %>%
ggplot(aes(x = as.factor(District), fill = Offense.Desc)) +
geom_bar()
```
### Offenses in Districts by Day of Week
```{r}
dfOD %>%
ggplot(aes(x = as.factor(District), fill = Occ.Dow)) +
geom_bar()
```
Pot Crimes
=====================================
-----------------------------------------------------------------------
### Top 5 Offense Categories by District Involving MJ
```{r}
#The fourth tab uses a different dataset, so we have to load in this other datset.
#This dataset is also located in my setwd() location.
dfPOT<-read.csv("crime_marijuana.csv")
d5<- dfPOT %>%
count(OFFENSE_CATEGORY_ID) %>%
top_n(5) %>%
arrange(n, OFFENSE_CATEGORY_ID) %>%
mutate(OFFENSE_CATEGORY_ID = factor(OFFENSE_CATEGORY_ID, levels = unique(OFFENSE_CATEGORY_ID)))
dfPOT %>%
filter(OFFENSE_CATEGORY_ID %in% d5$OFFENSE_CATEGORY_ID) %>%
mutate(OFFENSE_CATEGORY_ID = factor(OFFENSE_CATEGORY_ID, levels = levels(d5$OFFENSE_CATEGORY_ID))) %>%
ggplot(aes(x = as.factor(DISTRICT_ID), fill = OFFENSE_CATEGORY_ID)) +
geom_bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment