【转】Plotting texts as graphs with R and igraph
原文转自:http://blog.ynada.com/303
I’ve plotted several word association graphs for this New York Times article (1st paragraph) using Rand the igraph library.
#1, random method

#2, circle method

#3, sphere method

#4, spring method

#5, fruchterman-reingold method

# 6, kamada-kawai method

#7, graphopt method

The red vertices mark cliques. Here’s the (rough) R code for plotting such graphs:
rm(list=ls());
library("igraph");
library("Cairo");
# read parameters
print("Text-as-Graph for R 0.1");
print("------------------------------------");
print("Path (no trailing slash): ");
datafolder <- scan(file="", what="char");
print("Text file: ");
datafile <- scan(file="", what="char");
txt <- scan(paste(datafolder, datafile, sep="/"), what="char", sep="\n", encoding="UTF-8");
print("Width/Height (e.g. 1024x768): ");
res <- scan(file="", what="char");
rwidth <- unlist(strsplit(res, "x"))[1]
rheight <- unlist(strsplit(res, "x"))[2]
words <- unlist(strsplit(gsub("[[:punct:]]", " ", tolower(txt)), "[[:space:]]+"));
g.start <- 1;
g.end <- length(words) - 1;
assocs <- matrix(nrow=g.end, ncol=2)
for (i in g.start:g.end)
{
assocs[i,1] <- words[i];
assocs[i,2] <- words[i+1];
print(paste("Pass #", i, " of ", g.end, ". ", "Node word is ", toupper(words[i]), ".", sep=""));
}
print("Build graph from data frame...");
g.assocs <- graph.data.frame(assocs, directed=F);
print("Label vertices...");
V(g.assocs)$label <- V(g.assocs)$name;
print("Associate colors...");
V(g.assocs)$color <- "Gray";
print("Find cliques...");
V(g.assocs)[unlist(largest.cliques(g.assocs))]$color <- "Red";
print("Plotting random graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-random.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.random, vertex.size=4, vertex.label.dist=0);
dev.off();
print("Plotting circle graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-circle.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.circle, vertex.size=4, vertex.label.dist=0);
dev.off();
print("Plotting sphere graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-sphere.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.sphere, vertex.size=4, vertex.label.dist=0);
dev.off();
print("Plotting spring graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-spring.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.spring, vertex.size=4, vertex.label.dist=0);
dev.off();
print("Plotting fruchterman-reingold graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-fruchterman-reingold.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.fruchterman.reingold, vertex.size=4, vertex.label.dist=0);
dev.off();
print("Plotting kamada-kawai graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-kamada-kawai.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.kamada.kawai, vertex.size=4, vertex.label.dist=0);
dev.off();
#CairoPNG(paste(datafolder, "/", "text-igraph-reingold-tilford.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
#plot(g.assocs, layout=layout.reingold.tilford, vertex.size=4, vertex.label.dist=0);
#dev.off();
print("Plotting graphopt graph...");
CairoPNG(paste(datafolder, "/", "text-igraph-graphopt.png", sep=""), width=as.numeric(rwidth), height=as.numeric(rheight));
plot(g.assocs, layout=layout.graphopt, vertex.size=4, vertex.label.dist=0);
dev.off();
print("Done!");
【转】Plotting texts as graphs with R and igraph的更多相关文章
- R包igraph探究
前段时候由于项目的原因,需要画图,然后开始接触R语言的igraph包,网上零零散散的搜罗了不少的信息,放在这边交流分享的同时也给自己留个备份吧~ 1.首先是读取文件,基本选用的都是csv文件 edge ...
- 用R的igraph包来画蛋白质互作网络图 | PPI | protein protein interaction network | Cytoscape
igraph语法简单,画图快速. Cytoscape专业,个性定制. 最终效果图: 当然也可以用Cytoscape来画. 参考:Network visualization with R Cytosca ...
- R语言igraph 包-构建网络图
igaph 是一个项目,目标是建立一条简单,易用的网络分析工具,有 R, python, C/C++ 等语言的具体实现: 项目主页: http://igraph.org/ 在R语言中,对应的就是 ig ...
- Graphics for R
https://cran.r-project.org/web/views/Graphics.html CRAN Task View: Graphic Displays & Dynamic Gr ...
- R语言构建蛋白质网络并实现GN算法
目录 R语言构建蛋白质网络并实现GN算法 1.蛋白质网络的构建 2.生物网络的模块发现方法 3.模块发现方法实现和图形展示 4.附录:igraph中常用函数 参考链接 R语言构建蛋白质网络并实现GN算 ...
- igraph安装(R/Python)
python-igraph:啥都不说了,用Ubuntu吧,虽然按照官方的流程还是会出错,但是排错会比较少,一般找不了多久就能找到解决方案 R-igraph:一般需要升级R版本,用3.3吧.升级R的方法 ...
- 安装 r 里的 igraph 报错
转载来源:http://genek.tv/article/40 1186 0 0 安装 r 里的 igraph 报错: foreign-graphml.c: In function ‘igraph_w ...
- R语言入门级实例——用igragh包分析社群
R语言入门级实例——用igragh包分析社群 引入—— 本文的主要目的是初步实现R的igraph包的基础功能,包括绘制关系网络图(social relationship).利用算法进行社群发现(com ...
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...
随机推荐
- 对开发中常见的内存泄露,GDI泄露进行检测
对开发中常见的内存泄露,GDI泄露进行检测 一.GDI泄露检测方法: 在软件测试阶段,可以通过procexp.exe 工具,或是通过任务管理器中选择GDI对象来查看软件GDI的对象是使用情况. 注意点 ...
- hdu 5461 Largest Point
Thinking about it: 对于式子 a * ti * ti + b * tj,可以看作时有两部分构成 a * ti * ti 和 b * tj,如果整个式子要最大,则要求这两部分都要尽量大 ...
- linux核心之进程管理
进程就是处于执行期的程序(目标码存放在某中介质上).进程并不仅仅局限于一段可执行程序代码,通常还包括其他资源,例如打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间 ...
- Java中布尔类型操作符&=,|=与^=的使用
今天在对同事的代码进行code review的时候,见到一个比较好玩的写法.“flag &= false:”,乍一看,还感觉他写错了,但是程序可以正常运行,赶紧去百度,看一下这个写法到底是怎么 ...
- XGPush集成(信鸽集成)demo
#import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #defi ...
- Spring注解与Spring与Struts2整合
@Component @Controller @Service @Repository 四大注解作用基本一样,只是表象在不同层面 @Resource @Scope Struts2与Spring整合:1 ...
- ThinkPHP第十七天(隐藏index.php和简短路径配置)
1.路由设置,让路径中不显示index.php方法: 第一步:在apache中的httpd.conf中查找: LoadModule rewrite_module modules/mod_rewrite ...
- PowerShell Remove all user defined variable in PowerShell
When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...
- (Problem 73)Counting fractions in a range
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- 帝国cms <!--list.var1-->,<!--list.var2-->的终极用法
谢寒原创,转载注明. 在制作帝国cms列表时,如果我们希望同每篇文章之后就会有一条横线,或者分线代码,我们就需要用到多个<!--list.var—> 比如我们需要这样的效果 文章1 文章 ...