Tarea ggmap

Author

Luis Coneo & Lorenzo Gutierrez

Cargue de datos

library(ggmap)
Loading required package: ggplot2
Warning: package 'ggplot2' was built under R version 4.2.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.
library(readr)
Warning: package 'readr' was built under R version 4.2.3
library(lubridate)
Warning: package 'lubridate' was built under R version 4.2.3

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(plotly)
Warning: package 'plotly' was built under R version 4.2.3

Attaching package: 'plotly'
The following object is masked from 'package:ggmap':

    wind
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(dplyr)
Warning: package 'dplyr' was built under R version 4.2.3

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
mykey="AIzaSyDo4PQhLTqYrhmrUqX9T_TMipirveEDc0I"
register_google(key=mykey)

get_googlemap("waco, texas") %>% ggmap()
ℹ <https://maps.googleapis.com/maps/api/staticmap?center=waco,%20texas&zoom=10&size=640x640&scale=2&maptype=terrain&key=xxx>
ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=waco,+texas&key=xxx>

myLocation <- "California"
df<- read_csv("C:/Users/g_a09/Desktop/Universidad/Visualizacion de datos/R/ggmap/RDataSets/RDataSets/violent_crimes.csv", col_names = TRUE)
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.
head(df)
# A tibble: 6 × 9
  Dates               Category  Descript DayOfWeek PdDistrict Resolution Address
  <dttm>              <chr>     <chr>    <chr>     <chr>      <chr>      <chr>  
1 2015-05-13 23:53:00 WARRANTS  WARRANT… Wednesday NORTHERN   ARREST, B… OAK ST…
2 2015-05-13 23:53:00 OTHER OF… TRAFFIC… Wednesday NORTHERN   ARREST, B… OAK ST…
3 2015-05-13 23:33:00 OTHER OF… TRAFFIC… Wednesday NORTHERN   ARREST, B… VANNES…
4 2015-05-13 23:30:00 LARCENY/… GRAND T… Wednesday NORTHERN   NONE       1500 B…
5 2015-05-13 23:30:00 LARCENY/… GRAND T… Wednesday PARK       NONE       100 Bl…
6 2015-05-13 23:30:00 LARCENY/… GRAND T… Wednesday INGLESIDE  NONE       0 Bloc…
# ℹ 2 more variables: X <dbl>, Y <dbl>
unique(df["Category"])
# A tibble: 39 × 1
   Category      
   <chr>         
 1 WARRANTS      
 2 OTHER OFFENSES
 3 LARCENY/THEFT 
 4 VEHICLE THEFT 
 5 VANDALISM     
 6 NON-CRIMINAL  
 7 ROBBERY       
 8 ASSAULT       
 9 WEAPON LAWS   
10 BURGLARY      
# ℹ 29 more rows

Dado que se quiere buscar algun patron para los crimenes violentos, se procede a filtrar el df para la categoria “Assault”

df_new<-df[df$Category=="ASSAULT",]
df_new
# A tibble: 76,876 × 9
   Dates               Category Descript DayOfWeek PdDistrict Resolution Address
   <dttm>              <chr>    <chr>    <chr>     <chr>      <chr>      <chr>  
 1 2015-05-13 21:55:00 ASSAULT  AGGRAVA… Wednesday INGLESIDE  NONE       GODEUS…
 2 2015-05-13 19:33:00 ASSAULT  AGGRAVA… Wednesday BAYVIEW    NONE       23RD S…
 3 2015-05-13 17:47:00 ASSAULT  CHILD A… Wednesday BAYVIEW    NONE       0 Bloc…
 4 2015-05-13 17:40:00 ASSAULT  THREATS… Wednesday CENTRAL    NONE       1400 B…
 5 2015-05-13 15:40:00 ASSAULT  BATTERY… Wednesday PARK       NONE       1700 B…
 6 2015-05-13 15:22:00 ASSAULT  SHOOTIN… Wednesday BAYVIEW    NONE       1200 B…
 7 2015-05-13 14:03:00 ASSAULT  THREATS… Wednesday INGLESIDE  ARREST, B… 29TH S…
 8 2015-05-13 14:00:00 ASSAULT  BATTERY  Wednesday TENDERLOIN NONE       0 Bloc…
 9 2015-05-13 13:58:00 ASSAULT  AGGRAVA… Wednesday CENTRAL    JUVENILE … 300 Bl…
