Creating a Point Map From a CSV File in R
The CSV file in this example is HERE...
# Load the SimpleFeatures library library(sf) # Read the CSV file with latitudes and longitudes data = read.csv("2016-cracker-barrel.csv", as.is=T) # Convert lat/long to simple features points points = st_as_sf(data, coords = c("LONGITUDE", "LATITUDE"), crs = 4326) # Simple x/y plot of the points plot(st_geometry(points), pch=16, col="navy") # Optional save the points to a GeoJSON file for later use st_write(points, dsn="cracker-barrel.geojson")
Since points by themselves usually don't give much context for where they are located, it may be helpful to plot them over an OpenStreetMap base map.
# Load the OpenStreetMap Library library(OpenStreetMap) # Get the outer corners of the points and pad 0.2 degrees bbox = st_bbox(restaurants) padding = 0.2 upperLeft = c(bbox$ymax + padding, bbox$xmin - padding) lowerRight = c(bbox$ymin - padding, bbox$xmax + padding) # Get the base map tiles (this can be slow) base_map = openmap(upperLeft, lowerRight, type="osm") # Plot the base map plot(base_map) # Transform the points to the OSM projection points = st_transform(points, osm()@projargs) # Plot the points on top of the base map plot(st_geometry(points), pch=16, col="navy", cex=2, add=T)