【转】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下载资源的信息,最重要的是动态获取资源的评论,它是 ...
随机推荐
- cuda核函数再调用核函数,多层并行
#include <stdio.h> __global__ void childKernel(int i) { int tid = blockIdx.x*blockDim.x+thread ...
- Android首席设计师宣称移动概念已死,开发人员应该面向屏幕编写应用而非移动
腾讯科技对Android首席设计师Duarte"移动已死"訪谈内容的翻译错得离谱,被到处转载,误人视听. 而要真正理解Duarte所想表达的含义,须要深入了解互联网前沿设计理念以及 ...
- ADF 项目创建流程
ADF 项目创建流程: 1.首先建好应用 2.创建model,UI 3.创建EO,VO,AO, VL 4.设置EO的属性 5.新建lov 6.设置VO的View Accessors,并设置Attrib ...
- 新浪微博 2.4sdk 一闪而过
解决方法,保持 ios应用中的 build id和 开放平台中填写的一致
- SharePoint 2007 (MOSS/WSS) - how to remove "Download a Copy" context menu from a Document Library
One of my friend and colleague asked me this question. I found it tricky and a good post for my blog ...
- 小猪猪C++笔记基础篇(五)表达式、语句
小猪猪C++笔记基础篇(五) 关键词:表达式.语句 本章的内容比较简单,基本上没有什么理解上的困难,都是知识上的问题.先开始想要不要写呢,本来是不准备写的,但是既然读了书就要做笔记,还是写一写,毕竟还 ...
- [LeetCode]题解(python):134-Gas Station
题目来源: https://leetcode.com/problems/gas-station/ 题意分析: 在一个圈子路线里面有N个汽油站,i站的汽油有gas[i]汽油.现在有一辆无限容量的车,它从 ...
- PSAM卡
PSAM卡 终端安全控制模块,符合<中国金融集成电路(IC卡)PSAM卡规范>, 包括普通PSAM卡和高速PSAM卡. PSAM符合以下标准及规范: 识别卡,带触点的集成电路卡标 ...
- 去确认CP210x UART Bridge的USB的VID和PID
[背景] 之前买的USB口的HART猫: [记录]为USB接口的HART猫ExSaf ESH232U安装对应的USB转RS232驱动 其中内部是USB转RS232. 然后打算去看看之前的自己此处的某个 ...
- 用于展现图表的50种JavaScript库
在很多项目中都会有在前端展现数据图表的需求,而在开发过程中,开发者往往会使用一些JavaScript库,从而更有效地达到想要的目标.最近,TechSlide上的一篇文章总结了50种用于展现图表的Jav ...