SF Crime Map

Jun

January 23, 2016

Data set

The data is taken from Kaggle SF Crime Challenge and processed by the following code.

crime <- fread("train.csv", stringsAsFactors = F)
#crime$month <- months(as.Date(crime$Dates))
#crime$year <- year(as.Date(crime$Dates))
#crime$day <- mday(as.Date(crime$Dates))
crime$Category <- tolower(crime$Category)

crime <- crime %>%
  filter(Category %in% c("assault", "kidnapping", 
  "robbery", "sex offenses forcible")) %>%
  mutate(year = year(as.Date(Dates))) %>%
  select(year, Category, X, Y)

Due to the size of the original data, I chose to only use those for serious violent crimes (assualt, kidnapping, robbery and sex offenses forcible).

App guide

Generate a density map

crime <- fread("crime.csv", stringsAsFactors = F)
crime.dt <- filter(crime, year = 2015, Category %in% 'kidnapping')
map <- get_map(location = 'san francisco', zoom = 13, source = "osm")
ggmap(map, extent = "device") + 
      stat_density2d(data=crime.dt, aes(x=X, y=Y, fill = ..level.., alpha = ..level..), 
                     size = 0.05, geom = "polygon") + scale_alpha_continuous(guide='none') +
      scale_fill_gradient('Violent Crime\nDensity', low = "black", high = "red")+
      theme(legend.key = element_blank(), legend.position=c(0.08, 0.5)) 

Line plot

crime.sum <- crime %>% group_by(Category, year) %>% summarise(count = n())
ggplot(data = crime.sum) + 
      geom_line(aes(x= year, y = count, color = Category), size = 2) + theme_bw(18) +
      theme(legend.key = element_blank(), legend.position=c(0.2, 0.5)) +
      xlab('Year') + ylab('count') + ggtitle('SF Violent Crime by Year')