Let's do some Logging
Let's look at the quartiles of mean zip code incomes in NC.
summary(high_inc$mean_income)
Min. 1st Qu. Median Mean 3rd Qu. Max.
55947 62380 71655 83682 90695 550849
The range is nearly $500,000! However, the median is much closer to the min than to the max, indicating a right-skew. Since the mean income variable contains exceptionally large values, the continuous color gradient is not very helpful. Log transforming a right-skewed variable pulls large values closer to the mean and yields a more symmetrically distributed variable.
Log transforming the mean income on our map increases the variation in the color gradient across the high income zip codes and enables better visualization of the distribution of mean income across the state.
This exercise is part of the course
Interactive Maps with leaflet in R
Exercise instructions
- Create a logged version of the nc_pal color palette.
- Apply the logged color palette to the
leaflet
map. - Comment out the map tile from the
leaflet
map to more easily visualize the variation in mean income (add # without a space prior to the function that adds the map tile).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use the log function to create a new version of nc_pal
nc_pal <- colorNumeric("YlGn", domain = ___(high_inc@data$mean_income))
# comment out the map tile
high_inc %>%
leaflet() %>%
___addProviderTiles("CartoDB") %>%
# apply the new nc_pal to the map
addPolygons(weight = 1, color = ~nc_pal(___(mean_income)), fillOpacity = 1,
label = ~paste0("Mean Income: ", dollar(mean_income)),
highlightOptions = highlightOptions(weight = 5, color = "white", bringToFront = TRUE))