Get startedGet started for free

sf plots with plot()

As you have seen, sf objects can be plotted using the base R plot() function. The first plot will allow you to set up the base for the map, and subsequent layers can be added using plot().

In the following, there are four sf objects stored as:

  • london_poly
  • london_loop
  • london_capital
  • num_listings

Complete the code below to obtain a geospatial map using plot() to piece sf objects together. Use st_geometry() to ensure that all objects are indeed sf objects when plotted.

The sf and tidyverse libraries have been loaded.

This exercise is part of the course

Building Dashboards with shinydashboard

View Course

Exercise instructions

  • Plot the first layer which is a multipolygon called london_poly.
  • Plot london_loop and color it purple.
  • Plot london_capital and color it green.
  • Add red markers for each point in num_listings.

Hands-on interactive exercise

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

# Plot the first layer which is a multipolygon
___(___(london_poly), 
     col = sf.colors(length(london_poly$Name), 
                     categorical = TRUE), 
     axes=TRUE, border=grey(0.2, 0.5))
# Plot london_loop and color it purple
___(___(london_loop), add=___, col=___)
# Plot london_capital and color it green
___
# Add red markers for each point in num_listings
___
Edit and Run Code