Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Plotting both state AND county boundaries on same map using plot_usmap from usmap package in R

$
0
0

I would like to create a map of the US showing both state and county boundaries (i.e. state boundaries in a different color). I typically do this using either shape files that I import or using ggplot2's map_data function. However, I face three obstacles.

1) I cannot install gdal and geos in my computing environment so that precludes the use of any shape files.

2) I need to include Hawaii and Alaska, so that excludes the use of map_data from ggplot2.

3) I need the map to include both state AND county boundaries, which makes the use of usmap package problematic as its a wrapper function for ggplot2 but without the ease and general ability to customize to the level of a raw ggplot2 object.

What I need: A map that can project Alaska and Hawaii and display state and county boundaries using contrasting colors and I need to accomplish all this without resorting to any packages that rely on rgeos and/or rgdal.

What I've tried thus far:

library(dplyr)
library(stringr)
library(ggplot2)
library(usmap)
library(mapproj)
devtools::install_github("wmurphyrd/fiftystater")
library(fiftystater)

county_data<-read.csv("https://www.ers.usda.gov/webdocs/DataFiles/48747/PovertyEstimates.csv?v=2529") %>% #
  filter(Area_Name != "United States") %>%
  select(FIPStxt, State, Area_Name, PCTPOVALL_2017) %>%
  rename(fips = FIPStxt)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
state_map <- map_data("state")

plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") + 
  geom_map(data = crimes, aes(map_id = state), map = fifty_states, color= "red") + 
  geom_path(data = state_map, aes(x =long , y=lat), color= "red")+
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  theme(legend.position = "none") +
  theme_map() #no go

plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") + 
  geom_map(data = crimes, aes(map_id = state), map = fifty_states, color= "red") + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  theme(legend.position = "none") +
  theme_map() #no go

plot_usmap(data = county_data, values = "PCTPOVALL_2017", color = "white") + 
  geom_map(data = crimes, aes(map_id = state, color= "red"), map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  theme(legend.position = "none") +
  theme_map() #no go

What I suspect is happening is that one layer (the original ggplot code) is projecting an extremely small image vs the other layer which is the entire map (plot_usmap) -I say that bc the resulting map is simply the original plot_usmap with an extremely small red dot that is extremely hard to even identify. See the map below with the black circle highlighting where the red dot is.

enter image description here


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>