Skip to content

CSV Files

jlhapp edited this page Sep 7, 2016 · 3 revisions

In order to add xy information from a csv file, you need to have x, y coordinates in the csv file, separating the x and y coordinates into two columns. Import the csv file into R using read.csv("file location and extension"). To view all of the data and column headings, use the print() function. To view the first three rows of data use the head function. For example: head(object name, 3)

There are two ways to map csv files into points on a map: using ggmap or using leaflet.

Using leaflet:

  1. Read the csv file and load the leaflet library.
  2. Use cbind function to combine lat/long column. A new data table will be created.
  3. Create a Spatial Points Data Frame and define the projection. Use SPDF function set the coordinates to the object name created in step 2, set the data to the csv file and proj4string to WGS84.
  4. Use the spTransform function to project to NAD83 UTM17.
  5. Add leaflet and markers.

**Code is available in "csv2leaflet.R" script and you will get a result like this:

csv

Using ggmap to display x,y coordinates on top of a Google map:

  1. Geocode the center of a map. object1 <- geocode("North Carolina")
  2. Map the geocoded point and set zoom level. object2 <- get_map((object1), zoom = 7)
  3. Set bounding box using goeocded map.
  4. Get the Stamenmap using the bounding box.
  5. Use the created map in ggmap and add the UNC table using the "geo_point" expression. You must have (data = your table, aes(x=x column name, y=y column name), color="color")

**Code is on "csv.R" script and you will get a result like this: csv2

Clone this wiki locally