Active Travel in the UK.

Active travel is the defined as moving through space via cycling or walking. As a concept, active travel has become commonplace due to its societal benefits in public health and sustainablity. Public perception of active travel has grown through the COVID-19 pandemic. As a result, there is now a general consesus among researchers, local and national government and industries that active travel is key to building a more sustainable urban environment.

In this RPubs, I outline how you can use the stplanr package to visualise active travel in the UK using 2011 census data.

Prerequisites.

#Install and load the following libraries

#stplanr provides tools for sustainable transport planning for spatial data
if (!require(stplanr)) {
  install.packages("stplanr", repos = "http://cran.us.r-project.org")
  require(stplanr)
}
## Loading required package: stplanr
## Warning: package 'stplanr' was built under R version 3.6.2
#dplyr provides tools sorting and managing data
if (!require(dplyr)) {
  install.packages("dplyr", repos = "http://cran.us.r-project.org")
  require(dplyr)
}
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.6.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#dplyr provides tools to read spatial data
if (!require(sf)) {
  install.packages("sf", repos = "http://cran.us.r-project.org")
  require(sf)
}
## Loading required package: sf
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
#tmap provides tools for visualisation
if (!require(tmap)) {
  install.packages("tmap", repos = "http://cran.us.r-project.org")
  require(tmap)
}
## Loading required package: tmap

Fetch flow data

Once you have the following libraries installed, its time to begin the analysis. Firstly, you need to fetch orgin and destination data for the UK. The stplanr package makes this very easy, with one line of code you can fetch data at the MSOA level.

# Fetch origin and destination data for the england and wales
od_all <- pct::get_od()
## No region provided. Returning national OD data.
## Parsed with column specification:
## cols(
##   `Area of residence` = col_character(),
##   `Area of workplace` = col_character(),
##   `All categories: Method of travel to work` = col_double(),
##   `Work mainly at or from home` = col_double(),
##   `Underground, metro, light rail, tram` = col_double(),
##   Train = col_double(),
##   `Bus, minibus or coach` = col_double(),
##   Taxi = col_double(),
##   `Motorcycle, scooter or moped` = col_double(),
##   `Driving a car or van` = col_double(),
##   `Passenger in a car or van` = col_double(),
##   Bicycle = col_double(),
##   `On foot` = col_double(),
##   `Other method of travel to work` = col_double()
## )
## Parsed with column specification:
## cols(
##   MSOA11CD = col_character(),
##   MSOA11NM = col_character(),
##   BNGEAST = col_double(),
##   BNGNORTH = col_double(),
##   LONGITUDE = col_double(),
##   LATITUDE = col_double()
## )

Create active travel column from data

od_all$Active <- (od_all$bicycle + od_all$foot) /
  od_all$all * 100

Fetch centroids and reproject

Now you have flow data, you need to get the centroids for each MSOA in order to visualise flows to the middle of the geographical area. Once you have these, you can the transform them into a simple feature with a 4326 projection.

# Fetch centroids for england and wales. Then transofrm to 4326 spatial projection.
centroids_all <- pct::get_centroids_ew() %>% sf::st_transform(4326)
## Parsed with column specification:
## cols(
##   MSOA11CD = col_character(),
##   MSOA11NM = col_character(),
##   BNGEAST = col_double(),
##   BNGNORTH = col_double(),
##   LONGITUDE = col_double(),
##   LATITUDE = col_double()
## )

Fetch study area geometry

Next up you want to fetch the geometry of your study area. In this example I focus on Manchester. However, you can easily change this to any region in the UK by querying pct_regions

# Saving greater manchester geometry to global environment
manchester <-
  pct::pct_regions %>% filter(region_name == "greater-manchester")

Filter centroids to study area

You then want to filter the centroids you have against your study area.

# Saving greater manchester geometry to global environment
centroids_manchester <- centroids_all[manchester,]
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
od_manchester <- od_all %>%
  filter(geo_code1 %in% centroids_manchester$msoa11cd) %>%
  filter(geo_code2 %in% centroids_manchester$msoa11cd)

od_manchester <- od_all[od_all$geo_code1 %in% centroids_manchester$msoa11cd &
                          od_all$geo_code2 %in% centroids_manchester$msoa11cd,]

Create desire lines of routes

Desire lines show the connections between origin and destination centroids, they are created easily using the od2line function.

#Create desire lines
desire_lines_manchester <-
  od2line(od_manchester, centroids_manchester)

Filtering results

Some trips aren’t worth showing, not because they aren’t important, but because they will be too thin too see. So by setting a minimum threshold we can ensure all desire lines are visible. Moreover, there you don’t want to show flows where the orgin and destination are same.

#Set min threshold and then filter trips to the freshold
min_trips_threshold <- 20
desire_lines_inter <-
  desire_lines_manchester %>% filter(geo_code1 != geo_code2)
desire_lines_intra <-
  desire_lines_manchester %>% filter(geo_code1 == geo_code2)
desire_lines_top <-
  desire_lines_inter %>% filter(all >= min_trips_threshold)

Plot results

To create an interactive plot set the tmap mode to view, choose a basemap from leaflet:providers and you are done!

tmap_mode("view")
desire_lines_top <- desire_lines_top %>% arrange(Active)
tm_basemap(leaflet::providers$Stamen.TonerLite) +
  tm_shape(manchester) + tm_borders() +
  tm_shape(desire_lines_top) +
  tm_lines(
    palette = "plasma",
    breaks = c(0, 5, 10, 20, 40, 100),
    lwd = "all",
    scale = 9,
    title.lwd = "Number of trips",
    alpha = 0.5,
    col = "Active",
    title = "Active travel (%)",
    legend.lwd.show = FALSE
  ) +
  tm_scale_bar() +
  tm_layout(legend.bg.alpha = 0.5,
            legend.bg.color = "white")
Your plot should look something like this

Your plot should look something like this

Conclusions

In this simple example, I show how easy it can be to visualise flow data. The end map produces a telling representation of Greater Manchesters active transport characterstics and would provide insight for decision makers, businesses and the public. In my next example, I will use a spatial interaction model to show how demand may change in the upcomming census. Thanks for reading, and check out more details about the fantastic stplanr package from Robin Lovelace.