10 2015-05-13 13:00:00 ASSAULT  THREATS… Wednesday SOUTHERN   NONE       500 Bl…
# ℹ 76,866 more rows
# ℹ 2 more variables: X <dbl>, Y <dbl>
myLocation <- "San Francisco, California"
myMap <- get_map(location = myLocation, zoom = 12)
ℹ <https://maps.googleapis.com/maps/api/staticmap?center=San%20Francisco,%20California&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx>
ℹ <https://maps.googleapis.com/maps/api/geocode/json?address=San+Francisco,+California&key=xxx>
ggmap(myMap) + geom_point(data=df_new, aes(x = X, y = Y))
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_point()`).

Se procede a filtrar por año, mes o dia semana, debido a la cantidad de datos, no se logra apreciar bien informacion relevante

df_new$Year<-year(df_new$Dates) 

df_new$Month<-month(df_new$Dates) 

df_new$WeekDay<-wday(df_new$Dates) 
df_new
# A tibble: 76,876 × 12
   Dates               Category Descript DayOfWeek PdDistrict Resolution Address
   <dttm>              <chr>    <chr>    <chr>     <chr>      <chr>      <chr>  
 1 2015-05-13 21:55:00 ASSAULT  AGGRAVA… Wednesday INGLESIDE  NONE       GODEUS…
 2 2015-05-13 19:33:00 ASSAULT  AGGRAVA… Wednesday BAYVIEW    NONE       23RD S…
 3 2015-05-13 17:47:00 ASSAULT  CHILD A… Wednesday BAYVIEW    NONE       0 Bloc…
 4 2015-05-13 17:40:00 ASSAULT  THREATS… Wednesday CENTRAL    NONE       1400 B…
 5 2015-05-13 15:40:00 ASSAULT  BATTERY… Wednesday PARK       NONE       1700 B…
 6 2015-05-13 15:22:00 ASSAULT  SHOOTIN… Wednesday BAYVIEW    NONE       1200 B…
 7 2015-05-13 14:03:00 ASSAULT  THREATS… Wednesday INGLESIDE  ARREST, B… 29TH S…
 8 2015-05-13 14:00:00 ASSAULT  BATTERY  Wednesday TENDERLOIN NONE       0 Bloc…
 9 2015-05-13 13:58:00 ASSAULT  AGGRAVA… Wednesday CENTRAL    JUVENILE … 300 Bl…
10 2015-05-13 13:00:00 ASSAULT  THREATS… Wednesday SOUTHERN   NONE       500 Bl…
# ℹ 76,866 more rows
# ℹ 5 more variables: X <dbl>, Y <dbl>, Year <dbl>, Month <dbl>, WeekDay <dbl>
unique(df_new$Year)
 [1] 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005 2004 2003
df_filtered <- df_new %>%
  filter(Year >= 2004 & Year <= 2009)

gg <- ggmap(myMap, extent = "device") + 
  stat_density2d(data = df_filtered, aes(x = X, y = Y, fill = ..level.., alpha = ..level..),
                 size = 0.01, bins = 16, geom = "polygon") +
  scale_fill_gradient(low = "green", high ="red") + 
  scale_alpha(range = c(0, 0.3), guide = FALSE) + 
  facet_wrap(~Year, ncol = 3)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
  theme(plot.title = element_text(size = 20)) +
  theme(plot.margin = margin(1, 1, 1, 1, "cm"))
List of 2
 $ plot.title :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : num 20
  ..$ hjust        : NULL
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi FALSE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ plot.margin: 'margin' num [1:4] 1cm 1cm 1cm 1cm
  ..- attr(*, "unit")= int 1
 - attr(*, "class")= chr [1:2] "theme" "gg"
 - attr(*, "complete")= logi FALSE
 - attr(*, "validate")= logi TRUE
print(gg)
Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(level)` instead.
ℹ The deprecated feature was likely used in the ggmap package.
  Please report the issue at <https://github.com/dkahle/ggmap/issues>.
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_density2d()`).
Warning: The `guide` argument in `scale_*()` cannot be `FALSE`. This was deprecated in
ggplot2 3.3.4.
ℹ Please use "none" instead.
ℹ The deprecated feature was likely used in the ggmap package.
  Please report the issue at <https://github.com/dkahle/ggmap/issues>.

Se puede observar una concentracion bastante grande en la parte noreste de la ciudad, donde se ve mas presencia de casos violentos en el 2007 y 2008.

df_filtered <- df_new %>%
  filter(Year >= 2010 & Year <= 2015)

gg <- ggmap(myMap, extent = "device") + 
  stat_density2d(data = df_filtered, aes(x = X, y = Y, fill = ..level.., alpha = ..level..),
                 size = 0.01, bins = 16, geom = "polygon") +
  scale_fill_gradient(low = "green", high ="red") + 
  scale_alpha(range = c(0, 0.3), guide = FALSE) + 
  facet_wrap(~Year, ncol = 3)
  theme(plot.title = element_text(size = 20)) +
  theme(plot.margin = margin(1, 1, 1, 1, "cm"))
List of 2
 $ plot.title :List of 11
  ..$ family       : NULL
  ..$ face         : NULL
  ..$ colour       : NULL
  ..$ size         : num 20
  ..$ hjust        : NULL
  ..$ vjust        : NULL
  ..$ angle        : NULL
  ..$ lineheight   : NULL
  ..$ margin       : NULL
  ..$ debug        : NULL
  ..$ inherit.blank: logi FALSE
  ..- attr(*, "class")= chr [1:2] "element_text" "element"
 $ plot.margin: 'margin' num [1:4] 1cm 1cm 1cm 1cm
  ..- attr(*, "unit")= int 1
 - attr(*, "class")= chr [1:2] "theme" "gg"
 - attr(*, "complete")= logi FALSE
 - attr(*, "validate")= logi TRUE
print(gg)

con respecto a las graficas anteriores, se puede observar una “mejora” en cuanto a los crimenes violentos se trata, donde se comienzan a reducir pero nuevamente desde el 2013 y 2014 tienen un aumento significativo para luego disminuir en el 2015, pero lo que comparten todos los años es la localizacion, la mayoria de casos violentos se dan en la zona noreste, por ende seria lo mas recomndable poner a disposicion alta densidad de policias de rapida respuesta para atender los casos de esa zona.