Creating a choropleth map using USA regions as the boundaries

Good day!

I would like to create a choropleth map with R using USA regions as the boundaries. Here is an example created in PowerPoint:

This is what I could acchieve with qmplot:

library(tidyverse)
library(ggmap)
library(ggrepel)
data <- tibble::tribble(
                  ~lon,       ~lat,
            -98.133208, 11.4326077,
            -89.552784,  11.634501,
           -62.2766186, 44.5090949,
           -65.3279894, 33.1067754,
           -67.7095365, 44.6294348,
            -96.552784,  35.634501,
           -97.3279093, 29.7724417,
           -82.6363869, 28.2949194,
           -80.2061931, 46.0133808,
            -72.014118, 32.4681642,
           -76.2531465, 47.3666368,
           -82.1650991, 46.7758541,
             -5.696645,  11.945587,
           -112.707349, 38.5205043,
           -63.0884036, 52.3930959,
            -87.128901, 39.1242719,
           -65.1626756, 31.3463503,
           -94.3254958, 40.3274999,
             -98.56121, 42.5770056,
          -115.4429944, 46.1502862,
          -117.7901088, 30.6913751,
           -63.7389596, 54.6584068,
          -109.1147095, 24.2156978,
          -119.8340735, 22.6832497,
          -117.8780275, 37.7311394,
           -67.1763467, 38.5861576,
           -96.4427769,   25.14644,
           -78.0814292, 15.5386936,
           -74.4185584, 34.5834425,
           -77.4185584, 36.5834425,
           -79.4185584, 37.5834425,
            -63.121085, 33.9241038,
            -88.121085, 41.9241038,
          -112.7260713,  40.836309,
            -90.552784,  30.634501,
           -109.552784,  12.634501,
           -73.9224329, 48.6153549
          ) 

data <-
  data %>% sample_n(200, replace = TRUE) %>% mutate(
    total = round( runif(nrow(.), min = 100, max = 10000), digits = 0) ,
    lon =  lon + round( runif(nrow(.), min = -10, max = 10), digits = 0),
    lat =  lat + round( runif(nrow(.), min = -10, max = 10), digits = 0)
  ) 

qmplot(x = lon, y = lat, data = data, maptype = "toner-lite", legend = "topleft" , alpha = .3,geom = c("point","density2d"), size = total)+ theme(legend.position = "none")+ 
  geom_label_repel(aes(label=total),
                  box.padding   = 0.35, 
                  point.padding = 0.5,
                  segment.color = 'grey50') 
#> Using zoom = 3...
#> Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Warning: ggrepel: 174 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

Created on 2021-09-16 by the reprex package (v2.0.1)

Any suggestions?

Thank you very much.

André

You have points. You want to join them to the polygons then represent them as the value. Check out the sf package.

1 Like

Hi @williaml , thank you. I think your suggestion got me closer: Mapping by US Census Divisions in ggplot2 Unfortunately, I'm still unable to use the Total in my data to fill the map below.

library(tidyverse)
library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
# Download state data and filter states
sts <- states() #%>%
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |=====================================                                 |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  76%  |                                                                              |======================================================                |  77%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |===============================================================       |  91%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
  #filter(!STUSPS %in% c('HI', 'AK', 'PR', 'GU', 'VI', 'AS', 'MP'))

# Summarize to DIVISION polygons, see sf::st_union
REGION <- sts %>%
  group_by(REGION) %>% 
  summarize()

# Plot it
ggplot() + 
  theme_void() +
  geom_sf(data = sts, 
          aes(fill = as.numeric(REGION)), 
          color = 'white') +
  geom_sf(data = REGION, 
          color = 'black', 
          fill = NA,
          size = 1) +

  scale_fill_viridis_c() +
  coord_sf(crs = 5070) +
  labs(fill = NULL)  

Created on 2021-09-18 by the reprex package (v2.0.1)

It would be something like this. You could put the state borders on top like you did for your first map. You also don't need to group by region.

# spatial points using your data
data_pt <- sf::st_as_sf(data, coords = c("lon", "lat"), crs = 4269) 

# join points to region spatially
# then make non spatial
# summarise to get total
region_counts <- REGION %>% 
  st_transform(4269) %>% 
  st_intersection(data_pt) %>% 
  st_set_geometry(NULL) %>% 
  group_by(REGION) %>% 
  summarise(total = sum(total))

# join counts to region, spatial data
region_data <- left_join(REGION, region_counts) %>% 
  st_transform(5070)

# Plot it
ggplot() + 
  theme_void() +
  geom_sf(data = region_data, 
          aes(fill = total), 
          color = 'white') +
  scale_fill_viridis_c() +
  coord_sf(crs = 5070) +
  labs(fill = NULL)  

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.