LoginSignup
0
1

More than 3 years have passed since last update.

Rのsnaパッケージで緯度・経度を反映させたグラフの描画について

Last updated at Posted at 2020-01-23

はじめに

Rのsnaパッケージを使って緯度・経度を反映させたグラフを描こうとしているのですが、
ノードが密集してしまってうまく書くことができません。

もし良い解決方法をご存知の方がいらっしゃったらアドバイス頂けますと幸いです。

書いたコード

R
#snaパッケージを読み込み
library(sna)

#二部グラフのcsvファイルを読み込み
test <- read.csv("test.csv", row.names = "test") 
#matrixに変換
test <- as.matrix(test)
#転置行列を乗じる。
resulttest <- test%*%t(test)  

#ノードの緯度・経度のcsvファイルを読み込み
loctest <- read.csv("testgeo.csv",header = T,row.names = 1)
#「coo」と定義
coo <- loctest[,1:2]

#ノードに緯度・経度を反映させてプロットする。
gplot(resulttest,gmode = "graph",displaylabels = TRUE,
edge.col = rgb(0.6,0.7,0.8,0.5),jitter = FALSE,
label.pos = 5,coord = coo)

csvファイルの内容

・「test」の内容は以下のとおりです。
スクリーンショット 2020-01-23 21.20.45.png

・「testgeo」の内容は以下のとおりです。
スクリーンショット 2020-01-23 21.21.00.png

出力されたグラフ

スクリーンショット 2020-01-23 21.26.04.png

このような感じで、うまく表示することができませんでした。

追記

こんな感じでノードの大きさとエッジの幅を狭くして、ノードにラベルを表示するのを諦めたら多少マシになりました。

R
gplot(resulttest,gmode = "graph",displaylabels = FALSE,
edge.col = rgb(0.6,0.7,0.8,0.5),jitter = FALSE,
label.pos = 5,coord = coo,vertex.cex = 0.3,edge.lwd = 0.3)

スクリーンショット 2020-01-23 21.47.01.png

追記2

「suppress.axes = FALSE」で軸を表示させて、「xlim = c(133,138),ylim = c(34,37)」でプロットする範囲を指定するとこんな感じになりました。

R
gplot(resulttest,gmode = "graph",displaylabels = FALSE,
edge.col = rgb(0.6,0.7,0.8,0.5),jitter = FALSE,
label.pos = 5,coord = coo,vertex.cex = 0.3,edge.lwd = 0.3,
suppress.axes = FALSE,xlim = c(133,138),ylim = c(34,37))

スクリーンショット 2020-01-23 21.53.30.png

追記3

vertex.sidesを使って頂点の辺の数を指定することで、頂点の形状を変更できます。
(3→三角形、4→四角形、50→丸)
また、vertex.colを使って色を変更することができます。

以下のとおり、行列について上から順に形と色を指定していくことができます。
(行列の全部で19の頂点について、上から順にrep(3,4)→三角形を4つ、rep(4,5)→四角形を5つ、rep(50,10)→丸を10個など。色も同じ。)

R
gplot(resulttest,gmode = "graph",displaylabels = FALSE,
edge.col = rgb(0.6,0.7,0.8,0.5),jitter = FALSE,label.pos = 5,
coord = coo,vertex.cex = 0.3,edge.lwd = 0.3,suppress.axes = FALSE,
xlim = c(133,138),ylim = c(34,37),
vertex.sides = c(rep(3,4),rep(4,5),rep(50,10)),
vertex.col = c(rep("blue",4),rep("red",5),rep("green",10)))

スクリーンショット 2020-01-25 22.32.17.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1