library(readr)
san_francisco_suicide_2003_2017 <- read_csv("C:/Users/sumay/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.
View(san_francisco_suicide_2003_2017) 
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ purrr     1.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ── 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(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
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)
ls() 
## [1] "san_francisco_suicide_2003_2017"
head(san_francisco_suicide_2003_2017) 
## # A tibble: 6 × 14
##   IncidntNum Category Descript       DayOfWeek Date  Time  PdDistrict Resolution
##        <dbl> <chr>    <chr>          <chr>     <chr> <tim> <chr>      <chr>     
## 1  180318931 SUICIDE  ATTEMPTED SUI… Monday    04/3… 06:30 TARAVAL    NONE      
## 2  180315501 SUICIDE  ATTEMPTED SUI… Saturday  04/2… 17:54 NORTHERN   NONE      
## 3  180295674 SUICIDE  SUICIDE BY LA… Saturday  04/2… 12:20 RICHMOND   NONE      
## 4  180263659 SUICIDE  SUICIDE        Tuesday   04/1… 05:13 CENTRAL    NONE      
## 5  180235523 SUICIDE  ATTEMPTED SUI… Friday    03/3… 09:15 TARAVAL    NONE      
## 6  180236515 SUICIDE  SUICIDE BY AS… Thursday  03/2… 17:30 RICHMOND   NONE      
## # ℹ 6 more variables: Address <chr>, X <dbl>, Y <dbl>, Location <chr>,
## #   PdId <dbl>, year <dbl>
names(san_francisco_suicide_2003_2017) 
##  [1] "IncidntNum" "Category"   "Descript"   "DayOfWeek"  "Date"      
##  [6] "Time"       "PdDistrict" "Resolution" "Address"    "X"         
## [11] "Y"          "Location"   "PdId"       "year"
register_stadiamaps(key = "c89a0cb9-0668-4629-a317-7e2e26160a42") 
sf_map <- ggmap(
  get_stadiamap(
    bbox = c(left = -123.1738,
             bottom = 37.6403,
             right = -122.2815,
             top = 37.9297),
    zoom = 11,
    maptype = "stamen_toner_lite"
  )
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
sf_map 

suicide <- san_francisco_suicide_2003_2017
sf_map +
  geom_point(aes(x = X, y = Y),
             data = suicide) 

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

sf_map <- ggmap(
  get_stadiamap(
    bbox = c(left = -122.53,
             bottom = 37.70,
             right = -122.35,
             top = 37.82),
    zoom = 12,
    maptype = "stamen_toner_lite"
  )
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
sf_map 

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

rm(sf_map)
sf_map <- ggmap(
  get_stadiamap(
    bbox = c(left = -122.53,
             bottom = 37.70,
             right = -122.35,
             top = 37.82),
    zoom = 12,
    maptype = "stamen_toner_lite"
  )
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
sf_map

sf_map +
  geom_point(aes(x = X, y = Y),
             data  = suicide,
             color = "forestgreen",
             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.
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_hex()`).

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.
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_hex()`).