I\'ve got a table with place references and x and y coordinates in a given coordinate reference system. I want to turn that into a simple features data frame. How can I crea
Your attempt and the accepted answers are unnecessarily complicated and terribly confusing. Just go with st_as_sf (which by the way also easily migrates all objects from the outdated sp class (SpatialPolygonsDataFrames and the like)):
df <- data.frame(place = "London",
lat = 51.5074, lon = 0.1278,
population = 8500000) # just to add some value that is plotable
projcrs <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
df <- st_as_sf(x = df,
coords = c("lon", "lat"),
crs = projcrs)
And we are done, as easy as that.
Just to visualise it:
library(tmap)
data("World")
tm_shape(World[World$iso_a3 == "GBR", ]) + tm_polygons("pop_est") +
tm_shape(df) + tm_bubbles("population")
Or with the new amazing geom_sf from ggplot2:
library(ggplot2)
ggplot(World) + geom_sf() + geom_sf(data = df, shape = 4, col = "red", size = 5)