Reputation: 1014
When I am trying to add points layer to a geom_sf()
layer and a projection, the points seem to end up in one location in South Texas. Below is a minimal example of reproducing this issue.
library(sf)
library(ggplot2)
# devtools::install_github("hrbrmstr/albersusa")
library(albersusa)
crs_use = "+proj=laea +lat_0=30 +lon_0=-95"
d_points = data.frame(long = c(-110, -103, -84),
lat = c(45, 40, 41))
A = ggplot(data = usa_sf()) +
geom_sf() +
geom_point(data = d_points,
aes(x = long, y = lat),
color = "red", size = 5) +
theme_minimal() +
ggtitle("(A) right point position, wrong projection")
B = ggplot(data = usa_sf()) +
geom_sf() +
geom_point(data = d_points,
aes(x = long, y = lat),
color = "red", size = 5) +
coord_sf(crs = crs_use) +
theme_minimal() +
ggtitle("(B) right projection, wrong points using geom_point()")
C = ggplot() +
geom_sf(data = usa_sf()) +
geom_sf(data = st_as_sf(d_points,
coords = c("long", "lat"), crs = crs_use),
color = "red", size = 5) +
coord_sf(crs = crs_use) +
theme_minimal() +
ggtitle("(C) right projection, wrong points using geom_sf() points")
cowplot::plot_grid(A, B, C, nrow = 3)
I want to add a points layer to the US map with a custom projection. However, whenever I use a projection, the points that I specify become a weird location in Southeast Texas, which is not in my specified location.
Any suggestion on solving this issue is appreciated. Thank you!
Upvotes: 2
Views: 3355
Reputation: 8749
You need to reproject your points as well; sf::st_transform()
should do the job.
I don't have access to the {albersusa} package, so I am using {USABoundaries} to get a map of the lower 48, but that is not the point; the point is applying st_transform(crs = crs_use)
to both your spatial objects before plotting.
library(sf)
library(ggplot2)
library(USAboundaries)
crs_use <- "+proj=laea +lat_0=30 +lon_0=-95"
usa_sf <- us_boundaries(type="state", resolution = "low") %>%
dplyr::filter(!state_abbr %in% c("PR", "AK", "HI")) %>%
st_transform(crs = crs_use)
d_points <- data.frame(long = c(-110, -103, -84),
lat = c(45, 40, 41)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
st_transform(crs = crs_use)
ggplot(data = usa_sf) +
geom_sf() +
geom_sf(data = d_points,
color = "red", size = 5) +
theme_minimal()
Upvotes: 6