Jun
January 23, 2016
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).
You can chosse to view the crime density map for each crime categories all together or individually. You can also specify the year range. Due to the calling from map api, you may experiencing some delay.
In the second tab, you can see the count of crimes for each category year by year.
In the third tab, a table is presented and you can sort, select and etc.
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))
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')