Get Started

addPolygon() Options

So far we have used the default appearance for addPolygons(). There are several more ways to customize the polygons.

The arguments to addPolygons() we will focus on are:

  • weight: the thickness of the boundary lines in pixels
  • color: the color of the polygons
  • label: the information to appear on hover
  • highlightOptions: options to highlight a polygon on hover
addPolygons(weight = 2,
           color = "red",
           label = ~paste0("Total Income: ", dollar(income)),
           highlight = highlightOptions(weight = 10,
                                       color = "blue",
                                       bringToFront = TRUE))

The high_inc SpatialPolygonsDataFrame you created in the previous exercise has been loaded for you.

This is a part of the course

“Interactive Maps with leaflet in R”

View Course

Exercise instructions

  • Use the arguments of addPolygons() to map the high income zip codes in NC with:
    • A boundary thickness of 1 pixel,
    • Polygons that are colored with the nc_pal palette and are highlighted on hover, and
    • Labels that display the words "Mean Income:" followed by the mean income of the zip code.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# create color palette with colorNumeric()
nc_pal <- colorNumeric("YlGn", domain = high_inc@data$mean_income)

high_inc %>%
  leaflet() %>%
  addTiles() %>%
  # set boundary thickness to 1 and color polygons
  addPolygons(___ = ___, ___ = ~nc_pal(mean_income),
              # add labels that display mean income
              label = ___("Mean Income: ", dollar(mean_income)),
              # highlight polygons on hover
              ___ = highlightOptions(weight = 5, color = "white",
              bringToFront = TRUE))

This exercise is part of the course

Interactive Maps with leaflet in R

BeginnerSkill Level
4.6+
5 reviews

Learn how to produce interactive web maps with ease using leaflet.

In Chapter 4 students will learn to map polygons, which can be used to define geographic regions (e.g., zip codes, states, countries, etc.). Chapter 4 will start by plotting the zip codes in North Carolina that fall in the top quartile of mean family incomes. Students will learn to customize the polygons with color palettes and labels. Chapter 4 will conclude with adding a new layer to the map of every college in America that displays every zip code with a mean income of $200,000 or more during the 2015 tax year. Through the process of mapping zip codes students will learn about spatial data generally, geoJSON data, the @ symbol, and the addPolygons() function. Furthermore, students will have an opportunity to practice applying many of the options that they learned about in the previous chapters, such as popups and labels, as well as new ways to customize their maps, such as the highlight option in addPolygons().

Exercise 1: Spatial DataExercise 2: Introduction to Spatial DataExercise 3: Exploring Spatial DataExercise 4: Joining Spatial DataExercise 5: Mapping PolygonsExercise 6: addPolygons() FunctionExercise 7: NC High Income ZipsExercise 8: addPolygon() Options
Exercise 9: Let's do some LoggingExercise 10: Putting it All TogetherExercise 11: Wealthiest Zip Codes in AmericaExercise 12: Final MapExercise 13: Thank you!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free