I am trying to have the state boundaries clearly demarcated along with the county wise heat maps. But the state boundaries do not seem prominent. I have tried a couple of different code chunks but both do not work.
# Code chunk1
# Obtain state polygon data for all states
states_sf <- get_urbn_map(map = "states", sf = TRUE)
# Filter state polygon data for specified states
selected_states <- c("AL", "AR", "CT", "DC", "DE", "FL", "GA", "LA",
"MA", "MD", "ME", "MS", "NC", "NH", "NJ", "NY",
"PA", "RI", "SC", "TX", "VA", "VT", "WV")
selected_states_sf <- states_sf[states_sf$ST %in% selected_states, ]
# Assuming Hurricane$total_wind is a numeric variable in Hurricane dataset
plot_usmap("counties", data = Hurricane, values = "total_wind",
include = selected_states,
color = "black"
) +
ggplot2::scale_fill_continuous(low = "snow2", high = "red", guide = FALSE) +
geom_sf(data = selected_states_sf, fill = NA, color = "black", size = 0.5) + # Adjusted size for state borders
theme_minimal() +
# Remove grid lines from plot
coord_sf(datum = NA, crs = st_crs(states_sf)) + # Explicitly set the coordinate system
labs(fill = "Random Data") +
scale_fill_gradient2(low='snow2', high='red') +
theme_bw() +
theme(
# Hide panel borders and remove grid lines
panel.border = element_blank()
)
#Code chunk2
plot_usmap("counties", data = Hurricane, values = "total_wind",
include = c("AL", "AR", "CT", "DC", "DE", "FL", "GA", "LA",
"MA", "MD", "ME", "MS", "NC", "NH", "NJ", "NY",
"PA", "RI", "SC", "TX", "VA", "VT", "WV")
) +
ggplot2::scale_fill_continuous(low = "ghostwhite", high = "red", guide = FALSE)
Update The
usmaphas been modernized in version0.7.0and now returns map data as simple features, i.e.geom_polygonwill no longer work. Instead we have to usegeom_sf:Original answer
One option would be to add the state boundaries using a
geom_polygonand the state map data provided by theusmappackage which you can get usingusmap::us_map.