R语言学习笔记(4)

hxy    2019-06-12 17:22

R 语言绘制网络图示例:

# 绘制网络图
library(igraph)
library(gcookbook)

m <- madmen[1:nrow(madmen) %% 2 ==1, ]
g <- graph.data.frame(m, directed=FALSE)
V(g)$name

plot(g, layout=layout.fruchterman.reingold,
	vertex.size			= 4,
	vertex.lable		= V(g)$name,
	vertex.label.cex		= 0.8,
	vertex.label.dist		= 0.4,
	vertex.lable.color	= "black")
Fig. 1 带标签的网络图(igraph)

绘制有向图:

# 绘制有向图
library(igraph)
library(gcookbook)

madmen2
g <- graph.data.frame(madmen2, directed=TRUE)
par(mar=c(0,0,0,0))

plot(g, layout=layout.fruchterman.reingold,
	vertex.size			= 4,
	vertex.lable		= V(g)$name,
	vertex.label.cex		= 0.8,
	vertex.label.dist		= 0.4,
	edge.arrow.size		= 0.5,
	vertex.label.color	= NA)

# 试试圆形布局
g <- graph.data.frame(madmen2, directed=FALSE)
par(mar=c(0,0,0,0))
plot(g, layout=layout.circle,
	vertex.size			= 8,
	vertex.color		= "#3498DB",
	vertex.label		= NA)
Fig. 2 带标签的有向图
Fig. 3 带标签的圆形布局图

 

 

绘制谱系图:

# 绘制谱系图
library(gcookbook)
c2 <- subset(countries, Year==2009)
c2 <- c2[complete.cases(c2), ]
set.seed(201)
c2 < -c2[sample(1:nrow(c2),25), ]
c2

rownames(c2) <- c2$Name
c2 <- c2[,4:7]
c2

c3 <- scale(c2)
c3

hc <- hclust(dist(c3))
plot(hc)
plot(hc, hang = -1)

 

Fig. 4 谱系图

 

 

Last Modified: 2019-06-24 14:35
Views: 2.2K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment