I am attempting to outline a specific grouping of census tracts over entire US states.
This is the code I am attempting to run:
sf_states <- sf::st_as_sf(fifty_states, coords = c("long", "lat")) %>%
group_by(id, piece) %>%
summarize(do_union = FALSE) %>%
st_cast("POLYGON") %>%
ungroup()
illinois <- sf_states %>%
filter(id == "illinois")
arlington.crs <- arlington.test %>%
st_set_crs(4326)
ggplot() +
theme_minimal() +
geom_sf(data = illinois) +
geom_sf(data = arlington.crs, col = "green", alpha = 0, size = 2)
The arlington.test
data is coming from the tidycensus
package and includes the associated geometry for each census tract.
However, when I run the code, I get the following error:
Error in st_transform.sfc(st_geometry(x), crs, ...) :
cannot transform sfc object with missing crs
I tried dozens of different answers I have come across on StackOverflow and nothing seems to make this work.
What makes it even more strange is that I can run the two geom_sf
functions with ggplot separately, and they plot just fine.
The error only occurs when I attempt to run them together, as shown in the included ggplot
code above.
Any other thoughts/ideas?
Thanks in advance.
Edit (to answer the question below):
> sf::st_crs(arlington.test)
Coordinate Reference System:
User input: NAD83
wkt:
GEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]]
>
Your arlington.test
object seems to be in EPSG:4269; you attempted to override it to WGS 84 via sf::st_set_crs(4326)
. Are you certain you did not mean sf::st_transform(4326)
?
The set crs is best reserved for cases you are certain the CRS is malformed / incorrect (it can happen when the *.prj
gets lost from your *.shp
shapefile). This is not a typical use case for {tidycensus}
data.
To change projection from one known CRS to another sf::st_transform()
works the best.