Skip to contents

Given an sf data frame with the coordinates of a time series list transforms a data frame resulting from distantia() to an sf data frame with lines connecting the time series locations. This can be used to visualize a geographic network of similarity/dissimilarity between locations. See example for further details.

Usage

distantia_spatial_network(df = NULL, sf = NULL, one_to_many = FALSE)

Arguments

df

(required, data frame) Output of distantia() or distantia_aggregate(). Default: NULL

sf

(required, sf data frame) Points or polygons representing the location of the time series in argument 'df'. It must have a column with all time series names in df$x and df$y. Default: NULL

one_to_many

(optional, logical) If TRUE, the resulting data frame is reorganized to help map one-to-many relationships using the original geometry in sf rather than network edges. Default: FALSE

Value

sf data frame (LINESTRING geometry)

Examples

tsl <- distantia::tsl_initialize(
  x = distantia::covid_prevalence,
  name_column = "name",
  time_column = "time"
)

df_psi <- distantia::distantia_ls(
  tsl = tsl
)

#network many to many
sf_network <- distantia::distantia_spatial_network(
  df = df_psi,
  sf = distantia::covid_counties
)

#subset target counties
counties <- c("Los_Angeles", "San_Francisco", "Fresno", "San_Joaquin")

sf_network_subset <- sf_network[
  which(sf_network$x %in% counties & sf_network$y %in% counties), ]

#network map
# mapview::mapview(
#   distantia::covid_counties,
#   col.regions = NA,
#   color = "black",
#   label = "name",
#   legend = FALSE,
#   map.type = "OpenStreetMap"
# ) +
#   mapview::mapview(
#     sf_network_subset,
#     layer.name = "Psi",
#     label = "edge_name",
#     zcol = "psi",
#     lwd = 3
#   ) |>
#   suppressWarnings()

#one to many
sf_network <- distantia_spatial_network(
  df = df_psi,
  sf = distantia::covid_counties,
  one_to_many = TRUE
)

#subset one county
sf_network_subset <- sf_network[sf_network$x == "Los_Angeles", ]

#one to many map
#dissimilarity of all counties with Los Angeles
# mapview::mapview(
#   sf_network_subset,
#   layer.name = "Psi with LA",
#   label = "y",
#   zcol = "psi",
#   alpha.regions = 1
# ) |>
#   suppressWarnings()