Overview

Column

Location

Column

Crime Description Word Count & Composition

               word  freq
theft         theft 42099
from           from 29090
grand         grand 26391
auto           auto 24372
locked       locked 21954
property   property 16272
petty         petty 13469
malicious malicious  8478
mischief, mischief,  8478
stolen       stolen  7968

Crime Description Word Cloud

Drunkeness & Prostitution Hotspots

---
title: "Crime in SF: Word Discription Analysis"
author: 'AAYUSH SETHI'
output: 
  flexdashboard::flex_dashboard:
    source_code: embed
    vertical_layout: scroll
    theme: yeti
    orientation: column

---

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

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse) # metapackage with lots of helpful functions
library(caret)
library(highcharter)
library(ggmap)
library(leaflet)
library(plotly)
library(DT)
library(shiny)
library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")

list.files(path = "../input")
df <- read.csv(file = "~/Desktop/sf.csv")
df2 <- Corpus(VectorSource(df$Descript))
dtm <- TermDocumentMatrix(df2)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
```

Overview
=======================================================================

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

### Location
```{r, fig.height=10}
leaflet(df %>% select(latitude,PdDistrict,longitude,Resolution)) %>%
  setView(lng = -122.409981, lat = 37.809326,zoom = 10) %>%
   addTiles() %>% 
  addMarkers(
  clusterOptions = markerClusterOptions())
```

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

### Crime Description Word Count & Composition
```{r,fig.height=4}
head(d, 10)

```


### Crime Description Word Cloud
```{r,fig.height=4}
set.seed(1234)
wordcloud(words = d$word, freq = d$freq, min.freq = 1,
          max.words=200, random.order=FALSE, rot.per=0.35, 
          colors=brewer.pal(8, "Dark2"))

```






Drunkeness & Prostitution Hotspots
=======================================================================
```{r, fig.height=10}
library(RColorBrewer)
library(leaflet)
library(dplyr)


df.map <- function(categories, n) {

    new.df <- filter(df, Category %in% categories) %>% droplevels() 
    
    pal <- colorFactor(brewer.pal(length(unique(new.df$Category)), "Set1"),
                       domain = new.df$Category)

    leaflet(new.df[1:n,]) %>% 
        addProviderTiles("CartoDB.Positron") %>% 
        addCircleMarkers (lng =  ~longitude, lat =  ~latitude, 
                          color = ~pal(Category),
                          opacity = .7, radius  = 1) %>%  
        addLegend(pal = pal, values = new.df$Category)
}

df.map(c("SEX OFFENSES FORCIBLE", "PROSTITUTION","DRUNKENNESS"), n = 1000)
```