library(readxl) library(lubridate) library(dplyr) library(tidyr) # Pacotes utilizados para analise de redes: library(igraph) library(networkD3) library(htmlwidgets) library(rgexf) # Pacotes utilizados para= Rmarkdown library(knitr) library(kableExtra) output = "analise.html" ############### Aquisicao de dados ############################################################### dados.n <- read_excel("dadosPNAE.xlsx",sheet = "nos") dados.n <- as.data.frame(dados.n) head(dados.n) dados.a <- read_excel("dadosPNAE.xlsx",sheet = "arestas") dados.a <- as.data.frame(dados.a) head(dados.a) #FAZENDO A REDE no pacote igraph # Transformando data.frames em redes -------------------------------- # Grafos ================================================== grafo <- graph.data.frame(dados.a, directed = TRUE, vertices = dados.n) summary(grafo) colrs <- adjustcolor( c("tomato","lightsteelblue2"), alpha=.6) # Verificando caracteristicas dos vertices -------------------------- V(grafo) V(grafo)$Label V(grafo)$label <- V(grafo)$Label V(grafo)$cor <- colrs[V(grafo)$cat_num] V(grafo)$cor # Grau de cada vertice - - - - - - - - - - - - - - - - - - - - - - - V(grafo)$grau <- degree(grafo) V(grafo)$grau # Verificando caracteristicas das arestas --------------------------- E(grafo) E(grafo)$width <- E(grafo)$weight # Preparacao de Layouts # Fruchterman-Reingold is one of the most # used force-directed layout algorithms out there set.seed(567) L_FruchtR <- layout_with_fr(grafo) # Another popular force-directed algorithm # that produces nice results for connected # graphs is Kamada Kawai. L_KamadaKawai <- layout_with_kk(grafo) # Layout layout_nicely L_Nicely <- layout_nicely(grafo) # Plots # Fruchterman-Reingold: plot(grafo, rescale=T,layout=L_FruchtR, edge.arrow.size=0.2, vertex.color=V(grafo)$cor, vertex.frame.color="black", vertex.size = V(grafo)$grau, vertex.label.dist = 0, vertex.label.color = 'gray30', vertex.label.cex = 0.9) # Kamada Kawai plot(grafo, rescale=T,layout=L_KamadaKawai, edge.arrow.size=0.2, vertex.color=V(grafo)$cor, vertex.frame.color="black", vertex.size = V(grafo)$grau, vertex.label.dist = 0, vertex.label.color = 'gray30', vertex.label.cex = 0.9) # Nicely plot(grafo, rescale=T,layout=L_Nicely, edge.arrow.size=0.2, vertex.color=V(grafo)$cor, vertex.frame.color="black", vertex.size = V(grafo)$grau, vertex.label.dist = 0, vertex.label.color = 'gray30', vertex.label.cex = 0.9) # Para gravar num pdf - - - - - - - - - - - - - - - \ pdf("plot_nicely.pdf",20,20) igraph.options(plot.layout=L_Nicely, rescale=T, edge.arrow.size=0.3, vertex.color=V(grafo)$cor, vertex.frame.color="black", vertex.size = V(grafo)$grau, vertex.label.dist = 0, vertex.label.color = 'gray30', vertex.label.cex = 0.8) plot(grafo) dev.off() # - - - - - - - - - - - - - - - - - - - - - - - - - / # - - - - - - - - - - - - - - - - - - - - - - - - - / ################################################## # NetworkD3: Fazendo o layou da rede no formato D3 # para apresentação em web (carregar o pacore networkD3) #D3 wc <- cluster_walktrap(grafo) members <- membership(wc) # converte do igraph para networkD3 (group poderia ser # regiões e fornecedores) grafo_d3 <- igraph_to_networkD3(grafo, group = members) # Cria labels para os nós, porque na conversão ficou # apenas como os ids grafo_d3$nodes$label <- dados.n$Label # Apresenta a rede no RStudio forceNetwork(Links = grafo_d3$links, Nodes = grafo_d3$nodes, Source = 'source', Target = 'target', NodeID = 'label', Group = 'group',opacity = 1.5,zoom=TRUE, opacityNoHover = 1.0) # Cria página html com a rede - - - - - - - - - - - - - - - - - - - - - rede_d3 <- forceNetwork(Links = grafo_d3$links, Nodes = grafo_d3$nodes, Source = 'source', Target = 'target', NodeID = 'label',Group = 'group',opacity = 1.2,zoom=TRUE, opacityNoHover = 1.0) # save the widget saveWidget(rede_d3, file=paste0( getwd(), "/grafo.html")) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ############### Metricas de rede # Densidade densidade <- edge_density(grafo, loops=F) densidade # Diametro diam <- get_diameter(grafo, directed=F) # Grau medio grau <- degree(grafo,mode="all") graum <- mean(grau) graum # Hubs autoridades hs <- hub_score(grafo, weights=NA)$vector hs as <- authority_score(grafo, weights=NA)$vector as # -------------------------------------------------- # Construindo o arquivo html com R Markdown -------- # rmarkdown::render('analise.Rmd', output_file = output, #params = params, envir = new.env(parent = globalenv())) #------------------------------------------------