Creating a Point Map From a CSV File in R

The CSV file in this example is HERE...

Cracker Barrel Restaurants in St. Louis, MO
# The R GDAL library contains GDAL functions for loading spatial data
# RGDAL loads the sp library, which contains spatial functions
library(rgdal)

# Read the CSV file with latitudes and longitudes
data = read.csv("2016-cracker-barrel.csv", stringsAsFactors=F)

# Convert the data to a SpatialPointsDataFrame
coords = data.frame(data$LONGITUDE, data$LATITUDE)
wgs84 = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
restaurants = SpatialPointsDataFrame(coords, data, proj4string = wgs84)

# Simple x/y plot of the points
plot(restaurants, pch=16, col="navy")

# Load and plot an OpenStreetMap base map covering the area
upperLeft = c(max(data$LATITUDE) + 0.2, min(data$LONGITUDE) - 0.2)
lowerRight = c(min(data$LATITUDE) - 0.2, max(data$LONGITUDE) + 0.2)

library(OpenStreetMap)
osm = openmap(upperLeft, lowerRight, type="osm")
plot(osm)

# Transform the projection from WGS84 to Spherical Mercator to plot the points
spherical_mercator = CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 
	+k=1.0 +units=m +nadgrids=@null +no_defs")
restaurants = spTransform(restaurants, spherical_mercator)

plot(restaurants, pch=16, col="red", cex=2, add=T)

# Optional save to a shapefile
writeOGR(restaurants, ".", "cracker-barrel", "ESRI Shapefile")

# Optional save to sqlite database file
writeOGR(restaurants, dsn="cracker-barrel.geojson", layer="restaurants", driver="GeoJSON")