0

I would like to plot a world map where the U.S. is divided into two regions: blue_states <- tolower(c("California", "Illinois", "New York", "New Jersey", "Washington", "Massachusetts", "Oregon", "Connecticut", "Delaware", "Rhode Island", "District of Columbia", "Vermont")) and non_blue_states.

Crucially, I don't want to display U.S. states' boundaries (except the boundaries between the two regions).

I have a dataframe with two columns: country and value, with values between -1 and +1. The map should be colored according to value. The variable country spans all countries except the U.S., which is covered by country = Blue USA and Non-Blue USA.

I have found several related questions, but I am not able to make them work. For example, this one uses the package USAboundaries, which has been removed from CRAN, and this one plots only the U.S., not the whole world.

Ideally, I would like to use the map ggplot2::map_data(map = "state") for the U.S. states and merge it with ggplot2::map_data(map = "world") for the other countries. But I am obviously open to other solutions.

bixiou
  • 124
  • 2
  • 10

1 Answers1

1

You can do this using rnaturalearth and tidyverse:

library(rnaturalearth)
library(tidyverse)

blue_states <- c("California", "Illinois", "New York", "New Jersey", 
                 "Washington", "Massachusetts", "Oregon", "Connecticut", 
                 "Delaware", "Rhode Island", "District of Columbia", 
                 "Vermont")

ne_states(country = 'United States of America', returnclass = 'sf') %>%
  mutate(is_blue = name %in% blue_states) %>%
  group_by(is_blue) %>%
  summarize(geometry = st_union(geometry)) %>%
  ggplot() +
  geom_sf(aes(fill = is_blue), color = NA) +
  scale_fill_manual(values = c('red3', 'blue2')) +
  coord_sf(xlim = c(-180, -60)) +
  theme_void() +
  theme(legend.position = 'none') 

enter image description here

Or, if you just want the continental USA, you could change the coord_sf to coord_sf(xlim = c(-125, -60), ylim = c(20, 50)) to get:

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • I fail installing `rnaturalearthhires`. I have R 4.3.1. – bixiou Aug 01 '23 at 10:07
  • What code did you try to install it @bixiou? – Allan Cameron Aug 01 '23 at 10:43
  • Different things: `install.packages("rnaturalearthhires")`, `remotes::install_github("ropensci/rnaturalearthhires")`, `devtools::install_github("ropensci/rnaturalearthhires")`, `install.packages("rnaturalearthhires", repos = "https://ropensci.r-universe.dev", type = "source")`. Every time I enter an infinite loop. – bixiou Aug 01 '23 at 14:40
  • It worked well on another computer! Before I can validate the answer, @allan-cameron would you mind explainind how you then merge this map with a world map, where the U.S. is split into two countries (blue and non-blue)? – bixiou Aug 03 '23 at 08:13
  • I managed to install it on the first computer as well, with `devtools::install_github("ropensci/rnaturalearthhires")` using the terminal instead of RStudio. – bixiou Aug 03 '23 at 09:14
  • I didn't manage to merge this U.S. map with a world map. @allan-cameron, how would you do? – bixiou Aug 17 '23 at 17:19