Skip to content

R for Journalists

Unlock the power of R

  • What Is R?
  • R for Rob
  • GitHub
  • Twitter
  • Etsy
  • Home
  • 2020
  • December
  • 11
  • Five more useful spatial functions from the sf package

Five more useful spatial functions from the sf package

Posted on December 11, 2020January 7, 2021 By Rob
Geospatial data, Landmark Atlas, Learn

In my last post I introduced some useful functions from the sf package in R for working with geospatial data. Take a look at it if you haven’t already before looking at these additional ones.

You can find the code on GitHub to practise these functions.

1. st_buffer()

This function is a niche but very useful one. It allows you to draw an expanded polygon around an original.

This is a neat trick to add ‘territorial waters’ around land where it meets the sea. On old maps like the one pictured you often see a dark streak to denote the inner waters surrounding land. You can achieve the same effect with this function.

Photo by Alex Boyd on Unsplash

Here’s an example with the Isle of Wight:

#see part 1 for more instructions on how to read_sf

constituencies <- read_sf('your_working_directory/constituencies')
constituencies_wgs <- st_transform(constituencies, 4326)
iow <- constituencies_wgs[constituencies_wgs$pcon16nm == 'Isle of Wight',]
iow_buffer <- st_buffer(iow, dist = 0.003)
ggplot() + geom_sf(data = iow_buffer, fill = '#D1EDF2') +
geom_sf(data = iow, fill = '#FAF5EF') + theme_void()

2. st_difference()

If two polygons overlap then this function allows you to remove the overlapping areas from two polygons. If you imagine a Venn diagram – the part in the middle where the circles overlap is erased, leaving the parts that don’t overlap.

In our above example iow_buffer is plotted first with iow on top of it. The light purple of iow obscures most of the blue iow_buffer apart from the bit that juts out into the sea. But it is still there, invisible. In our example st_difference() removes it and keeps just the buffer section that doesn’t overlap.

iow_buffer_isolated <- st_difference(iow_buffer, iow)
ggplot() + geom_sf(data = iow_buffer_isolated, fill = '#D1EDF2') + theme_void()

3. st_centroid()

This function finds a centroid in a polygon.

iow_centroid <- st_centroid(iow)
 ggplot() + geom_sf(data = iow, fill = '#FAF5EF') + geom_sf(data = iow_centroid, size = 4) + theme_void()

4. st_distance()

This function finds the distance between two points.

Using this code we can see that the centroid of Portsmouth South is 20.5km away from the centroid of the Isle of Wight.

library(lwgeom)
portsmouth <- constituencies_wgs[constituencies_wgs$pcon16nm == 'Portsmouth South',]
portsmouth_centroid <- st_centroid(portsmouth)
st_distance(portsmouth_centroid, iow_centroid)
Units: [m]
[,1]
[1,] 20538.91

5. st_area()

This function finds the area of a polygon.

   st_area(iow)
   380450744 [m^2] 

This gives an area of 380.4 sq km. This is very similar to the area given on Wikipedia’s page for the Isle of Wight.

That just about covers my use of the sf package so far. I hope you find these functions practical for your work!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

Tags: geospatial mapping sf

Post navigation

❮ Previous Post: The Loudness War on Spotify
Next Post: How to access Open Street Map in R ❯

Recent Posts

  • I’ve moved my blog over to Substack
  • How to plot a large rural area using Ordnance Survey data in R
  • Check the COVID-19 vaccination progress in your area
  • Let R tell you what to watch on Netflix
  • Sentiment analysis of Nineteen-Eighty-Four: how gloomy is George Orwell’s dystopian novel?

Archives

  • April 2022
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016

Categories

  • Geospatial data
  • Landmark Atlas
  • Learn
  • See
  • Seen Elsewhere
  • Site
  • Uncategorized

Copyright © 2025 R for Journalists.

Theme: Oceanly by ScriptsTown

 

Loading Comments...