library(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.2
## ✔ purrr     1.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggmap)
## ℹ 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(crimedata)
library(hexbin)
library(readr)
san_francisco_suicide_2003_2017 <- read_csv("Downloads/san_francisco_suicide_2003_2017.csv")
## Rows: 1292 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (8): Category, Descript, DayOfWeek, Date, PdDistrict, Resolution, Addre...
## dbl  (5): IncidntNum, X, Y, PdId, year
## time (1): Time
## 
## ℹ 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.
suicide <- as.data.frame(san_francisco_suicide_2003_2017)
head(suicide) 
##   IncidntNum Category                           Descript DayOfWeek       Date
## 1  180318931  SUICIDE ATTEMPTED SUICIDE BY STRANGULATION    Monday 04/30/2018
## 2  180315501  SUICIDE       ATTEMPTED SUICIDE BY JUMPING  Saturday 04/28/2018
## 3  180295674  SUICIDE              SUICIDE BY LACERATION  Saturday 04/21/2018
## 4  180263659  SUICIDE                            SUICIDE   Tuesday 04/10/2018
## 5  180235523  SUICIDE     ATTEMPTED SUICIDE BY INGESTION    Friday 03/30/2018
## 6  180236515  SUICIDE            SUICIDE BY ASPHYXIATION  Thursday 03/29/2018
##       Time PdDistrict Resolution                 Address         X        Y
## 1 06:30:00    TARAVAL       NONE     0 Block of BRUCE AV -122.4517 37.72218
## 2 17:54:00   NORTHERN       NONE   700 Block of HAYES ST -122.4288 37.77620
## 3 12:20:00   RICHMOND       NONE   3700 Block of CLAY ST -122.4546 37.78818
## 4 05:13:00    CENTRAL       NONE     0 Block of DRUMM ST -122.3964 37.79414
## 5 09:15:00    TARAVAL       NONE 0 Block of FAIRFIELD WY -122.4632 37.72679
## 6 17:30:00   RICHMOND       NONE    300 Block of 29TH AV -122.4893 37.78274
##                                         Location         PdId year
## 1  POINT (-122.45168059935614 37.72218061554315) 1.803189e+13 2018
## 2  POINT (-122.42876060987851 37.77620120112792) 1.803155e+13 2018
## 3   POINT (-122.45462091999406 37.7881754224736) 1.802957e+13 2018
## 4  POINT (-122.39642194376758 37.79414474237039) 1.802637e+13 2018
## 5  POINT (-122.46324153155875 37.72679184368551) 1.802355e+13 2018
## 6 POINT (-122.48929119750689 37.782735835121265) 1.802365e+13 2018
names(suicide) 
##  [1] "IncidntNum" "Category"   "Descript"   "DayOfWeek"  "Date"      
##  [6] "Time"       "PdDistrict" "Resolution" "Address"    "X"         
## [11] "Y"          "Location"   "PdId"       "year"
register_stadiamaps(key = "89df51ab-0824-4f4c-b8cb-530ead65054d") 
sf_map <- ggmap( 

get_stadiamap( 

bbox = c(left = -122.60463, 

bottom = 37.640314, 

right = -122.20791, 

top = 37.929668), 

zoom = 11, 

maptype = "stamen_toner_lite" 

) 

)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
sf_map +
  geom_point(aes(x = X, y = Y), 
             data = suicide)

sf_map + 
  geom_point(aes(x = X, y = Y),
             data = suicide,
             color = "lightpink",
             size = 1.5,
             alpha = 0.5)

sf_map + 
  stat_binhex(aes(x = X, y = Y),
             bins = 60,
             data = suicide) + 
  coord_cartesian()
## Coordinate system already present.
## ℹ Adding new coordinate system, which will replace the existing one.

sf_map +
  stat_binhex(aes(x = X, y = Y),
              bins = 60,
              data = suicide) +
  coord_cartesian() +
  scale_fill_gradient(
    "Suicides",
    low  = "#ffeda0",
    high = "#f03b20"
  )
## Coordinate system already present.
## ℹ Adding new coordinate system, which will replace the existing one.