Você está na página 1de 10

Rede-Exemplo-1

L A Santos
7 de outubro de 2019

Exemplos de Criação de Redes


SE NÃO TENHO OS PESOS DA REDE,
POSSO USAR O GRAU DA REDE COMO
PESOS
instalar e carregar pacotes R - Carregar
sempre, instalar só uma vez
install.packages(“igraph”)
library(igraph)

##
## Attaching package: 'igraph'

## The following objects are masked from 'package:stats':


##
## decompose, spectrum

## The following object is masked from 'package:base':


##
## union

rede g1
g1 <- graph(edges = c(1,2,2,3,3,4,4,1))
plot(g1)
matriz de adjacencia g1
g1[ ]

## 4 x 4 sparse Matrix of class "dgCMatrix"


##
## [1,] . 1 . .
## [2,] . . 1 .
## [3,] . . . 1
## [4,] 1 . . .

rede g2
g2<- graph(edges = c(1,2,3,2,3,4,2,4,3,5,4,5,5,1))
plot(g2)
matriz adjacencia g2
g2[ ]

## 5 x 5 sparse Matrix of class "dgCMatrix"


##
## [1,] . 1 . . .
## [2,] . . . 1 .
## [3,] . 1 . 1 1
## [4,] . . . . 1
## [5,] 1 . . . .

calculando o grau da rede, de entrada e de


saida
degree(g2, mode="all")

## [1] 2 3 3 3 3

degree(g2, mode="in")
## [1] 1 2 0 2 2

degree(g2, mode="out")

## [1] 1 1 3 1 1

plotando o grau da rede


grau<-degree(g2, mode="all")
grau

## [1] 2 3 3 3 3

plot(g2, vertex.size =grau)


aumentando o tamanho do ponto/no - grau da
rede
grau<-degree(g2, mode="all")
grau<- 7*grau

plot(g2, vertex.size =grau)

calculando o diametro da rede


diameter(g2)

## [1] 3

get_diameter(g2)

## + 4/5 vertices, from f84589e:


## [1] 1 2 4 5
rede com texto g3
g3<- graph(edges = c("X1","A", "X2","A", "X2","B","A","B","X
2","C","B","C","C","X1"))
plot(g3)

matriz de adjacencia g3
g3[ ]

## 5 x 5 sparse Matrix of class "dgCMatrix"


## X1 A X2 B C
## X1 . 1 . . .
## A . . . 1 .
## X2 . 1 . 1 1
## B . . . . 1
## C 1 . . . .

outra rede com texto g4 - sem peso


g4<-graph(edges = c("A", "B", "A", "C", "B", "A", "B", "C", "C", "A", "C",
"B"), directed = F)
plot(g4)
melhorando a saida da rede g4 - colocando
pesos
V(g4)$Peso = c(30, 15, 15)

vertex_attr(g4)

## $name
## [1] "A" "B" "C"
##
## $Peso
## [1] 30 15 15

E(g4)$weight = c(3, 4, 1, 1, 7, 1)

vertex_attr(g4)$Cor = c("Red", "Green", "Blue")

plot(g4, vertex.size=vertex_attr(g4)$Peso, edge.width = edge_attr(g4)$weigh


t, vertex.color=vertex_attr(g4)$Cor, main="Fluxo de Rede")
grau da rede
degree(g4)

## A B C
## 4 4 4

exemplo final rede g2


g2<- graph_from_literal(1-+2,2-+3,3-+4,4-+1)
plot(g2)
preparando a rede g2
V(g2)$Peso = c(50, 30, 16, 60)

a<- vertex_attr(g2)$Peso
a

## [1] 50 30 16 60

E(g2)$Declividade = c(2, -1, -5, 3)

b<-edge_attr(g2)$Declividade
b

## [1] 2 -1 -5 3
E(g2)$weight = c(2, 2, 5, 4)

c<-edge_attr(g2)$weight

V(g2)$Cor = c("Red", "Green", "yellow", "Blue")

d<- vertex_attr(g2)$Cor
d

## [1] "Red" "Green" "yellow" "Blue"

E(g2)$Cor = c(c("Green", "yellow", "Red", "Blue"))

e<- edge_attr(g2)$Cor
e

## [1] "Green" "yellow" "Red" "Blue"

plotando novamente a rede g2


plot(g2, vertex.size = a, edge.width = c, vertex.color = d, edge.color = e,
edge.curved = .4, main = "Rede", vertex.shape= "square")

Você também pode gostar