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 or GeoJSON files (my attempts to map county level shape files loaded using fastshp
have not been successful but I'm open to any solution that can reproduce the map below but with state boundaries included).
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.
4) Also, cannot make use of sf
package bc it has a non R library dependency (units
package depends on C library libudunits2
).
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
, rgdal
, and/or units
.
What I've tried thus far plot_usmap
from the usmap
package:
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, Stabr, 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 projected using a different CRS system than the other layer -generated by plot_usmap
. That second layer results in a very small red dot (see circle in map below). Not sure how to re-project without geos/gdal installed. See the map below with the black circle highlighting where the red dot is.
fastshp
package that you were using last time? – Kilogrammetersf
package, which requiresunits
(similar to rgeos and rgdal needs non R libraries)..and unfortunately installing the package from hrbmstr also crashed my R environment... – Yarkandfastshp
with other Shapefiles I haven't been able to map county level boundaries properly (despite proper ordering and specification). – Yarkandparts
? That was the column indicating index for each polygon. Presumably, there should be an index number for each county. If you can identify such information, you need to create group variable for each county in each state as I was doing for you last time. In case you cannot import any county-level polygon data in R, I think you want to contact the package author. – Kilogrammeterlibrary(geojsonsf)
can read it and convert it tosf
, without a dependency onsf
– Saguntoplotly
please, I'll take a look at geojsonsf, thanks! – Yarkandsf
object one needs access to thesf
library (even if just usinggeom_sf
inggplot2
) andsf
depends ongeos
, which I cannot install. – Yarkandlibrary(mapdeck)
can plotsf
objects without depending onlibrary(sf)
, BUT, it's an interactive map. – Sagunto