0

I am trying to create a custom sales region map for my company. We have custom regions not based on states but rather a collection of zip codes. How do I create custom sales regions in US using zipcodes in R. My zip codes look something like the below. I want each sales region to have a different color to differentiate the regions.

I have the map of US with the below:

map_us <- tm_shape(us_states) +
  tm_fill() + tm_layout(frame = FALSE)

How do I layer the zip information on this map to color code each region?

enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data and all necessary code. It's unclear what exactly you're trying to do—you describe creating custom regions (sounds like aggregating), but then ask how to add zip code-based data onto a map. Which is it? And how are you trying to group them? – camille Jan 12 '20 at 00:57
  • Sorry if I wasnt clear. I have a file with a list of zip codes and and what regions they belong to. I want the US map to be color coded by region (each region having a different color based on my zip code mapping). I have uploaded a sample of what my data looks like. I am fairly new to R. Thanks in advance for your patience. – Freespirit Jan 12 '20 at 01:19
  • It is not that you were not clear, it was that typically we help people debug either code that is not working, or conceptual issues where the code completes but is not delivering what is wanted. Your questions is more of a write code for me question which is not why people are here.But you need to think through creating some kind of shape or markers based on lists of Zip Codes, you can create another region column and classify the zips appropriately. But then you need to create either polygons housing your regions, or use the lat lon points you and apply color to your points by region category – sconfluentus Jan 12 '20 at 01:46
  • The link I posted has a bunch of suggestions on how to include data in your post. If we had your code related to mapping zip codes, we wouldn't be able to run it on a picture of a spreadsheet anyway – camille Jan 12 '20 at 04:13

1 Answers1

0

This is certainly do-able, but without a decent data set I had to make some up as I went.

library(tigris)
library(tidyverse)
library(sf)

# get US zipcode shapefile
# This takes a while to download
zip <- zctas(cb = TRUE)

# Transform to an sf object, and change projection
zip_sf <- st_as_sf(zip) %>%
  filter(stringr::str_starts(ZCTA5CE10, '3')) %>% ## returns some of southeast US
  st_transform(5070) %>%
  st_simplify() %>%
  st_transform(4326)

# Wholly made-up points, bufferd by 150km
regions <- tribble(
  ~region, ~lon, ~lat,
  'one', -88, 32,
  'two', -82, 28,
  'three', -86, 34,
  'four', -82, 31
) %>% st_as_sf(coords = c('lon', 'lat')) %>%
  st_set_crs(4326) %>%
  st_transform(5070) %>%
  st_buffer(150000) %>%
  st_transform(4326)

# Here the data is joined spatially.
# Your data should be joined (probably with a left-join)
# using the zip code columns.
joined <- st_join(zip_sf, regions, join = st_within)

ggplot(joined) + 
  geom_sf(aes(fill = region), size = .1) + 
  scale_fill_discrete(na.value = 'white')


I used ggplot2 for the plot, but tmap, mapview, etc should work just as well.

enter image description here

mrhellmann
  • 5,069
  • 11
  • 38