Loved by learners at thousands of companies
Where should you buy a house to get the most value for your money? Your first step might be to make a map, but spatial analysis in R can be intimidating because of the complicated objects the data often live in.
This course will introduce you to spatial data by starting with objects you already know about, data frames, before introducing you to the special objects from the sp and raster packages used to represent spatial data for analysis in R. You'll learn to read, explore, and manipulate these objects with the big payoff of being able to use the tmap package to make maps.
By the end of the course you will have made maps of property sales in a small town, populations of the countries of the world, the distribution of people in the North East of the USA, and median income in the neighborhoods of New York City.
Basic mapping with ggplot2 and ggmapFree
We'll dive in by displaying some spatial data -- property sales in a small US town -- using ggplot2 and we'll introduce you to the ggmap package as a quick way to add spatial context to your plots. We'll talk about what makes spatial data special and introduce you to the common types of spatial data we'll be working with throughout the course.Introduction to spatial data50 xpGrabbing a background map100 xpPutting it all together100 xpInsight through aesthetics100 xpUseful get_map() and ggmap() options50 xpDifferent maps100 xpLeveraging ggplot2's strengths100 xpA quick alternative100 xpCommon types of spatial data50 xpDrawing polygons100 xpChoropleth map100 xpRaster data as a heatmap100 xp
Point and polygon data
You can get a long way with spatial data stored in data frames, but it makes life easier if they are stored in special spatial objects. In this chapter we'll introduce you to the spatial object classes provided by the sp package, particularly for point and polygon data. You'll learn how to explore and subset these objects by exploring a world map. The reward for learning about these object classes: we'll show you the package tmap which requires spatial objects as input, but makes creating maps really easy! You'll finish up by making a map of the world's population.Introducing sp objects50 xpLet's take a look at a spatial object100 xpWhat's inside a spatial object?100 xpA more complicated spatial object100 xpsp and S450 xpWalking the hierarchy100 xpFurther down the rabbit hole100 xpMore sp classes and methods50 xpSubsetting by index100 xpAccessing data in sp objects100 xpSubsetting based on data attributes100 xptmap, a package that works with sp objects100 xpIntroduction to tmap50 xpBuilding a plot in layers100 xpWhy is Greenland so big?100 xpSaving a tmap plot100 xp
Raster data and color
While the sp package provides some classes for raster data, the raster package provides more useful classes. You'll be introduced to these classes and their advantages and then learn to display them. The examples continue with the theme of population from Chapter 2, but you'll look at some much finer detail datasets, both spatially and demographically. In the second half of the chapter you'll learn about color -- an essential part of any visual display, but especially important for maps.The raster package50 xpWhat's a raster object?100 xpSome useful methods100 xpA more complicated object100 xpA package that uses Raster objects100 xpColor scales50 xpPick the right palette50 xpAdding a custom continuous color palette to ggplot2 plots100 xpCustom palette in tmap100 xpMore about color scales50 xpAn interval scale example100 xpA diverging scale example100 xpA qualitative example100 xp
Data import and projections
In this chapter you'll follow the creation of a visualization from raw spatial data files to adding a credit to a map. Along the way, you'll learn how to read spatial data into R, more about projections and coordinate reference systems, how to add additional data to a spatial object, and some tips for polishing your maps.Reading in spatial data50 xpReading in a shapefile100 xpReading in a raster file100 xpGetting data using a package100 xpCoordinate reference systems50 xpMerging data from different CRS/projections100 xpConverting from one CRS/projection to another100 xpAdding data to spatial objects50 xpThe wrong way100 xpChecking data will match100 xpMerging data attributes100 xpA first plot100 xpPolishing a map50 xpSubsetting the neighborhoods100 xpAdding neighborhood labels100 xpTidying up the legend and some final tweaks100 xpWrap up50 xp
DatasetsHouse sales in Corvallis, 2015Ward sales in Corvallis, 2015Predicted house prices in CorvallisCountries (sp object)Countries (spdf object)Population around the Boston and NYC areasPopulation around the Boston and NYC areas (Broken into different age groups)Population around the Boston and NYC areas (Proportion by age)MigrationNeighborhood Tabulation AreasMedian Income dataNYC tracts dataWater bodies in NYCNYC Income data
Charlotte WickhamSee More
Assistant Professor at Oregon State University
Charlotte is an Assistant Professor in the Department of Statistics at Oregon State University and an avid R programmer with a passion for teaching. Her interests lie in spatiotemporal data, statistical graphics and computing, and environmental statistics.