1. Mapping Polygons
Now that we are familiar with spatial polygon data frames, let's get back to making some maps. In this section, we'll be creating choropleth maps that color zip codes based on their total and mean incomes.
2. Plotting Polygons
Similar to how we plotted thousands of colleges with just a few lines of R code, we can map all of the zip codes in North Carolina by piping our spatial data into a chain of leaflet functions. By default this creates a map with blue boundaries that may be too thick for the number of polygons we are plotting. We’ll use a few options to the addPolygons() function to see if we can improve our boundaries.
3. addPolygons()
We'll focus on four arguments to customize the appearance of our polygons: weight, color, label, and highlightOptions. We can use the weight and color arguments to customize the appearance of our polygons. For example, we can change the boundaries to be 1 pixel and the polygon color to be gray. With the label argument, we can create labels just like we did for our circle markers. Lastly, the highlight argument can be used to customize the appearance of a polygon when a user hovers over it.
4. addPolygons()
With just a few arguments, we have created a customized map of North Carolina that highlights zip codes on hover and presents users with the total income in any zip code in the state. Our next step is to color the zip codes.
5. Coloring Numeric Data
There are three common approaches when mapping color palettes to numeric data. First, colorNumeric, which maps continuous data to an interpolated palette. Then colorBin and colorQuantile, which color the numeric data based on a specified number of groups using the cut function and the quantile function, respectively.
6. colorNumeric()
Let's take a closer look at how the colorNumeric() function works. First, we supply the name of the colors that we want to use to create the palette and the domain, which is a vector that describes the possible values that could be mapped to the colors. While creating a palette, we can use the previewColors function to take a look at how our palette will represent sample values from within the domain. For example, we can see how the zip codes with mean incomes from $100,000 to $600,000 will be represented.
7. Choropleth Map
We can apply our palette by using the color argument. Remember, we'll need to use the tilde operator to create a one-sided equation.
8. Choropleth Map
This palette creates a map that is colored by mean income, but is mostly light blue. This is because the data is right-skewed, meaning there are a small number of zip codes with very large mean incomes.
9. Choropleth Example
If we are interested in learning more about the variation in income across zip codes, we can log transform the mean income variable. Log transforming pulls those large values closer to the mean and yields a more symmetrically distributed variable.
10. Logging
As for the map, log transforming the mean income variable increases the variation in the color gradient across zip codes and enables us to better visualize the distribution of average income across the state.
11. Let's practice!
Now it's your turn to plot a few polygons.