code/13-desire.R

# Aim: generate tmap figure representing desire lines

# load data if not already loaded:
if (!exists("desire_lines")) {
  library(sf)
  library(dplyr)
  library(spDataLarge)
  library(stplanr)
  library(tmap)     
  zones_attr = bristol_od |> 
    group_by(o) |> 
    summarize_if(is.numeric, sum) |> 
    dplyr::rename(geo_code = o)
  
  zones_joined = left_join(bristol_zones, zones_attr, by = "geo_code")
  
  zones_od = bristol_od |> 
    group_by(d) |> 
    summarize_if(is.numeric, sum) |> 
    select(geo_code = d, all_dest = all) |> 
    inner_join(zones_joined, ., by = "geo_code") |> 
    st_as_sf()
  
  od_top5 = bristol_od |> 
    arrange(desc(all)) |> 
    top_n(5, wt = all)
  
  bristol_od$Active = (bristol_od$bicycle + bristol_od$foot) /
    bristol_od$all * 100
  
  od_intra = filter(bristol_od, o == d)
  od_inter = filter(bristol_od, o != d)
  desire_lines = od2line(od_inter, zones_od)
}

# u_od = "https://user-images.githubusercontent.com/1825120/34081176-74fd39c8-e341-11e7-9f3e-b98807cb113b.png"
# knitr::include_graphics(u_od)
tmap_mode("plot")
desire_lines_top5 = od2line(od_top5, zones_od)
# tmaptools::palette_explorer()
tm_shape(desire_lines) +
  tm_lines(col = "Active",
           col.scale = tm_scale(values = viridis::plasma(5), breaks = c(0, 5, 10, 20, 40, 100)),
           col.legend = tm_legend(title = "Active travel (%)"),
           col_alpha = 0.6,
           lwd = "all",
           #lwd.scale = tm_scale(values.scale = 2),
           lwd.legend = tm_legend(title = "Number of trips"))  +
  tm_shape(desire_lines_top5) +
  tm_lines(lwd = 5, col = "black", col_alpha = 0.7) +
  tm_scalebar()
Robinlovelace/geocompr documentation built on June 14, 2025, 1:21 p.m.