Get startedGet started for free

Geospatial maps with sf

1. Geospatial maps with sf

Let's create some geospatial visualizations.

2. An introduction to sf

Let's use the sf library to create geospatial maps. This is short for "simple features". Importantly, a sf geospatial visualization is created in layers, like layers of masterful brush strokes on an initially blank canvas. As this is not a course on geospatial visualizations, we shall not delve too deeply into the subject. The key message is that a geospatial visualization is made up of layers.

3. Importing library and geospatial data

Let's first import the sf and tidyverse libraries. Let's further import additional data from keyhole markup language, or KML files, using st_read and st_as_sf. These will be stored as sf objects. This first is london_poly, which contains the geospatial boundaries of London. In this code, we set the drivers to KML to be consistent with the dot-kml extension of the file, and also set quiet to TRUE to suppress summary messages. Try setting quiet to FALSE later! Let's store a circular path around London as london_loop, and another circular path linking parts of inner London, which we call london_capital. Finally, get the coordinates of Airbnb listings and call it listings_geo.

4. Different types of sf objects: MULTIPOLYGON

There are different types of sf objects. We can check the geometry type of a sf object by checking its geometry attribute using the $ notation. For instance, london_poly contains 33 features, and is a multipolygon, which means that each feature is a polygon. The boundaries of each of these 33 regions can be described by a polygon. The rest of the output shows additional feature details.

5. Different types of sf objects: LINESTRING

london_loop contains one feature, and is a linestring which traces out a path formed by a string of lines. This is the same for london_capital. Remember, each of these two sf objects traces a route, and can be described using several connected lines.

6. Different types of sf objects: POINT

listings_geo contains 69351 features, and where each feature is a point. Each listing can be located by placing a point on the map.

7. Plotting polygons using plot()

We can plot the boundaries by using st_geometry which creates polygons based on geospatial boundaries. The plot function quickly visualizes each boundary. Let's enable the axes and change the border color. We can fill each polygon with a different color by setting the col argument.

8. Plotting lines using plot()

Next, let's add lines that depict the route in london_loop. This is also achieved using st_geometry, where we must set add to TRUE to stack it over the existing plot. Let's change its color to blue, and do the same for london_capital, giving it a different color.

9. Plotting points using plot()

We can add points. This is done so by placing listings_geo in st_geometry. Remember also to set add to TRUE. As we can see, this map is not readable at this point, because much of it has been obscured by the absurd number of points added.

10. Plotting polygons using ggplot()

Alternatively, we can use ggplot to plot layers of sf. Let's demonstrate this with two layers. First, we shall obtain the number of listings in each region, which is stored as num_listings. The dataframe is converted to a sf object via st_as_sf. Since each layer may be aligned to a different coordinate reference system, or CRS, we need to match up the CRS for the two layers using st_crs. We shall set the coordinate system for num_listings to be that of london_poly. Now we can plot the polygons in london_poly using geom_sf. Using num_listings, we shall add labels using geom_sf_label, so that it shows the total number of listings in each region. In doing so, we have labeled polygons which depict the number of listings in each region.

11. Let's practice!

Let's practice creating geospatial maps using sf!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.