1. Reading in spatial data
You should now be feeling pretty comfortable with the types of spatial objects available in R, but so far you've been working with objects we've provided. In this chapter you'll learn the skills you need to import your own spatial objects, and how to handle a couple of common problems from using multiple data sources.
2. Median incomes in New York County
Throughout the chapter you'll be building up to this final plot: a plot of median incomes by census tract in Manhattan.
Census tracts are small geographical areas with roughly the same number of people that the US census uses as one level of aggregation in its surveys, including in the American Community Survey, an annual survey that supplements the decennial Census.
There are a couple of spatial objects involved in this plot. There are the census tract polygons that come from the US census, and some larger neighborhood polygons and polygons that describe the areas of water both from the New York City planning office. To get from the data files to this final plot, you'll be walking through the following steps.
3. Procedure
You'll start by learning to read in spatial files so our files containing polygons become spatial objects in R.
You'll discover that these polygons are provided in different coordinate systems and how to transform one to the other.
Then to add the income data to the census tract data, you'll learn how to add data to the data frame inside a Spatial__DataFrame object.
Finally, you'll make some tweaks to the plot to get it into a publication ready form.
4. Reading in a shape file
Let's get started by talking about how to vector data into R. Vector data is data described by points, including lines and polygons and the most common format for vector data is the shape file.
5. Reading in a shape file
The rgdal package provides the function readOGR that reads in many vector formats including shape files.
A shape file is really a collection of files that together describe the data. Let's take a look at the files for the water areas. The files live together in the directory water, inside we can see four files, all called water-areas, but with different extensions. To read this in, we pass readOGR first the directory name, water, and then the file name without the extension water-areas. readOGR prints some messages that lets us know it read something in.
6. Checking the result
The readOGR function can handle many kinds of file input, but it will always output an sp object, in this case, a SpatialPolygonsDataFrame.
You can use what you already know to check on what it read by calling summary and plot on the result.
7. Reading in a raster file
We don't need any raster data for our plot, but you should know how to read it in. rgdal has the readGDAL function for reading in raster file types, but we actually prefer using the raster function in the raster package, because it returns RasterLayer objects.
Here, we've got some raster data from Chapter 4, the population across the US. The data is stored inside the usgrid_data_2000 directory in a GeoTiff file, a common raster file type with the extension dot-tif. Reading in a raster using the raster function is as simple as passing the entire path to the file as the only argument.
8. Checking the result
The result is a RasterLayer object.
9. Let's practice!
Alright, it's your turn to get the data we need into R.