Quinterpret
Quinterpret

Reputation: 143

GGplot map stretching counties into lakes

I am trying to make a bivariate US map, but the resulting map seems to stretched counties beyond their borders into lakes, particularly the great lakes region. I've tried both fipio::fips_county() and tigris::counties(year = 2020, class = "sf", resolution = "20m") in order to extract the shapefile/coordinates, both of which output the map like the one displayed. Is there a way to fix this?

Thank you

enter image description here

Example code, not of photo displayed:

library(tidyverse)
library(ggplot2)
library(cowplot)
library(sf)
library(biscale)
library(fipio)

all_counties <- tigris::counties(year = 2020, class = "sf", resolution = "20m")

all_counties <- all_counties %>%
  # mutate(geometry = fips_geometry(GEOID)) %>%
  filter(!grepl("^(02|15)", GEOID))


all_counties <- bi_class(all_counties, x = ALAND, y = AWATER, style = "quantile", dim = 3) 

# create map
map <- ggplot() +
  geom_sf(data = all_counties, mapping = aes(fill = bi_class, geometry=geometry), color = "white", size = 1, show.legend = FALSE) +
  bi_scale_fill(pal = "GrPink", dim = 3) +
  bi_theme()
map

legend <- bi_legend(pal = "GrPink",
                    dim = 3,
                    xlab = "More Land ",
                    ylab = "More Water ",
                    size = 7)
finalPlot <- ggdraw() +
  draw_plot(map, 0, 0, 1, 1) +
  draw_plot(legend, 0.05, .2, 0.2, 0.2)

finalPlot

Upvotes: 1

Views: 292

Answers (1)

Arthur Welle
Arthur Welle

Reputation: 698

The area beneath the water is still part of the country, so they are there, rightfully so. There are a couple of ways of solving this. Take a look at the following:

https://community.rstudio.com/t/better-ways-to-remove-areas-of-water-from-us-map/60771

and

Removing the Great Lakes from US county-level maps in R

But the way I would go about it is to place another layer on top of first one with the geometries of the lakes. As I did here:

How to make the Great Lakes the same color as the ocean in R?

Using your exemple I would add:

# same projection as the lakes
all_counties2 <- sf::st_as_sf(all_counties, crs = 4269)

# download lakes geometries
lakes <- rnaturalearth::ne_download(scale = 110, 
                     type = 'lakes', 
                     category = 'physical') %>% 
      sf::st_as_sf(lakes110, crs = 4269)

# create map
map <- ggplot() +
  geom_sf(data = all_counties2,
          mapping = aes(fill = bi_class, 
                        geometry = geometry), 
          color = "white",
          size = 0.1, 
          show.legend = FALSE) +
  geom_sf(data = lakes,
          mapping = aes(geometry = geometry),
        color = "white",
        fill = "white")  +
  coord_sf(ylim = c(23, 49),
           xlim = c(-123, -69),
           expand = TRUE) +  
  bi_scale_fill(pal = "GrPink",
                dim = 3) +
  bi_theme()

legend <- bi_legend(pal = "GrPink",
                    dim = 3,
                    xlab = "More Land ",
                    ylab = "More Water ",
                    size = 7)
finalPlot <- ggdraw() +
  draw_plot(map, 0, 0, 1, 1) +
  draw_plot(legend, 0.05, .2, 0.2, 0.2)

finalPlot

enter image description here

Upvotes: 1

Related Questions