R Tutorial: Introduction to spatial data

Learn to plot geospatial data in R using a 2015 Corvallis house-sales dataset. The tutorial covers converting addresses to latitude and longitude coordinates, plotting points with ggplot2, and adding basemap context with ggmap to create maps that show roads and terrain.
Chapters
Welcome to working with geospatial data in R. Perhaps your first question is what makes data spatial? It's quite simple. Whenever our data is associated with locations, we think about it as spatial. When those locations are on the Earth, we'll often be a little more specific and call the data geospatial. A location is most unambiguously described by a set of coordinates and a description of what coordinate system is being used. You are already familiar with one coordinate reference. Geographical coordinates, the usual latitude longitude pairs we use to specify where something is on the globe. In this chapter, you'll be working with house sales from 2015 where I live, a town called Corvallis. When a house sells, the most obvious data is the price and the address of the house. But there may also be data on how big the house was, how many bedrooms it has, how many bathrooms, how much land, and what condition it is in. All this data is associated with a house at a specific location, so we can consider this spatial data. Although the address specifies the location, it isn't a set of coordinates. To work with these house sales, we need to convert the address to a set of coordinates. Latitude specifies the position north to south. Longitude the position east to west. Often the north, south and east west abbreviations are dropped and instead we use positive numbers for one direction, north for latitude and east for longitude, and negative numbers for the other direction, south for latitude and west for longitude. In math, we are so used to specifying location in X and Y that you'll also often see longitude listed first since it specifies the horizontal or X coordinate. Let's take a look at these house sales as you'll see them in a data frame in R. Each row is a sale. There are latitude and longitude columns that describe the location of the sale, and then all the other columns describe other attributes of that sale, the sale price, number of bedrooms, et cetera. When the data is associated with point locations, that is a single set of spatial coordinates, we describe it as point data. You'll see some other types of spatial data in the last section of this chapter. When spatial data is stored in a data frame, you already have tools to display it. For example, using ggplot2, we can plot all the house cell locations by mapping the X position to longitude and the Y position to latitude. This doesn't look like much of a map because it's missing spatial cues like terrain and roads that help orient us when we're looking at spatial data. In this chapter, we'll use the GGMAP package to add these spatial cues to our plots. The GGMAP package downloads maps from Web services and adds them as a layer in ggplot2plots. Here's an example of getting a map of New York City. We know the location and latitude and longitude. We use the getmap function and to download the map, passing in the location and a zoom parameter. Then we pass the result to the GGMAP function, which displays the map. Over the next few exercises, you'll see how we can combine the GGMAP function with our original ggplot2 map to add some context to our map of house sales. But first you'll play with GGMAP to learn where Corvallis is.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







