How to create a map using zip codes in R?

822 Views Asked by At

I'm trying to make a graph of California based on zip codes because I have some data that are based on them. I've tried excel-geography to plot but they only plotted a portion of zip codes with confidence.

excel map

Recently, I learned a mapping package in R called Tigris, which has been awesome. Even though zip codes aren't technically geography, they still have them listed, but they only 2010 data. Here's what I've done:

zipcodes <- zctas(year = 2010, state = "CA")
zipcodes <- zipcodes %>% filter(ZCTA5CE10>90000 & ZCTA5CE10 <96162) #Range of CA zip codes.
plot(zipcodes$geometry)

But the map seems incomplete using the proper range.

Update: R just produced a better one

I would like to get a map that's similar to the one produced in excel, but with more zip code coverages and aesthetic options. What are some suggestions in mapping using zip codes? I'm pretty new to spatial analysis and also open to learn about other packages if there's any. Thanks!

2

There are 2 best solutions below

2
Allan Cameron On BEST ANSWER

If you select cb = TRUE and year = 2000 you will get complete coverage of the state:

library(tigris)
library(ggplot2)

zipcodes <- zctas(year = 2000, state = "CA", cb = TRUE)

ggplot() +
  geom_sf(data = ca, fill = "gray90") +
  geom_sf(data = zipcodes, aes(fill = AREA), linewidth = 0.1) +
  scale_fill_gradientn(colors = c("red", "cornsilk", "#f0d080"),
                       values = c(0, 0.03, 1), guide = "none") +
  theme_minimal()

enter image description here

0
Till On

As you already mentioned, postal Codes (ZIP) are not a good fit for geo-spatial visualizations.

US Census on ZCTAs: "USPS ZIP Codes are not areal features but a collection of mail delivery routes."

The 2010 ZCTAs will not produce a complete map of California. To make the map look more coherent, you can plot the state's shape first and then the ZCTA shapes layered on top.

Here is an example of how to do that and use the land area of the ZCTAs for the shade of color:

library(tigris)
library(tidyverse)

zipcodes <- zctas(year = 2010, state = "CA", progress_bar = FALSE)

us_states <- states(progress_bar = FALSE)
ca <- us_states |> filter(STUSPS == "CA")

ggplot() +
  geom_sf(data = ca) +
  geom_sf(aes(fill = ALAND10, color = ALAND10), data = zipcodes) +
  theme_void()