Reputation: 21
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)
Upvotes: 0
Views: 398
Reputation: 125163
Update The usmap
has been modernized in version 0.7.0
and now returns map data as simple features, i.e. geom_polygon
will no longer work. Instead we have to use geom_sf
:
library(usmap)
library(ggplot2)
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_df <- usmap::us_map(include = selected_states)
plot_usmap("counties",
# data = Hurricane, values = "total_wind",
include = selected_states,
color = "black"
) +
geom_sf(
data = selected_states_df,
color = "blue", fill = NA, linewidth = 1
) +
scale_fill_gradient2(low = "snow2", high = "red") +
theme(
panel.border = element_blank()
) +
labs(fill = "Random Data")
Original answer
One option would be to add the state boundaries using a geom_polygon
and the state map data provided by the usmap
package which you can get using usmap::us_map
.
library(usmap)
library(ggplot2)
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_df <- usmap::us_map(include = selected_states)
plot_usmap("counties",
# data = Hurricane, values = "total_wind",
include = selected_states,
color = "black"
) +
geom_polygon(
data = selected_states_df,
aes(x = x, y = y, group = group),
color = "blue", fill = NA, linewidth = 1
) +
scale_fill_gradient2(low = "snow2", high = "red") +
theme(
panel.border = element_blank()
) +
labs(fill = "Random Data")
Upvotes: 1