Overlaying networks on maps using as reference http://gis.stackexchange.com/questions/172606/how-to-use-igraph-with-leaflet-for-r
Create sample datasets:
library(igraph)
df<-data.frame("from" = c("Lyon", "Toulouse", "Paris", "Marseille"),
"to"= c("Paris", "Paris", "Marseille", "Toulouse"),
"width" = 1:4)
meta <- data.frame("name"=c("Lyon", "Toulouse", "Paris", "Marseille"),
"lon"=c(-4.850000, 1.444209, 2.352222, 5.36978),
"lat"=c(45.750000, 43.604652, 48.856614, 43.296482))
g <- graph.data.frame(df, directed=FALSE, vertices=meta)
lo <- layout.norm(as.matrix(meta[,2:3]))
Plot on a map, note tooltips available on locations and edges:
library(sp)
gg <- get.data.frame(g, "both")
vert <- gg$vertices
coordinates(vert) <- ~ lon + lat
edges <- gg$edges
edges <- lapply(1:nrow(edges), function(i) {
as(rbind(vert[vert$name == edges[i, "from"], ],
vert[vert$name == edges[i, "to"], ]),
"SpatialLines")
})
for (i in seq_along(edges)) {
edges[[i]] <- spChFIDs(edges[[i]], as.character(i))
}
edges <- do.call(rbind, edges)
location_label <- function(loc){
paste0(
"City: ",loc
)
}
edge_label <- function(weight){
paste0(
"Weighting: ",weight
)
}
library(leaflet)
leaflet(vert) %>% addTiles() %>% addMarkers(data = vert, popup = ~location_label(name)) %>%
addPolylines(data = edges, weight = 5 * df$width, popup = edge_label(df$width))