Plot graph with arced edges on top of ggmap

I have a graph with geographic locations stored in an SQLITE database, and I want to plot a subsection of it with arced edges on top of map tiles. I got the Points to be drawn on the correct map locations using

library(DBI) library(sf) library(ggmap) library(tidygraph) library(ggraph) library(ggplot2)  con <- dbConnect(RSQLite::SQLite(), "../plot.sqlite")  dbExecute(con,               "CREATE TEMPORARY VIEW IF NOT EXISTS here AS SELECT ROW_NUMBER() OVER (ORDER BY hexbin) AS node, hexbin, habitable, vlongitude as longitude, vlatitude as latitude FROM hex WHERE vlongitude < -150.5 AND vlongitude > -151.5 AND vlatitude > 59.75 AND vlatitude < 60.25 LIMIT 10")  nodes <- dbFetch(dbSendQuery(con, "SELECT * FROM here")) edges <- dbFetch(dbSendQuery(con, "SELECT here1.node AS `from`, here2.node AS `to`, distance FROM dist JOIN here AS here1 ON dist.hexbin1 == here1.hexbin JOIN here AS here2 ON dist.hexbin2 == here2.hexbin"))  nodes <- data.frame(   node=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),   hexbin=c(-80266354, 80252696, 80252697, 80252850, 80252980, 80252981, 80253631, 80253632, 80253907, 80254261),   habitable=c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),   longitude=c(-150.9438, -151.1729, -150.9938, -150.9813, -151.1646, -151.1604, -150.8521, -150.8313, -150.8104, -150.8021),   latitude=c(59.79375, 60.24375, 60.24375, 60.23958, 60.23542, 60.23542, 60.21458, 60.21458, 60.20625, 60.19792))  edges <- data.frame(   from=c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10),   to=c(2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10, 2, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10),   distance=c(0.0, 12753.13, 14009.8, 3.446667, 1330.21, 1628.646, 25417.91, 26900.81, 29122.5, 30454.68, 12753.07, 0, 3.843333, 1258.867, 12896.77, 12600.61, 12666.48, 14148.82, 16370.37, 17702.55, 14011.96, 498.0562, 1258.883, 0, 13419.04, 13122.88, 29.52333, 11408.56, 12891.08, 15112.71, 16444.88, 441.3853, 1331.829, 12891.69, 13417.91, 0, 0.7666667, 298.4363, 24092.76, 25575.84, 27797.81, 29129.99, 1626.942, 12594.82, 13119.47, 99.35205, 296.1583, 0, 23794.32, 25277.41, 27499.37, 28831.55, 54550.65, 25419.67, 12666.6, 3805.51, 11408.66, 24090.87, 23794.71, 0, 3.836667, 1483.652, 3705.955, 5038.134, 53704, 26902, 14148.93, 12891.18, 25573.96, 25277.8, 491.2628, 1483.652, 0, 5.75, 2222.304, 3554.483, 51880.78, 29123.33, 16370.26, 15112.84, 27795.95, 27499.8, 3705.988, 736.7606, 2222.337, 0, 3.45, 1332.179, 50548.6, 30455.54, 17702.47, 16445.05, 29128.17, 28832.01, 5038.2, 3554.548, 447.0842, 1332.212, 0))  locations <- st_as_sf(   nodes, coords = c("longitude", "latitude"),   crs = 4326,  agr = "constant")  graph <- tbl_graph(nodes=locations, edges=edges, directed = TRUE)  mapbox <- st_bbox(locations) names(mapbox) <- c("left", "bottom", "right", "top") background <- get_map(location = mapbox, source = "stamen", maptype = "toner")  locations$habitable = !is.na(locations$habitable)  plot(st_transform(locations[, "habitable"], crs = 3857)[1], bgMap = background) 

basemap with nodes

I can also create the edge visualizations separately using

ctr <- as.data.frame(as(st_geometry(locations), "Spatial")@coords) names(ctr) <- c("x", "y") ggraph(graph, layout=ctr) + geom_edge_arc(edge_alpha=0.3, strength=0.2) 

arced edges

But I fail to get these things to combine, preferably with nice looking code. I got as far as

ctr <- as.data.frame(as(st_geometry(locations), "Spatial")@coords) names(ctr) <- c("x", "y") ggmap(background) +   geom_edge_arc(aes(), data=get_edges(graph, format="short", collapse="none")(     layout=create_layout(graph, layout=ctr))) +   geom_sf(aes(color=habitable), data = locations)  

before giving up, because that snippet of code complains about Error in rep.igraph(X[[i]], ...) : argument "n" is missing, with no default and I really don’t see how rep.igraph plays into this or how it gets fed its n parameter normally when called from ggraph. In addition, I suspect there is a much easier way to do this that would also be properly CRS-aware. How should I do this?

Asked on August 30, 2020 in R.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.