2013-10-28 46 views
0

我有200個具體商品國家的貿易數據(出口/進口)。 例子:r國家之間貿易流量/貿易網絡的可視化

a <- c(2000, 2000, 2000, 2000, 2000, 2000) 
b <- c("countryA", "countryB", "countryC", "countryA", "countryC", "countryA") 
c <- c("countryB", "countryC", "countryA", "countryB", "countryA", "countryB") 
d<- c(100, 200, 200, 300, 400, 200) 
mydata <- data.frame(a,b,c,d) 
colnames(mydata) <- c("year", "exporteur", "partner", "tradeflow") 

現在我想以可視化的個別貿易r中的國家之間流動。

應該是這樣的: http://www.graphviz.org/Gallery/directed/world.html

有沒有辦法做到這一點爲r?

在此先感謝!

+1

您好,歡迎計算器!非常感謝您提供一個小的虛擬數據集和您想要的結果。你能否也請告訴我們你已經嘗試過自己。請閱讀[關於Stackoverflow](http://stackoverflow.com/about)和[問什麼](http://stackoverflow.com/help/on-topic)。正如你在這兩個鏈接中發現的,你應該「展示你的工作!」。那就是:「詢問代碼的問題必須包括嘗試解決方案,以及爲什麼他們不工作」。乾杯。 – Henrik

回答

1

你可能想看看igraph包。以下是使用igraph進行圖形顯示的示例。

require(igraph) 
adj.mat <- matrix(c(0, 0, 1, 1, 0, 0, 0, 1, 0), nrow=3) 
colnames(adj.mat) <- c("A", "B", "C") 
g <- graph.adjacency(adj.mat) 
plot(g) 

關於第二個想法,你可以用你的數據來創建graph.data.frame圖表:

mydata <- data.frame(b,c,a,d) # different order necessary for graph.data.frame 
colnames(mydata) <- c("exporteur", "partner", "year", "tradeflow") 
g <- graph.data.frame(mydata) 
plot(g) 
+0

精彩,謝謝! –

相關問題