2012-06-28 76 views
5

我發現這個answer真的很有用。它可以幫助我繪製網絡/圖形並選擇圖中節點的座標。igraph固定節點座標佈局

但是,佈局重新調整座標爲-1到1.首先我試圖找出它是如何做到這一點,但不能。它做這樣的事情?

(coordinate - mean(coordinates))/(coordinate + mean(coordinates) 

其次有沒有辦法保持原來的座標?我喜歡用圖表製作座標軸,所以不希望頂部重新調整一切。

回答

9

您的第一個問題的答案在plot.igraph函數的源代碼中;在R提示符下鍵入plot.igraph以獲取完整的源代碼。書中有一部分它說:

layout <- layout.norm(layout, -1, 1, -1, 1) 

layout.normigraph這確實魔法爲你的另一功能;鍵入layout.norm以查看它是如何工作的。

現在,第二個問題的答案非常簡單;只需將rescale=F傳遞給參數plot,這會使igraph跳過plot.igraph中的整個分支,其中layout.norm被調用,因此它將與您的原始座標一起工作。然後,您可以像往常一樣使用xlimylim來設置X軸和Y軸的極限。

+1

非常感謝您的幫助,現在檢查源代碼。儘管ylim = c(0,6)使xlim範圍也變爲6,但是軸限制看起來似乎是連在一起的。這是令人討厭的,但這是一個飛躍,再次感謝。 – user1320502

+2

@ user1320502:設置'asp = FALSE'以避免默認的1:1寬高比。 –

+0

當我設置'rescale = FALSE'時,爲什麼沒有輸出? – pengchy

0
set.seed(111) 
    ig <- graph_from_data_frame(as.data.frame(matrix(sample(letters,40,rep=TRUE),nc=2))) 
    set.seed(123) 
    ig.layout <- layout.fruchterman.reingold(ig) 
    rownames(ig.layout) <- V(ig)$name 
    par(bg="white",mar=c(0,0,0,0),oma=c(0,0,0,0)) 
    plot.igraph(ig,layout=ig.layout,vertex.color=adjustcolor("gray",alpha.f=0.5),rescale=FALSE,xlim=c(4,11),ylim=c(4,11)) 
    set.seed(321) 
    ig.sub <- subgraph(ig,sample(V(ig)$name,5)) 
    plot.igraph(ig.sub,layout=ig.layout[V(ig.sub)$name,],add=TRUE,vertex.color=adjustcolor("orange",alpha.f=0.5),rescale=FALSE) 

此代碼輸出圖形,其中橙色節點是稍後添加的。

enter image description here