library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplot2)
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.3.3
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
## OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
##
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
##
## wind
library(plotly)
library(rgdax)
## Warning: package 'rgdax' was built under R version 4.3.3
## Loading required package: digest
## Loading required package: jsonlite
## Loading required package: RCurl
## Warning: package 'RCurl' was built under R version 4.3.2
## Loading required package: httr
##
## Attaching package: 'httr'
## The following object is masked from 'package:plotly':
##
## config
## Loading required package: plyr
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:plotly':
##
## arrange, mutate, rename, summarise
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
myLocation <- "San Francisco"
mykey = "AIzaSyABAXSsDR-f9qby3LK2o6bh0BypFWlKrm4"
register_google(key = mykey)
myMap <- get_map(location = myLocation, zoom = 12)
## ℹ <https://maps.googleapis.com/maps/api/staticmap?center=San%20Francisco&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx-f9qby3LK2o6bh0BypFWlKrm4>
## ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=San+Francisco&key=xxx-f9qby3LK2o6bh0BypFWlKrm4>
crime_data <- read_csv("violent_crimes.csv")
## Rows: 878049 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): Category, Descript, DayOfWeek, PdDistrict, Resolution, Address
## dbl (2): X, Y
## dttm (1): Dates
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
crime_data2 <- crime_data
crime_data <- subset(crime_data, Category %in% c("FRAUD", "BURGLARY", "DRUG/NARCOTIC"))
crime_data
## # A tibble: 107,405 × 9
## Dates Category Descript DayOfWeek PdDistrict Resolution Address
## <dttm> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 2015-05-13 19:52:00 BURGLARY BURGLAR… Wednesday PARK ARREST, B… 1500 B…
## 2 2015-05-13 18:30:00 BURGLARY BURGLAR… Wednesday BAYVIEW NONE 1300 B…
## 3 2015-05-13 18:00:00 BURGLARY BURGLAR… Wednesday SOUTHERN NONE 0 Bloc…
## 4 2015-05-13 17:55:00 BURGLARY BURGLAR… Wednesday SOUTHERN ARREST, B… 1200 B…
## 5 2015-05-13 17:47:00 DRUG/NA… POSSESS… Wednesday BAYVIEW NONE 0 Bloc…
## 6 2015-05-13 17:20:00 BURGLARY BURGLAR… Wednesday CENTRAL NONE 600 Bl…
## 7 2015-05-13 17:12:00 DRUG/NA… POSSESS… Wednesday SOUTHERN ARREST, B… 6TH ST…
## 8 2015-05-13 17:00:00 BURGLARY BURGLAR… Wednesday NORTHERN NONE 500 Bl…
## 9 2015-05-13 14:45:00 BURGLARY BURGLAR… Wednesday SOUTHERN NONE 800 Bl…
## 10 2015-05-13 14:31:00 DRUG/NA… POSSESS… Wednesday PARK ARREST, B… 1600 B…
## # ℹ 107,395 more rows
## # ℹ 2 more variables: X <dbl>, Y <dbl>
# Distribución de crimenes en San Francisico
ggmap(myMap) + geom_point(data=crime_data, aes(x = X, y = Y))
## Warning: Removed 1 rows containing missing values (`geom_point()`).
Notemos que el registro de crímenes está distribuido uniformemente en San Francisco, como se puede observar en el mapa.
crime_data <- crime_data %>%
mutate(
YEAR = year(ymd_hms(Dates))
)
crime_data <- mutate(crime_data,
THREEYEARS = ifelse(YEAR %in% 2003:2006, "2003-2006",
ifelse(YEAR %in% 2007:2010, "2007-2010",
ifelse(YEAR %in% 2011:2014, "2011-2014",
ifelse(YEAR %in% 2015, "2015", "-99")))))
last_plot <- ggmap(myMap) +
geom_point(data=crime_data, aes(x = X, y = Y, colour= THREEYEARS, alpha = 0.2))
gg <- ggplot_build(last_plot)$plot
plotly_plot <- ggplotly(gg)
plotly_plot