<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>生信菜鸟团 &#187; sna</title>
	<atom:link href="http://www.bio-info-trainee.com/tag/sna/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bio-info-trainee.com</link>
	<description>欢迎去论坛biotrainee.com留言参与讨论，或者关注同名微信公众号biotrainee</description>
	<lastBuildDate>Sat, 28 Jun 2025 14:30:13 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.33</generator>
	<item>
		<title>R语言画网络图三部曲之sna</title>
		<link>http://www.bio-info-trainee.com/1355.html</link>
		<comments>http://www.bio-info-trainee.com/1355.html#comments</comments>
		<pubDate>Thu, 14 Jan 2016 12:59:07 +0000</pubDate>
		<dc:creator><![CDATA[ulwvfje]]></dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[sna]]></category>
		<category><![CDATA[网络图]]></category>

		<guid isPermaLink="false">http://www.bio-info-trainee.com/?p=1355</guid>
		<description><![CDATA[如果只是画网络图，那么只需要把所有的点，按照算好的坐标画出来，然后把所有的连线也 &#8230; <a href="http://www.bio-info-trainee.com/1355.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>如果只是画网络图，那么只需要把所有的点，按照算好的坐标画出来，然后把所有的连线也画出即可！</p>
<div><span style="font-family: Times New Roman;">其中算法就是，点的坐标该如何确定？Two of the most prominente algorithms (<a href="http://en.wikipedia.org/wiki/Force-based_algorithms_%28graph_drawing%29">Fruchterman &amp; Reingold’s</a> force-directed placement algorithm and <a href="http://cs.wellesley.edu/~cs315/Papers/Kamada-Graph%20Drawing.pdf">Kamada-Kawai’s</a>)</span></div>
<div><span style="font-family: Times New Roman;">有一个networkD3的包可以直接画图，但是跳过了确定点的坐标这个步骤，我重新找了一包，可以做到！</span></div>
<div><span style="font-family: Times New Roman;">来自于一个博客：<b><a href="https://sumtxt.wordpress.com/2011/07/02/visualizing-networks-with-ggplot2-in-r/" target="_blank">https://sumtxt.wordpress.com/2011/07/02/visualizing-networks-with-ggplot2-in-r/</a></b></span></div>
<div>
<div><span style="color: #000000; font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">作者只是用sna包来得到数据，其实用的是ggplot来画网络图！</span></div>
<div><span style="color: #000000; font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">需要熟悉network包里面的network对象的具体东西，如何自己构造一个，然后数学sna包如何计算layout即可</span></div>
<div><span style="color: #ff00ff; font-family: Helvetica Neue,Helvetica,Arial,sans-serif;"><a href="https://cran.r-project.org/web/packages/network/network.pdf">https://cran.r-project.org/web/packages/network/network.pdf</a><br />
</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;"><span style="color: #ff00ff;"><a href="https://cran.r-project.org/web/packages/sna/sna.pdf">https://cran.r-project.org/web/packages/sna/sna.pdf</a></span></span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">解读这个包，也可以自己画网络图，代码如下：</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">plot(plotcord)</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">text(x=plotcord$X1+0.2,y=plotcord$X2,labels = LETTERS[1:10])</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">for (i in 1:10){</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">  for (j in 1:10){</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">    if(tmp[i,j]) lines(plotcord[c language="(i,j),1"][/c],plotcord[c language="(i,j),2"][/c])</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">  }</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">}</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">当然，我们还没有涉及到算法，就是如何生成plotcord这个坐标矩阵的！</span></div>
<div><span style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;">大家看下面这个示意图就知道网络图是怎么样画出来的了，首先我们有一些点，它们之间有联系，都存储在networData这个数据里面，是10个点，共9个连接，然后我用reshape包把它转换成连接矩阵，理论上10个点的两两相互作用应该有100条线，但是我们的数据清楚的说明只有9条，所以只有9个1，其余的0代表点之间没有关系。接下来我们用sna这个包对这个连接矩阵生成了这10个点的坐标（<b>这个是重点），最后很简单了，把点和线画出来即可！</b></span></div>
<p><a href="http://www.bio-info-trainee.com/wp-content/uploads/2016/01/13.png"><img class="alignnone size-full wp-image-1361" src="http://www.bio-info-trainee.com/wp-content/uploads/2016/01/13.png" alt="1" width="704" height="596" /></a></div>
<div>另外一个例子：</div>
<div>
<div><b>net=network(150, directed=FALSE, density=0.03)</b></div>
<div><b>m &lt;- as.matrix.network.adjacency(net) # get sociomatrix  </b></div>
<div><b># get coordinates from Fruchterman and Reingold's force-directed placement algorithm.</b></div>
<div><b>plotcord &lt;- data.frame(gplot.layout.fruchtermanreingold(m, NULL)) </b></div>
<div><b># or get it them from Kamada-Kawai's algorithm: </b></div>
<div><b># plotcord &lt;- data.frame(gplot.layout.kamadakawai(m, NULL)) </b></div>
<div><b>colnames(plotcord) = c("X1","X2")  ###所有点的坐标，共150个点</b></div>
<div><b>edglist &lt;- as.matrix.network.edgelist(net) ##所有点之间的关系-edge ##共335条线</b></div>
<div><b>edges &lt;- data.frame(plotcord[edglist[,1],], plotcord[edglist[,2],]) ##两点之间的连线的具体坐标，335条线的起始终止点点坐标</b></div>
</div>
<div><span style="font-family: Times New Roman;">原始代码如下：</span></div>
<div><span style="font-family: Times New Roman;">library(network)</span></div>
<div><span style="font-family: Times New Roman;">library(ggplot2)</span></div>
<div><span style="font-family: Times New Roman;">library(sna)</span></div>
<div><span style="font-family: Times New Roman;">library(ergm)</span></div>
<div><a href="http://www.bio-info-trainee.com/wp-content/uploads/2016/01/clipboard11.png"><img class="alignnone size-full wp-image-1362" src="http://www.bio-info-trainee.com/wp-content/uploads/2016/01/clipboard11.png" alt="clipboard" width="823" height="596" /></a></div>
<div><span style="font-family: Times New Roman;">大家可以试用这个代码，因为它用的ggplot，肯定比我那个简单R作图要好看多了<br />
</span></div>
<div><span style="font-family: Times New Roman;">参考算法文献：</span></div>
<div><span style="font-family: Times New Roman;"><a href="http://www.jstatsoft.org/article/view/v024i02/v24i02.pdf" target="_blank">http://www.jstatsoft.org/article/view/v024i02/v24i02.pdf</a><br />
</span></div>
<div><span style="font-family: Times New Roman;"><a href="http://www.jstatsoft.org/article/view/v024i06/v24i06.pdf" target="_blank">http://www.jstatsoft.org/article/view/v024i06/v24i06.pdf</a><br />
</span></div>
<div><span style="font-family: Times New Roman;"><span style="color: #000000; font-family: Helvetica Neue,Helvetica,Arial,sans-serif;"><a href="http://web.stanford.edu/~messing/RforSNA.html" target="_blank">http://web.stanford.edu/~messing/RforSNA.html</a></span><br />
</span></div>
<div><span style="font-family: Times New Roman;"> </span></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bio-info-trainee.com/1355.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
