I want to plot a world map with multiple points aka combinations of latitude and longitude coordinates.
I don't want to use Mercator, therefore I re-project both, the data for the world map and my coordinates.
While the projection for the world changes, all points are suddenly placed in the middle of the map (a common behavior, when the projections don't align, see https://www.earthdatascience.org/courses/earth-analytics/spatial-data-r/intro-to-coordinate-reference-systems/).
What am I doing wrong in when assigning the projection to the points?
My code:
library(ggplot2)
library(sf)
library(rnaturalearth)
# assign a projection, for example ...
crs <- 3035
# get data for the world map and assign the projection
world <- ne_countries(scale = "medium", returnclass = "sf")
world <- st_transform(world, crs = crs)
# create data frame with three points, convert it to a spatial object
# and assign the same projection
points <- data.frame(longitude = c(-105.2519, 10.7500, 2.9833),
latitude = c(40.0274, 59.9500, 39.6167))
points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = crs)
# plot the data with ggplot2:
ggplot() +
geom_sf(data = world) +
geom_sf(data = points, color = "red")
The result:
It does work, however, when I use the standard projection WGS84, i.e. crs = 4326
):
The coordinates of your points
dataframe were defined in terms of lat/lon, which are congruent with EPSG 4326. You should convert it into an sf
object with that specific crs
parameter, before transforming it to other coordinate systems.
Replace this:
points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = crs)
With this:
points <- st_as_sf(points, coords = c("longitude", "latitude"), crs = 4326)
points <- st_transform(points, crs = crs)
And your code should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With