Read the map:
rm(list = ls())
##
library(sf)
## Warning: package 'sf' was built under R version 4.3.2
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
usaMap=read_sf("gz_2010_us_040_00_500k.json")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
ggplot(data = usaMap)+ geom_sf() +coord_sf(crs = st_crs(2163))
Read in the data
variables=read.csv("data_for_map.csv")
head(variables)
Add Region to map:
head(usaMap)
sort(usaMap$NAME)==sort(variables$State)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Then:
usaMap=merge(usaMap,variables, by.x='NAME', by.y='State')
usaMap
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
usaMap %>%
group_by(Region) %>%
summarise_at(vars(Prop_no_Comp), list(Prop_no_Comp_mean=mean)) ->variablesByRegion
#newMap
variablesByRegion
Plot
ggplot(variablesByRegion) + geom_sf(aes(fill=Prop_no_Comp_mean)) +coord_sf(crs = st_crs(2163))