78

基于ggplot2的网络可视化(1)

 5 years ago
source link: http://www.10tiao.com/html/404/201806/2651058140/2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
作者简介

作者:吴健 中国科学院大学 R语言、统计学爱好者,尤其擅长R语言和Arcgis在生态领域的应用分享。个人公众号:统计与编程语言 


往期回顾:

R语言绘制条形图


本文介绍三种基于ggplot2包可视化网络数据的方法,以便于读者进一步掌握ggplot2的图形语法,并基于该语法可视化网络数据。


首先我们需要加载和安装相关的包。

安装包:

install.packages(“GGally”)
install.packages(“network”)
install.packages(“geomnet”)
install.packages(“ggnetwork”)
install.packages(“ggplot2”)

加载包:

library(ggplot2)
library(GGally)
library(geomnet)
library(ggnetwork)
library(“network”)

加载数据

data(blood, package='geomnet')#该数据主要展示了不同血型间是否可以捐献的网络结果

(1)基于ggnet2绘制网络数据

set.seed(12252016)
ggnet2(network(blood$edge[,1:2],directed=TRUE), mode=’circle’, size=15,
 label=T, arrow.size=10, arrow.gap=0.05, vjust=0.5, node.color=’darkred’,
 label.color=’grey80’)




(2)基于geomnet绘制网络数据


set.seed(12252016)
ggplot(data=blood$edges, aes(from_id=from, to_id=to))+
 geom_net(colour=”darkred”, layout.alg=’circle’,
 size=15,labelon=TRUE, vjust=0.5, labelcolour=’grey80’,arrowsize=1.5,
 linewidth=0.5, arrowgap=0.05, ecolour=’grey40’, selfloops=TRUE,directed=T)+theme_net()


(3)基于ggnetwork绘制网络数据


set.seed(12252016)
ggplot(ggnetwork(network(blood$edges[,1:2]),layout=’circle’, arrow.gap=0.05),aes(x,y,xend=xend,yend=yend))+
 geom_edges(color=’grey50’, arrow=arrow(length=unit(10,’pt’),type=’closed’))+
 geom_nodes(size=15,color=’darkred’)+
 geom_nodetext(aes(label=vertex.names),color=’grey80’)+
 theme_blank()




大家都在看

2017年R语言发展报告(国内)

R语言中文社区历史文章整理(作者篇)

R语言中文社区历史文章整理(类型篇)



公众号后台回复关键字即可学习

回复 R                  R语言快速入门及数据挖掘 
回复 Kaggle案例  Kaggle十大案例精讲(连载中)
回复 文本挖掘      手把手教你做文本挖掘
回复 可视化          R语言可视化在商务场景中的应用 
回复 大数据         大数据系列免费视频教程 
回复 量化投资      张丹教你如何用R语言量化投资 
回复 用户画像      京东大数据,揭秘用户画像
回复 数据挖掘     常用数据挖掘算法原理解释与应用
回复 机器学习     人工智能系列之机器学习与实践
回复 爬虫            R语言爬虫实战案例分享


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK