https://github.com/artod83/netmap
Represent Network Objects on a Map
https://github.com/artod83/netmap
ggplot2 network-analysis r sfc
Last synced: 8 months ago
JSON representation
Represent Network Objects on a Map
- Host: GitHub
- URL: https://github.com/artod83/netmap
- Owner: artod83
- License: gpl-3.0
- Created: 2021-12-22T12:17:12.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-02-26T13:21:50.000Z (over 2 years ago)
- Last Synced: 2025-09-28T01:42:56.537Z (8 months ago)
- Topics: ggplot2, network-analysis, r, sfc
- Language: R
- Homepage:
- Size: 698 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/",
out.width = "100%"
)
```
# netmap
Netmap is a package that aids in the visualizations of network objects (made with the `network` or the `igraph` package) on geographical maps, imported through `sf`. Vertices in the network are linked with features in the `sf` object either directly if they share a common attribute or by an optional look-up table.
The package either creates data frames that can be used in a `ggplot2` plot or adds to `sf` features attributes like centrality indices or variables representing connections to a certain node.
## Installation
You can install the development version of netmap from [GitHub](https://github.com/) with:
``` {r eval=FALSE}
# install.packages("devtools")
devtools::install_github("artod83/netmap")
```
## Example
We take a network object, where each vertex is a geographical object, and overlay the network on the map. I.e. a network could represent direct bus routes between cities.
We can plot the network as overlayed on the complete map or just the 4 cities in the network
The Friuli Venezia Giulia region in northeastern Italy has four major cities: from east to west, Trieste, Gorizia, Udine and Pordenone. Let's imagine that we want to plot a network with these cities as vertices and edges between Trieste and Gorizia, Gorizia and Udine, Trieste and Udine and Udine and Pordenone.
```{r network_overlay, eval=FALSE}
library(ggplot2)
library(netmap)
data(fvgmap)
routes=network::network(matrix(c(0, 1, 1, 0,
1, 0, 1, 0,
1, 1, 0, 1,
0, 0, 1, 0), nrow=4, byrow=TRUE))
network::set.vertex.attribute(routes, "names", value=c("Trieste", "Gorizia", "Udine", "Pordenone"))
routes_df=netmap::ggnetmap(routes, fvgmap, m_name="Comune", n_name="names")
ggplot() +
geom_sf(data=fvgmap) +
ggnetwork::geom_edges(data=routes_df, aes(x=x,y=y, xend=xend, yend=yend), colour="red") +
ggnetwork::geom_nodes(data=routes_df, aes(x=x,y=y)) +
ggnetwork::geom_nodetext(data=routes_df, aes(x=x,y=y, label = Comune), fontface = "bold") +
theme_blank()
```
```{r plot_1, echo=FALSE, eval=TRUE}
knitr::include_graphics('man/figures/netmap-1.png')
```
It is also possible to plot a sf object with centrality measures of the network:
```{r centrality, eval=FALSE}
routes2=network::network(matrix(c(0, 1, 1, 0, 0, 1 ,
1, 0, 1, 0, 0, 1,
1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 1,
0, 0, 1, 1, 0, 0,
1, 1, 1, 1, 0, 0), nrow=6, byrow=TRUE))
network::set.vertex.attribute(routes2, "names",
value=c("Trieste", "Gorizia", "Udine", "Pordenone",
"Tolmezzo", "Grado"))
lkpt=data.frame(Pro_com=c(32006, 31007, 30129, 93033, 30121, 31009),
names=c("Trieste", "Gorizia", "Udine", "Pordenone", "Tolmezzo",
"Grado"))
routes2_df=netmap::ggnetmap(routes2, fvgmap, lkpt, m_name="Pro_com", n_name="names")
map_centrality=netmap::ggcentrality(routes2, fvgmap, lkpt, m_name="Pro_com",
n_name="names", par.deg=list(gmode="graph"))
ggplot() +
geom_sf(data=fvgmap) +
geom_sf(data=map_centrality, aes(fill=degree)) +
ggnetwork::geom_edges(data=routes2_df, aes(x=x,y=y, xend=xend, yend=yend), colour="red") +
ggnetwork::geom_nodes(data=routes2_df, aes(x=x,y=y)) +
ggnetwork::geom_nodetext(data=routes2_df, aes(x=x,y=y, label = names), fontface = "bold", colour="") +
theme_blank()
```
```{r plot_2, echo=FALSE, eval=TRUE}
knitr::include_graphics('man/figures/netmap-2.png')
```
Just the network can be plotted as well without using `ggplot2`. For this purpose,
a simple layout function, `network.layout.extract_coordinates`, is provided for use with `plot.network` or `plot.igraph`. A user-friendly wrapper, `netmap_plot`, is available.