我想生成一個類似於the one below from this page的R世界網絡圖。R:創建世界網絡圖
我一直在尋找的R包,讓我這樣做,但我一直沒能找到一個。有D3 JavaScript Network Graphs from R,但我找不到世界網絡地圖示例。
如何在R中創建類似的東西?
我想生成一個類似於the one below from this page的R世界網絡圖。R:創建世界網絡圖
我一直在尋找的R包,讓我這樣做,但我一直沒能找到一個。有D3 JavaScript Network Graphs from R,但我找不到世界網絡地圖示例。
如何在R中創建類似的東西?
好,FWIW:這裏有一個快速的&骯髒的方式在地圖上繪製的頂點(「城市」)和連接頂點之間的邊緣與箭頭:
library(maps)
library(diagram)
library(plotrix)
palette(rainbow(20))
data("world.cities")
pdf(tf <- tempfile(fileext = ".pdf"), width = 40, height = 20)
map('world', fill = TRUE, col = "lightgray", mar = rep(0, 4))
nodes <- transform(with(world.cities, world.cities[pop > 5e6,]), country.etc = as.factor(country.etc))
with(nodes, points(long, lat, col=country.etc, pch=19, cex=rescale(pop, c(1, 8))))
set.seed(1)
edges <- subset(data.frame(from = sample(nodes$name, 20, replace = TRUE), to = sample(nodes$name, 20, replace = TRUE), stringsAsFactors = F), from != to)
edges <- merge(merge(edges, nodes[, c("name", "long", "lat")], by.x = "from", by.y = "name"), nodes[, c("name", "long", "lat")], by.x = "to", by.y = "name")
edges$col <- as.integer(nodes$country.etc[match(edges$from, nodes$name)])
apply(edges[, -(1:2)], 1, function(x) curvedarrow(to=x[3:4], from=x[1:2], lcol=x[5], curve=.1, arr.pos = 1, lwd=.5))
dev.off()
shell.exec(tf)
這裏是一個解決方案使用geosphere
和maps
包。 使用gcIntermediate
函數可以「在用於定義大圓的兩點之間的大圓上的中間點」。
這裏是示出從JKF機場航班conncetions(樣本數據從nycflights13
封裝dplyr
過濾)的示例:
library(maps)
library(geosphere)
library(dplyr)
library(nycflights13)
usairports <- filter(airports, lat < 48.5)
usairports <- filter(usairports, lon > -130)
usairports <- filter(usairports, faa!="JFK")
jfk <- filter(airports, faa=="JFK")
map("world", regions=c("usa"), fill=T, col="grey8", bg="grey15", ylim=c(21.0,50.0), xlim=c(-130.0,-65.0))
for (i in (1:dim(usairports)[1])) {
inter <- gcIntermediate(c(jfk$lon[1], jfk$lat[1]), c(usairports$lon[i], usairports$lat[i]), n=200)
lines(inter, lwd=0.1, col="turquoise2")
}
points(usairports$lon,usairports$lat, pch=3, cex=0.1, col="chocolate1")
這是基於貼在一個教程我的blog。
此帖子和前段時間做類似的事情:http://stackoverflow.com/a/19695755/1718356 – Andy
哪裏會是最好的地方發佈幫助呢? –