1. Learn
  2. /
  3. Courses
  4. /
  5. Visualizing Geospatial Data in R

Exercise

Drawing polygons

A choropleth map describes a map where polygons are colored according to some variable. In the ward_sales data frame, you have information on the house sales summarised to the ward level. Your goal is to create a map where each ward is colored by one of your summaries: the number of sales or the average sales price.

In the data frame, each row describes one point on the boundary of a ward. The lon and lat variables describe its location and ward describes which ward it belongs to, but what are group and order?

Remember the two tricky things about polygons? An area may be described by more than one polygon and order matters. group is an identifier for a single polygon, but a ward may be composed of more than one polygon, so you would see more than one value of group for such a ward. order describes the order in which the points should be drawn to create the correct shapes.

In ggplot2, polygons are drawn with geom_polygon(). Each row of your data is one point on the boundary and points are joined up in the order in which they appear in the data frame. You specify which variables describe position using the x and y aesthetics and which points belong to a single polygon using the group aesthetic.

This is a little tricky, so before you make your desired plot, let's explore this a little more.

Instructions 1/4

undefined XP
  • 1

    The ward_sales data frame is loaded in your workspace. You may want to take a look with head(ward_sales).

    • Add a geom_point() layer with the color aesthetic mapped to ward. How many wards are in Corvallis?
  • 2
    • Add a geom_point() layer with the color aesthetic mapped to group. Can you see some wards that are described by more than one polygon?
  • 3
    • Add a geom_path() layer with the group aesthetic mapped to group. See how points in the same group are joined.
  • 4
    • Finally, add a geom_polygon() layer with the fill aesthetic mapped to ward and the group aesthetic mapped to group.