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(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
##install.packages("tmap")
library(tmap)
library(crimedata)
data(package = "crimedata")
list_crime_data()
## city years
## 1 All cities 2007 to 2022
## 2 Austin 2007 to 2022
## 3 Boston 2016 to 2022
## 4 Charlotte 2017 to 2022
## 5 Chicago 2007 to 2022
## 6 Colorado Springs 2016 to 2022
## 7 Detroit 2009 to 2022
## 8 Fort Worth 2007 to 2019
## 9 Houston 2019 to 2022
## 10 Kansas City 2009 to 2022
## 11 Los Angeles 2010 to 2022
## 12 Louisville 2007 to 2022
## 13 Memphis 2007 to 2022
## 14 Mesa 2016 to 2022
## 15 Minneapolis 2019 to 2022
## 16 Nashville 2013 to 2022
## 17 New York 2007 to 2022
## 18 San Francisco 2007 to 2022
## 19 Seattle 2008 to 2022
## 20 St Louis 2008 to 2020
## 21 Tucson 2009 to 2020
## 22 Virginia Beach 2013 to 2021
df <- get_crime_data(
years = 2020,
cities = "San Francisco",
type = "sample",
cache = TRUE,
quiet = !interactive(),
output = "tbl"
)
?tmap
head(df)
## # A tibble: 6 × 10
## uid city_name offense_code offense_type offense_group offense_against
## <int> <fct> <fct> <fct> <fct> <fct>
## 1 26160368 San Francisco 90Z all other o… all other of… other
## 2 26160379 San Francisco 280 stolen prop… stolen prope… property
## 3 26160440 San Francisco 23H all other l… larceny/thef… property
## 4 26160468 San Francisco 23C shoplifting larceny/thef… property
## 5 26160519 San Francisco 23D theft from … larceny/thef… property
## 6 26160915 San Francisco 290 destruction… destruction/… property
## # ℹ 4 more variables: date_single <dttm>, longitude <dbl>, latitude <dbl>,
## # census_block <chr>
Note: my crime_data only includes San Francisco data from 2020.
sf_df <- st_as_sf(df,
coords = c("longitude", "latitude"),
crs = 4326)
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
tm_shape(sf_df) +
tm_dots(size = 0.2) +
tm_basemap("CartoDB.Positron")
tmap_mode("view")
## ℹ tmap modes "plot" - "view"
tm_shape(sf_df) +
tm_dots(size = 0.2,
scale = FALSE,
col = "offense_against",
palette = "Dark2") +
tm_basemap("CartoDB.Positron")
##
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_dots()`: migrate the argument(s) related to the scale of the
## visual variable `fill` namely 'palette' (rename to 'values') to fill.scale =
## tm_scale(<HERE>).[v3->v4] `tm_dots()`: use 'fill' for the fill color of polygons/symbols
## (instead of 'col'), and 'col' for the outlines (instead of 'border.col').[v3->v4] `tm_tm_dots()`: migrate the argument(s) related to the scale of the
## visual variable `size` namely 'scale' (rename to 'values.scale') to size.scale
## = tm_scale_continuous(<HERE>).
## ℹ For small multiples, specify a 'tm_scale_' for each multiple, and put them in
## a list: 'size.scale = list(<scale1>, <scale2>, ...)'[cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Dark2" is named
## "brewer.dark2"Multiple palettes called "dark2" found: "brewer.dark2", "hcl.dark2". The first one, "brewer.dark2", is returned.