1

ggnetwork:如何将地理信息放在顶点上?

ggnetwork(n, layout = "fruchtermanreingold", cell.jitter = 0.75)

ggnetwork 用于生成这些坐标的默认节点放置算法是 Fruchterman-Reingold 力导向布局算法。我想把地理信息(纬度,经度)

4

2 回答 2

1

文件说_

layout参数还将接受用户提交的坐标作为两列矩阵,其行数与网络中的节点数一样多。

所以你可以提供节点的坐标作为矩阵。但是它使用scale函数,原始值丢失了,只保留了相对位置。所以需要撤消缩放,或者修改源以绕过缩放。它位于R/fortify.network.R的第 121 行附近

于 2018-11-05T02:31:21.763 回答
0

使用地理坐标所需的选项ggnetwork刚刚添加到包的开发版本中。

下面的例子:

library(dplyr)
library(nycflights13)
library(network)
# IMPORTANT: install from `devtools` or `remotes`
# remotes::install_github("briatte/ggnetwork")
library(ggnetwork)
library(ggmap)
library(ggplot2)

# edges: flights
e <- select(flights, origin, dest) %>%
  group_by(origin, dest) %>%
  tally %>% 
  filter(origin != dest, n > 2500) %>%  # no self-loops + thinning
  filter(origin %in% airports$faa, dest %in% airports$faa) # lose SJU

# weighted directed network
n <- network::network(select(e, origin, dest), directed = TRUE)
set.edge.attribute(n, "weight", e$n) # weight = number of flights

# nodes: airports
y <- filter(airports, faa %in% e$origin | faa %in% e$dest) %>% 
  select(faa, lat, lon)

# sanity check (no missing airports)
stopifnot(nrow(y) == network.size(n))

# named geo coordinate vectors...
lat <- y$lat
names(lat) <- y$faa
lon <- y$lon
names(lon) <- y$faa
# ... as a (x, y) coordinate matrix
geo <- cbind(lon[ network.vertex.names(n) ], lat[ network.vertex.names(n) ])

# geographic network
g <- ggnetwork(n, layout = geo, scale = FALSE) %>% 
  rename(lon = x, lat = y)

# map background
map <- ggmap::get_map(c(left = min(geo[, 1]), bottom = min(geo[, 2]), right = max(geo[, 1]), top = max(geo[, 2])))
# map + network plot
ggmap::ggmap(map) +
  geom_point(data = g, aes(lon, lat)) +
  geom_edges(data = g, aes(lon, lat, xend = xend, yend = yend)) +
  geom_nodelabel(data = g, aes(label = vertex.names))

ggsave("map_example.png")

地图示例

于 2019-07-30T14:56:12.773 回答