how to extract the US States using lat-lon and merge them?

I have a few coordinate points which I would like to extract from the respective US States.
For example, A (83.5 W, 32.8 N) and B (81.6W, 28.3 N) are in Georgia and Florida respectively.
Finally, I want a map that shows only Georgia and Florida.

Thanks

Here's one way which uses the sf, tidyverse, and tidycensus packages.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
options(tigris_class = "sf")

point_tib <- tibble(
  Points=c("A", "B"),
  longitude=c(-83.5, -81.6),
  latitude=c(32.8, 28.3)
)

point_sf = st_as_sf(point_tib, coords = c("longitude", "latitude"), 
                    crs = 4326, agr = "constant")

states_sf <- states()


point_sf <- st_transform(point_sf, st_crs(states_sf))

states_subset_idx <-  st_contains(states_sf, point_sf) %>% as.data.frame() %>% pull(row.id)

states_subset <- states_sf %>%
  slice(states_subset_idx)

ggplot() +
  geom_sf(aes(geometry=geometry), data=states_subset) +
  geom_sf(aes(geometry=geometry), data=point_sf) 

Created on 2021-11-03 by the reprex package (v2.0.1)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.