g <- graph(c(2,1,3,1,4,1,5,1,6,1,7,1),directed = F)
par(mfrow=c(2,3))
plot(g,vertex.size=40,layout=layout_on_grid,main="简单网络布局")
plot(g,vertex.size=40,layout=layout.auto,main="自动布局")
plot(g,vertex.size=40,layout=layout_as_star,main="星型布局")
plot(g,vertex.size=40,layout=layout.circle,main="环形布局")
plot(g,vertex.size=40,layout=layout_randomly,main="随机布局")
plot(g,vertex.size=40,layout=layout_as_tree(g),main="树状布局")
library(RColorBrewer)
weight<-seq(1,3,length.out = ecount(g))
V(g)$color <- brewer.pal(9,"Set1")[1:vcount(g)]#点的填充色
V(g)$frame.color <- NA# 点边框
V(g)$label.color <- "white"
V(g)$label.font <- 2
V(g)$label.cex <- 2
E(g)$width <- weight
E(g)$color <- "steelblue4"
E(g)$lty <- 2
plot(g,main="对网络图进行美化")
tkplot()
rglplot()
data(karate)
fc1 <- multilevel.community(karate)
fc2 <- edge.betweenness.community(karate)
fc3 <- walktrap.community(karate)
fc4 <- infomap.community(karate)
fc5 <- fastgreedy.community(karate)
fc6 <- label.propagation.community(karate)
fc_list <- list(fc1,fc2,fc3,fc4,fc5,fc6)
algorithm_list <- c("multilevel","edge.betweenness","walktrap",
"infomap","fastgreedy","label.propagation")
par(mfrow=c(2,3),mar=c(1,1,2,1))
for (i in 1:6) {
plot(fc_list[[i]],karate,main=algorithm_list[[i]])
}
Links <- read.csv("Links.csv")
Nodes <- read.csv("Nodes.csv")
g1 <- graph.data.frame(Links,directed=T,vertices = Nodes)
g1
V(g1)$size <- 10
V(g1)$color <- Nodes$Group
V(g1)$label <- as.character(Nodes$Label)
V(g1)$label.cex <- 0.7
E(g1)$width <- (Links$Weight-min(Links$Weight))*5/(max(Links$Weight)-min(Links$Weight))
E(g1)$arrow.size <- 0.5
plot(g1,main="用户社会网络图")
#移除某个用户
remove.u <- "user12"
remove.v <- which(V(g1)$Label!=remove.u)
g1.new <- induced.subgraph(g1,remove.v)
plot(g1.new,vertex.size=10,vertex.color =Nodes[-which(Nodes$Label==remove.u),"Group"],
vertex.label=as.character(setdiff(Nodes$Label,remove.u)),
vertex.label.ces =0.7,
main=paste0("移除",remove.u,"用户后的网络图"))
Gephi软件