有没有办法可以在 usmap R 包中用县地图覆盖州边界?

问题描述 投票:0回答:1

我正在努力明确划分州边界以及县明智的热图。但州边界似乎并不“突出”。我尝试了几个不同的代码块,但都不起作用。 # 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)

                
r ggplot2 maps usmap
1个回答
0
投票
geom_polygon

添加州边界以及

usmap
包提供的州地图数据,您可以使用
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")

© www.soinside.com 2019 - 2024. All rights reserved.