How to Plot ZipCodes onto a Map of USA using Counts in R

1.7k views Asked by At

I am attempting to create a map of the US showing the following zipcodes and to color it based on count. I have tried ggmaps and chloroplethr but keep getting errors. I think I need to link the zipcodes to geocodes but have not been able to do so. If anyone has any insights, much appreciated.

Zip Codes and Counts

1

There are 1 answers

0
defuneste On

You should try to provide more reproducible code.

Here:

df_count <- data.frame(zip = as.character(c(94025, 98550, 94303, 94306,
                               94062, 94305, 94301, 94061, 943125)),
                       Count = c(1465, 915, 810, 768, 715, 
                                 711, 678, 630, 584))

Then I googled for this https://catalog.data.gov/dataset/tiger-line-shapefile-2019-2010-nation-u-s-2010-census-5-digit-zip-code-tabulation-area-zcta5-na

# Loading packages
library(sf)
library(dplyr)

# loading shapefile with zip codes
zip_codes <- sf::st_read("tl_2019_us_zcta510/tl_2019_us_zcta510.shp")

# join that only keep data in df_count
zip_with_count <- zip_codes %>%
                        dplyr::right_join(df_count, by = c("ZCTA5CE10" = "zip"))

# quick and dirty plot
plot(zip_with_count["Count"])

I will recommand that your read: https://geocompr.robinlovelace.net/index.html

If you want to do better mapping check either the tmap or the cartography packages