简体   繁体   中英

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

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.

邮政编码和计数

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-us-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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM