0

I'm using urbnmapr to get a county base map. I want to plot my own points on top of this map. Even though the urbnmapr map plots with lat/long on the x y axis, the lat/long points that I want to plot over it do not. Why is this and how can I fix it?

library(tidyverse)
library(sf)
#devtools::install_github("UrbanInstitute/urbnmapr")
library(urbnmapr)
  
counties_sf <- get_urbn_map(map = "counties", sf = TRUE,) %>%
  subset(state_abbv %in% "SC")
  
points <- tibble(x = c(34.95116, 34.95265, 34.95116, 35.07469), 
                 y = c(-82.79619 , -82.79647, -82.79676, -82.63421)) %>%
  st_as_sf(coords = c("x", "y"), crs = st_crs(counties_sf))

ggplot()+
  geom_sf(data = counties_sf)+
  geom_sf(data = points)

Here the map of SC is in the right position, but the points are not. Examining the counties_sf data, I see their geometry is wacky, while the points geometry is not. I'm very confused...

2 Answers 2

0

I have two suggestions:

  • how hard is the requirement to use urbnmapr? the golden standard of accessing US county data is (arguably) {tigris}. Note that this may not be the real culprit.
  • you seem to have the coords wrong in your points object - lat long vs long lat. This seems a bigger issue than the urbnmapr (which I don't have on hand) vs tigris packages.

Or, because an example is more than 1000 words, consider this piece of code:

library(tidyverse)
library(sf)
library(tigris)


counties_sf <- tigris::counties(state = "SC", resolution = "20m")

points <- tibble(x = c(34.95116, 34.95265, 34.95116, 35.07469), 
                 y = c(-82.79619 , -82.79647, -82.79676, -82.63421)) %>%
  st_as_sf(coords = c("y", "x"), crs = st_crs(counties_sf))

ggplot()+
  geom_sf(data = counties_sf) +
  geom_sf(data = points, pch = 4, color = "red")

enter image description here

1
  • This is great. Thanks. I was wondering why I couldn't find a standard program for US boundaries in SF format. Thanks for pointing me there.
    – Alex Krohn
    Sep 12 at 16:43
0

The coordinate numbers you feed to points are in lat-long, but you then tell sf that these numbers are the same coordinate system as your boundaries.

What you should do is tell sf these are in lat-long (which is normally the EPSG code 4326, unless you know that these locations are not EPSG 4326, aka WGS 84, aka "GPS Coordinates"), and also get x and y the right way round:

points <- st_as_sf(
    data.frame(
      x = c(34.95116, 34.95265, 34.95116, 35.07469), 
      y = c(-82.79619 , -82.79647, -82.79676, -82.63421)
    ), coords=c("y","x"), crs=4326)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.