Here are the steps on how to create the maps:
setwd("~/Documents/GIS Shapefiles/OD Drug Deaths Resident Town 2022")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.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.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tmap)
##
## Attaching package: 'tmap'
##
## The following object is masked from 'package:datasets':
##
## rivers
library(tmaptools)
library(ggthemes)
library(readr)
my_data<- read_csv("OD_Drug_Deaths_Resident_Town_DATA_SOURCE.csv")
## Rows: 173 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): GEOID, NAME
## dbl (1): COUNT
##
## ℹ 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(my_data, 5)
## # A tibble: 5 × 3
## GEOID NAME COUNT
## <chr> <chr> <dbl>
## 1 0900560750 Plymouth 3
## 2 0900100000 County subdivisions not defined 0
## 3 0900156060 Norwalk 24
## 4 0900104720 Bethel 2
## 5 0900108070 Bridgeport 92
my_map <- st_read("CT_Towns_Shp.shp")
## Reading layer `CT_Towns_Shp' from data source
## `/Users/ramonrodriguez-santana/Documents/GIS Shapefiles/OD Drug Deaths Resident Town 2022/CT_Towns_Shp.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 169 features and 18 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -73.72777 ymin: 40.95094 xmax: -71.78724 ymax: 42.05051
## Geodetic CRS: GCS_unknown
head(my_map, 5)
## Simple feature collection with 5 features and 18 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -73.47456 ymin: 41.02045 xmax: -72.98325 ymax: 41.7144
## Geodetic CRS: GCS_unknown
## STATEFP COUNTYFP COUSUBFP COUSUBNS GEOID NAME NAMELSAD LSAD
## 1 09 005 60750 00213489 0900560750 Plymouth Plymouth town 43
## 2 09 001 56060 00213481 0900156060 Norwalk Norwalk town 43
## 3 09 001 04720 00213390 0900104720 Bethel Bethel town 43
## 4 09 001 08070 00213396 0900108070 Bridgeport Bridgeport town 43
## 5 09 001 08980 00213399 0900108980 Brookfield Brookfield town 43
## CLASSFP MTFCC CNECTAFP NECTAFP NCTADVFP FUNCSTAT ALAND AWATER
## 1 T1 G4040 790 73450 <NA> A 56650624 1199855
## 2 T5 G4040 720 71950 <NA> C 59275235 34928558
## 3 T1 G4040 720 72850 <NA> A 43918201 156161
## 4 T5 G4040 720 71950 <NA> C 41606698 8705768
## 5 T1 G4040 720 72850 <NA> A 51115717 1541601
## INTPTLAT INTPTLON geometry
## 1 +41.6663915 -073.0265164 MULTIPOLYGON (((-73.06467 4...
## 2 +41.0927388 -073.4197955 MULTIPOLYGON (((-73.47456 4...
## 3 +41.3697777 -073.3895032 MULTIPOLYGON (((-73.43502 4...
## 4 +41.1873933 -073.1957567 MULTIPOLYGON (((-73.24409 4...
## 5 +41.4698845 -073.3936666 MULTIPOLYGON (((-73.44441 4...
my_map<- my_map %>%
st_set_crs(4326)
head(my_map, 5)
## Simple feature collection with 5 features and 18 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -73.47456 ymin: 41.02045 xmax: -72.98325 ymax: 41.7144
## Geodetic CRS: WGS 84
## STATEFP COUNTYFP COUSUBFP COUSUBNS GEOID NAME NAMELSAD LSAD
## 1 09 005 60750 00213489 0900560750 Plymouth Plymouth town 43
## 2 09 001 56060 00213481 0900156060 Norwalk Norwalk town 43
## 3 09 001 04720 00213390 0900104720 Bethel Bethel town 43
## 4 09 001 08070 00213396 0900108070 Bridgeport Bridgeport town 43
## 5 09 001 08980 00213399 0900108980 Brookfield Brookfield town 43
## CLASSFP MTFCC CNECTAFP NECTAFP NCTADVFP FUNCSTAT ALAND AWATER
## 1 T1 G4040 790 73450 <NA> A 56650624 1199855
## 2 T5 G4040 720 71950 <NA> C 59275235 34928558
## 3 T1 G4040 720 72850 <NA> A 43918201 156161
## 4 T5 G4040 720 71950 <NA> C 41606698 8705768
## 5 T1 G4040 720 72850 <NA> A 51115717 1541601
## INTPTLAT INTPTLON geometry
## 1 +41.6663915 -073.0265164 MULTIPOLYGON (((-73.06467 4...
## 2 +41.0927388 -073.4197955 MULTIPOLYGON (((-73.47456 4...
## 3 +41.3697777 -073.3895032 MULTIPOLYGON (((-73.43502 4...
## 4 +41.1873933 -073.1957567 MULTIPOLYGON (((-73.24409 4...
## 5 +41.4698845 -073.3936666 MULTIPOLYGON (((-73.44441 4...
map_and_data<- inner_join(my_map, my_data, by = "GEOID")
head(map_and_data, 5)
## Simple feature collection with 5 features and 20 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -73.47456 ymin: 41.02045 xmax: -72.98325 ymax: 41.7144
## Geodetic CRS: WGS 84
## STATEFP COUNTYFP COUSUBFP COUSUBNS GEOID NAME.x NAMELSAD LSAD
## 1 09 005 60750 00213489 0900560750 Plymouth Plymouth town 43
## 2 09 001 56060 00213481 0900156060 Norwalk Norwalk town 43
## 3 09 001 04720 00213390 0900104720 Bethel Bethel town 43
## 4 09 001 08070 00213396 0900108070 Bridgeport Bridgeport town 43
## 5 09 001 08980 00213399 0900108980 Brookfield Brookfield town 43
## CLASSFP MTFCC CNECTAFP NECTAFP NCTADVFP FUNCSTAT ALAND AWATER
## 1 T1 G4040 790 73450 <NA> A 56650624 1199855
## 2 T5 G4040 720 71950 <NA> C 59275235 34928558
## 3 T1 G4040 720 72850 <NA> A 43918201 156161
## 4 T5 G4040 720 71950 <NA> C 41606698 8705768
## 5 T1 G4040 720 72850 <NA> A 51115717 1541601
## INTPTLAT INTPTLON NAME.y COUNT geometry
## 1 +41.6663915 -073.0265164 Plymouth 3 MULTIPOLYGON (((-73.06467 4...
## 2 +41.0927388 -073.4197955 Norwalk 24 MULTIPOLYGON (((-73.47456 4...
## 3 +41.3697777 -073.3895032 Bethel 2 MULTIPOLYGON (((-73.43502 4...
## 4 +41.1873933 -073.1957567 Bridgeport 92 MULTIPOLYGON (((-73.24409 4...
## 5 +41.4698845 -073.3936666 Brookfield 4 MULTIPOLYGON (((-73.44441 4...
ggplot(map_and_data) +
geom_sf(aes(fill = COUNT)) +
geom_sf_label(aes(label = NAME.x), size = 1) +
ggtitle("Overdose Deaths by Town of Residence, Connecticut, 2022") +
theme(plot.title = element_text(size = 15, face = "bold"),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background = element_blank()) +
scale_fill_gradient(low = "#56B1F7", high = "#132B43")
tmap_mode("plot")
## tmap mode set to 'plot'
tm_shape(map_and_data) +
tm_polygons("COUNT",
palette = "RdYlBu",
title = "OD Deaths") +
tm_style("cobalt") +
tm_text(text = "NAME.x",
size = 0.25) +
tm_layout(title = "Overdose Deaths by Town of Residence, Connecticut, 2022") +
tm_compass(position = c("right", "bottom"), size = 2) +
tm_scalebar(position = c("right", "bottom"), width = 0.15)
## Deprecated tmap v3 code detected. Code translated to v4