Joining Points Together in R Using sf and tmap Libraries

Joining Points Together to Create a Single Line and Mapping Output

As a data analyst or geospatial enthusiast, you have likely encountered the need to join points together to create a single line. This can be particularly useful when visualizing geographic data using maps. In this article, we will explore how to achieve this using the sf library in R, which provides an efficient and convenient way to work with spatial data.

Background: Spatial Data and sf

The sf library provides a simple and consistent interface for working with spatial data in R. It is built on top of the sp package, which has been a standard for spatial analysis in R for many years. The sf library provides an object-oriented approach to spatial data, allowing you to easily create, manipulate, and analyze spatial data.

One of the key features of the sf library is its support for the WGS 84 coordinate reference system (CRS). This CRS is a widely used standard for geographic coordinates that is suitable for most applications. The sf library also supports other CRSs, including the British National Grid (GN) and the Mercator projection.

Joining Points Together

To join points together to create a single line, we can use the sf_linestring() function from the sfheaders library, which is specifically designed for this purpose. This function takes a data frame as input, along with the column names of the x and y coordinates.

# Load necessary libraries
library(sfheaders)
library(sf)

# Create an sf object from the given data
dt1 <- data.table(
  code = c("A00111", "A00112","A00113","A00211","A00212","A00213","A00214","A00311","A00312","A00472"),
  x = c(325147,323095,596020,257409,241206,248371,261076,595218,596678,597678),
  y = c(286151,284740,335814,079727,084266,078283,062045,333889,337836,339836),
  point_id = c("P01","P02","P03","P04","P05","P06","P07","P08","P09","P10")
)

sf1 <- sf_linestring(
  obj = dt1,
  x = "x",
  y = "y"
)

Mapping Output

To visualize the resulting line, we can use the tmap library. This library provides a convenient interface for creating maps and visualizing spatial data.

# Load necessary libraries
library(tmap)

# Create a map object using the sf1 object
map <- tm_shape(sf1) + tm_dots()

# Set the map mode to view
tmap_mode("view")

# Display the map
map

The Result

When we run this code, we get an sf object that represents the joined line, along with a map visualization using tmap.

## The sf object
# Simple feature collection with 1 feature and 1 field
# geometry type: LINESTRING
# dimension:      XY
# bbox:           xmin: 241206 ymin: 62045 xmax: 597678 ymax: 339836
# epsg (SRID):    27700
# proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
# id                       geometry
# 1  1 LINESTRING (325147 286151, 

Conclusion

Joining points together to create a single line is a common requirement in spatial data analysis and visualization. The sf library provides an efficient and convenient way to achieve this using the sf_linestring() function. By combining sf with tmap, we can easily visualize the resulting line as part of a larger map.

This article has demonstrated how to use the sfheaders library to join points together to create a single line, along with an example of how to visualize this line using tmap.


Last modified on 2025-03-18