\n
Example code, not of photo displayed:
\nlibrary(tidyverse)\nlibrary(ggplot2)\nlibrary(cowplot)\nlibrary(sf)\nlibrary(biscale)\nlibrary(fipio)\n\nall_counties <- tigris::counties(year = 2020, class = "sf", resolution = "20m")\n\nall_counties <- all_counties %>%\n # mutate(geometry = fips_geometry(GEOID)) %>%\n filter(!grepl("^(02|15)", GEOID))\n\n\nall_counties <- bi_class(all_counties, x = ALAND, y = AWATER, style = "quantile", dim = 3) \n\n# create map\nmap <- ggplot() +\n geom_sf(data = all_counties, mapping = aes(fill = bi_class, geometry=geometry), color = "white", size = 1, show.legend = FALSE) +\n bi_scale_fill(pal = "GrPink", dim = 3) +\n bi_theme()\nmap\n\nlegend <- bi_legend(pal = "GrPink",\n dim = 3,\n xlab = "More Land ",\n ylab = "More Water ",\n size = 7)\nfinalPlot <- ggdraw() +\n draw_plot(map, 0, 0, 1, 1) +\n draw_plot(legend, 0.05, .2, 0.2, 0.2)\n\nfinalPlot\n
\n","author":{"@type":"Person","name":"Quinterpret"},"upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"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.\nTake a look at the following:
\nhttps://community.rstudio.com/t/better-ways-to-remove-areas-of-water-from-us-map/60771
\nand
\nRemoving the Great Lakes from US county-level maps in R
\nBut 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:
\nHow to make the Great Lakes the same color as the ocean in R?
\nUsing your exemple I would add:
\n# same projection as the lakes\nall_counties2 <- sf::st_as_sf(all_counties, crs = 4269)\n\n# download lakes geometries\nlakes <- rnaturalearth::ne_download(scale = 110, \n type = 'lakes', \n category = 'physical') %>% \n sf::st_as_sf(lakes110, crs = 4269)\n\n# create map\nmap <- ggplot() +\n geom_sf(data = all_counties2,\n mapping = aes(fill = bi_class, \n geometry = geometry), \n color = "white",\n size = 0.1, \n show.legend = FALSE) +\n geom_sf(data = lakes,\n mapping = aes(geometry = geometry),\n color = "white",\n fill = "white") +\n coord_sf(ylim = c(23, 49),\n xlim = c(-123, -69),\n expand = TRUE) + \n bi_scale_fill(pal = "GrPink",\n dim = 3) +\n bi_theme()\n\nlegend <- bi_legend(pal = "GrPink",\n dim = 3,\n xlab = "More Land ",\n ylab = "More Water ",\n size = 7)\nfinalPlot <- ggdraw() +\n draw_plot(map, 0, 0, 1, 1) +\n draw_plot(legend, 0.05, .2, 0.2, 0.2)\n\nfinalPlot\n
\n\n","author":{"@type":"Person","name":"Arthur Welle"},"upvoteCount":1}}}Reputation: 143
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
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
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
Upvotes: 1