Title: | Represent Network Objects on a Map |
---|---|
Description: | Represent 'network' or 'igraph' objects whose vertices can be represented by features in an 'sf' object as a network graph surmising a 'sf' plot. Fits into 'ggplot2' grammar. |
Authors: | Matteo Dimai [aut, cre] |
Maintainer: | Matteo Dimai <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.4 |
Built: | 2025-02-20 04:15:21 UTC |
Source: | https://github.com/artod83/netmap |
Checks whether the proper packages are installed, whether the parameters are of the proper classes, whether the network-map link is possible, then performs the link.
check_network_sf(n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names")
check_network_sf(n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names")
n |
A |
m |
A |
lkp |
An optional lookup table. |
m_name |
Optional |
n_name |
Optional |
A list with a network
or igraph
object with only the
vertices present in the sf
object as the first element and a list
with two vectors, one of features in m
present
both in the lookup table and in n
, the other of nodes in n
present both in the lookup table and in m
An sf object containing the ISTAT municipality codes, geometry and the municipality names in the Friuli Venezia Giulia region in northeastern Italy, based on official ISTAT shapefiles.
fvgmap
fvgmap
An sf object with 215 features and 6 fields:
region code, always =6 (Friuli Venezia Giulia)
province code (93=Pordenone, 30=Udine, 31=Gorizia, 32=Trieste)
municipality code, consists of province code + progressive code of the municipality within the province
length of municipality perimeter
municipality area
a MULTIPOLYGON
https://www.istat.it/it/archivio/104317
Given a sf
object with features that can be linked to a network
or igraph
object, obtain centrality indices for linked features.
ggcentrality( n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names", par.deg = NULL, par.bet = NULL, par.clo = NULL )
ggcentrality( n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names", par.deg = NULL, par.bet = NULL, par.clo = NULL )
n |
A |
m |
A |
lkp |
An optional lookup table. |
m_name |
Optional |
n_name |
Optional |
par.deg |
List with additional optional parameters to functions
|
par.bet |
List with additional optional parameters to functions
|
par.clo |
List with additional optional parameters to functions
|
An sf
object, input m
with added columns for centrality
indices (degree, betweenness, closeness; existing columns with the same name
will be overwritten) and with only the features linked to vertices in input
n
.
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) netmap::ggcentrality(net, map, lkptbl, "id", "name")
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) netmap::ggcentrality(net, map, lkptbl, "id", "name")
Given a sf
object with features that can be linked to a network
or igraph
object and given a node with id id
in said graph that
can be linked to the sf
object, obtain an indicator variable denoting,
for each node, a connection to id
.
ggconn_area(n, m, id, lkp = NULL, m_name = NULL, n_name = "vertex.names")
ggconn_area(n, m, id, lkp = NULL, m_name = NULL, n_name = "vertex.names")
n |
A |
m |
A |
id |
The identifier (as vertex attribute |
lkp |
An optional lookup table. |
m_name |
Optional |
n_name |
Optional |
An sf
object, input m
with an added column
conn_area
with an indicator variable set to 1 if the feature is
connected to the feature with vertex id id
, 0 otherwise.
In directed graphs, only outgoing links are considered a connection.
Any existing column with the same name will be overwritten, the result will
contain only the features linked to vertices in input. If the vertex
id
is not present in object n
, conn_area
will be set to
0 for all vertices.
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) ggconn_area(net, map, "b", lkptbl, "id", "name")
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) ggconn_area(net, map, "b", lkptbl, "id", "name")
Link a network
or igraph
and a sf
object in a
data.frame
for subsequent representation on a plot using ggplot2
.
ggnetmap( n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names", scale = FALSE, ... )
ggnetmap( n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names", scale = FALSE, ... )
n |
A |
m |
A |
lkp |
An optional lookup table. |
m_name |
Optional |
n_name |
Optional |
scale |
Whether coordinates should be scaled (defaults to FALSE since the
network should be overlayed with the non-scaled |
... |
Additional parameters passed to |
Using a network
or igraph
and a sf
object as inputs,
with an optional lookup table (a data.frame
) in case the IDs don't
match, produces a data.frame
that can be used with ggnetwork
's
geom_edges
and geom_nodes
functions to represent the network as overlayed on a sf
object in a
ggplot2
graph. Only vertices with a corresponding feature in the
sf
object are included.
A data frame, produced by fortify
, which can be
used as data source in ggplot2
graphs.
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) ggnetmap(net, map, lkptbl, "id", "name")
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) ggnetmap(net, map, lkptbl, "id", "name")
Checks whether a data.frame
is a valid lookup table.
is_lookup_table(lkp, m_name = NULL, n_name = NULL)
is_lookup_table(lkp, m_name = NULL, n_name = NULL)
lkp |
A |
m_name |
Optional, a |
n_name |
Optional, a |
FALSE
on error, a vector with m_name
and n_name
if the lookup table is valid.
Checks whether an object is a network
object or an igraph
object,
returns message if it's not
is_network(n)
is_network(n)
n |
Object of class |
TRUE
if object of class network
, FALSE
otherwise.
Checks whether an object is an sf
object, returns message if it's not
is_sf(m)
is_sf(m)
m |
Object of class |
TRUE
if object of classes sf
and data.frame
, FALSE
otherwise.
Checks which vertices of a network
object can be represented with
features of a sf
object.
link_network_map(m, n, m_name, n_name = "vertex.names")
link_network_map(m, n, m_name, n_name = "vertex.names")
m |
Object of class |
n |
Object of class |
m_name |
Name of the map field to use for the link. |
n_name |
Name of the vertex attribute to use for the link, defaults to
|
On success a list with two vectors, one of features in m
present in n
, the other of nodes in n
present in m
,
-1
on error.
Checks which vertices of a network object can be represented with features of a sf object with a lookup table.
link_network_map2(m, n, lkp, m_name = NULL, n_name = NULL)
link_network_map2(m, n, lkp, m_name = NULL, n_name = NULL)
m |
Object of class |
n |
Object of class |
lkp |
Lookup table, a |
m_name |
Optional character, name of field in |
n_name |
Optional character, name of vertex attribute in |
On success a list with two vectors, one of features in m
present
both in the lookup table and in n
, the other of nodes in n
present
both in the lookup table and in m
, -1
on error.
network
and igraph
objects on a sf
map
using ggplot2
The netmap package extends the ggnetwork
package by providing functions to
plot networks, with vertices usually representing objects with a spatial
attribute (cities, regions, countries, users with location data attached etc.)
on a map, provided by a sf
object (which in turn is able to represent
more or less all spatial data available). Networks and maps need not have the
same set of elements: if they don't, only the intersection will be represented.
The main function is ggnetmap
, which produces a data.frame
that is then used as data
within ggplot2
calls. For those wishing
to use the plot.network
or the
plot.igraph
function to plot the network
(without overlaying it on an sf
object), both a custom layout function,
network.layout.extract_coordinates
, and a wrapper that provides
convenient manipulation of network
and sf
objects,
netmap_plot
, are available.
sf
objectWrapper for plot.network
and
plot.igraph
using a custom network layout that extracts
coordinates of centroids from a sf
object. Only vertices with a
corresponding feature are plotted.
netmap_plot(n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names", ...)
netmap_plot(n, m, lkp = NULL, m_name = NULL, n_name = "vertex.names", ...)
n |
A |
m |
A |
lkp |
An optional lookup table. |
m_name |
Optional |
n_name |
Optional |
... |
Additional parameters passed to |
A plot of the network.
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) netmap::netmap_plot(net, map, lkptbl, "id", "name")
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) netmap::netmap_plot(net, map, lkptbl, "id", "name")
sf
objectCustom layout for plot.network
, extracting coordinates
of vertices from a sf
object. Its result can be used by
plot.igraph
as well.
network.layout.extract_coordinates(n, layout.par)
network.layout.extract_coordinates(n, layout.par)
n |
A |
layout.par |
A |
A matrix whose rows contain the x,y coordinates of the vertices of n
.
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) netmap::network.layout.extract_coordinates(net, list(sf=map))
net=network::network(matrix(c(0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0), nrow=4, byrow=TRUE)) network::set.vertex.attribute(net, "name", value=c("a", "b", "c", "d")) wkb = structure(list("01010000204071000000000000801A064100000000AC5C1641", "01010000204071000000000000801A084100000000AC5C1441", "01010000204071000000000000801A044100000000AC5C1241", "01010000204071000000000000801A024100000000AC5C1841"), class = "WKB") map=sf::st_sf(id=c("a1", "b2", "c3", "d4"), sf::st_as_sfc(wkb, EWKB=TRUE)) lkptbl=data.frame(id=c("a1", "b2", "c3", "d4"), name=c("a", "b", "c", "d")) netmap::network.layout.extract_coordinates(net, list(sf=map))
Removes vertices from a network
or igraph
object which are not
present in the link vector produced by link_network_map
or
link_network_map2
.
reduce_to_map(n, link, n_name)
reduce_to_map(n, link, n_name)
n |
A |
link |
A vector with the identifiers of the vertices to keep. |
n_name |
Name of the vertex attribute to filter on. |
A network
or igraph
object with only the vertices
listed in link
.