--- title: "networks" author: "Antoine Neuraz" date: "6/7/2019" output: html_document --- # Visualisation de réseaux et graphes ```{r, include = FALSE} library(igraph) library(ggraph) library(tidygraph) nodes <- data.frame(id = c("node1","node2","node3"), #label = c("node1","node2","node3"), node_type= rep(1,3)) edges <- data.frame(from = c("node1", "node2", "node1" ), to = c("node2","node3", "node3"), type = rep("normal",3) , weight = rep(1,3)) ``` ```{r} ``` ```{r} nodes <- data.frame(id = c("node1","node2","node3"), #label = c("node1","node2","node3"), node_type= rep(1,3)) edges <- data.frame(from = c("node1", "node2", "node3" ), to = c("node2","node3", "node1"), type = rep("normal",3) , weight = c(3,1,0), color = c("s","s","h")) net <- graph_from_data_frame(d=edges, vertices = nodes, directed = TRUE) ggraph(net, layout = "linear", circular = TRUE) + geom_edge_link(arrow = grid::arrow(type = "closed", length = unit(2,"mm")), end_cap = circle(5, 'mm'), start_cap = circle(5, 'mm')) + geom_node_point(color = 'black', fill ="lightgrey", size = 10, shape = 21) + expand_limits(y=c(-1.3,1), x = c(-1,1)) + theme_void() + theme(legend.position="none") + theme(plot.margin=unit(c(1,3,1,3),"cm")) ``` ```{r} ggraph(net, layout = "linear", circular = TRUE) + geom_edge_link(aes(width = weight, color = color)) + geom_node_point(color = 'black', fill ="lightgrey", size = 10, shape = 21) + scale_edge_color_manual(values = c('s'="darkgrey", "h" = "white"))+ expand_limits(y=c(-1.3,1), x = c(-1,1)) + theme_void() + theme(legend.position="none") + theme(plot.margin=unit(c(1,3,1,3),"cm")) ``` ```{r} plot(net, edge.color = 'black', edge.width = 2, vertex.label = NA, vertex.size = 30, vertex.color = "lightgrey", vertex.frame.color="black") ``` ```{r} nodes <- data.frame(id = c("node1","node2","node3", "node4","node5","node6"), #label = c("node1","node2","node3"), node_type= c(rep(1,2), rep(2,4)), node_weight = c(30,20,rep(0,4))) edges <- data.frame(from = c("node1", "node1", "node1", "node1", "node1" ), to = c("node2","node3", "node4", "node5","node6"), type = c(rep("normal",1), rep('hidden',4)) , weight = c(3,rep(1,4))) net <- graph_from_data_frame(d=edges, vertices = nodes, directed = FALSE) V(net)$color <- "lightgrey" V(net)[node_type == 2]$color <- NA V(net)$frame.color <- "lightgrey" V(net)[node_type == 2]$frame.color <- NA V(net)["node1"]$frame.color <- 633 V(net)["node1"]$size2 <- 8 V(net)$size <- nodes$node_weight E(net)$width <- edges$weight E(net)$color <- "darkgrey" E(net)[type == "normal"]$color = 633 plot(net, vertex.label = NA, ) ``` ```{r} highschool$size <- abs(rnorm(506)) graph <- read_graph("~/Downloads/lesmiserables.gml .webarchive", "gml") fills <- V(graph)$fill %>% as.factor() %>% levels graph_mod <- as_tbl_graph(graph) %>% activate(nodes) %>% mutate(size = log(centrality_degree())+0.5) %>% arrange(desc(group),desc(size)) graph_mod %>% ggraph("linear") + geom_edge_arc(aes(width = value),color = hsv(0, 0, 0.2, 0.25), fold= TRUE) + geom_node_point(aes(fill = fill, size =size), shape=21 ) + theme(legend.position="none") + scale_fill_manual(values = fills) + geom_node_text(aes(label = label), angle = 90, hjust=1, size = 2.5, nudge_y = -1) + expand_limits(y=c(-10,NA)) + theme_void() + theme(legend.position="none") ``` ```{r} graph_mod %>% ggraph('kk') + geom_edge_link(aes(width = value),color = hsv(0, 0, 0.2, 0.25), fold= TRUE) + geom_node_point(aes(fill = fill, size =size), shape=21 ) + theme(legend.position="none") + scale_fill_manual(values = fills) + geom_node_text(aes(label = label), repel = TRUE, size = 2) + expand_limits(y=c(-10,NA)) + theme_void() + theme(legend.position="none") ```