library(readr)
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.4 ✔ 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(ggplot2)
install.packages("ggmap", repos = "https://cloud.r-project.org")
## package 'ggmap' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Lexib\AppData\Local\Temp\RtmpEXo9ok\downloaded_packages
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.
suicide <- read.csv("C:/Users/Lexib/Downloads/san_francisco_suicide_2003_2017.csv")
View(suicide)
suicide <- as.data.frame(suicide)
suicide <- suicide %>%
mutate(Descript = case_when(
Descript == "SUICIDE" ~ "UNKNOWN SUICIDE",
Descript == "ATTEMPTED SUICIDE" ~ "UNKNOWN ATTEMPTED SUICIDE",
TRUE ~ Descript
))
bbox_coords <- c(left = -122.560899, bottom = 37.676558, right = -122.332927, top = 37.833454)
register_stadiamaps("82f10e38-0d79-4410-bf69-691e4f268e8e")
sf_map <- get_stadiamap(
bbox = bbox_coords,
zoom = 12,
maptype = "stamen_terrain"
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(sf_map)
colnames(suicide)
## [1] "IncidntNum" "Category" "Descript" "DayOfWeek" "Date"
## [6] "Time" "PdDistrict" "Resolution" "Address" "X"
## [11] "Y" "Location" "PdId" "year"
suicide$X <- as.numeric(suicide$X)
suicide$Y <- as.numeric(suicide$Y)
suicide <- suicide[!is.na(suicide$X) & !is.na(suicide$Y), ]
ggmap(sf_map) +
geom_point(
data = suicide,
aes(x = X, y = Y, color = Descript),
size = 2,
alpha = 0.7
) +
labs(
title = "San Francisco Suicide Incidents by Type",
x = "Longitude",
y = "Latitude",
color = "Descript"
) +
theme_minimal() +
scale_color_viridis_d(option = "plasma")
```