You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 5 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ---
  2. title: "networks"
  3. author: "Antoine Neuraz"
  4. date: "6/7/2019"
  5. output: html_document
  6. ---
  7. # Visualisation de réseaux et graphes
  8. ```{r, include = FALSE}
  9. library(igraph)
  10. library(ggraph)
  11. library(tidygraph)
  12. nodes <- data.frame(id = c("node1","node2","node3"),
  13. #label = c("node1","node2","node3"),
  14. node_type= rep(1,3))
  15. edges <- data.frame(from = c("node1", "node2", "node1" ),
  16. to = c("node2","node3", "node3"),
  17. type = rep("normal",3) ,
  18. weight = rep(1,3))
  19. ```
  20. ```{r}
  21. ```
  22. ```{r}
  23. nodes <- data.frame(id = c("node1","node2","node3"),
  24. #label = c("node1","node2","node3"),
  25. node_type= rep(1,3))
  26. edges <- data.frame(from = c("node1", "node2", "node3" ),
  27. to = c("node2","node3", "node1"),
  28. type = rep("normal",3) ,
  29. weight = c(3,1,0),
  30. color = c("s","s","h"))
  31. net <- graph_from_data_frame(d=edges, vertices = nodes, directed = TRUE)
  32. ggraph(net, layout = "linear", circular = TRUE) +
  33. geom_edge_link(arrow = grid::arrow(type = "closed", length = unit(2,"mm")),
  34. end_cap = circle(5, 'mm'),
  35. start_cap = circle(5, 'mm')) +
  36. geom_node_point(color = 'black', fill ="lightgrey", size = 10, shape = 21) +
  37. expand_limits(y=c(-1.3,1), x = c(-1,1)) +
  38. theme_void() +
  39. theme(legend.position="none") +
  40. theme(plot.margin=unit(c(1,3,1,3),"cm"))
  41. ```
  42. ```{r}
  43. ggraph(net, layout = "linear", circular = TRUE) +
  44. geom_edge_link(aes(width = weight, color = color)) +
  45. geom_node_point(color = 'black', fill ="lightgrey", size = 10, shape = 21) +
  46. scale_edge_color_manual(values = c('s'="darkgrey", "h" = "white"))+
  47. expand_limits(y=c(-1.3,1), x = c(-1,1)) +
  48. theme_void() +
  49. theme(legend.position="none") +
  50. theme(plot.margin=unit(c(1,3,1,3),"cm"))
  51. ```
  52. ```{r}
  53. plot(net,
  54. edge.color = 'black',
  55. edge.width = 2,
  56. vertex.label = NA,
  57. vertex.size = 30,
  58. vertex.color = "lightgrey",
  59. vertex.frame.color="black")
  60. ```
  61. ```{r}
  62. nodes <- data.frame(id = c("node1","node2","node3", "node4","node5","node6"),
  63. #label = c("node1","node2","node3"),
  64. node_type= c(rep(1,2), rep(2,4)),
  65. node_weight = c(30,20,rep(0,4)))
  66. edges <- data.frame(from = c("node1", "node1", "node1", "node1", "node1" ),
  67. to = c("node2","node3", "node4", "node5","node6"),
  68. type = c(rep("normal",1), rep('hidden',4)) ,
  69. weight = c(3,rep(1,4)))
  70. net <- graph_from_data_frame(d=edges, vertices = nodes, directed = FALSE)
  71. V(net)$color <- "lightgrey"
  72. V(net)[node_type == 2]$color <- NA
  73. V(net)$frame.color <- "lightgrey"
  74. V(net)[node_type == 2]$frame.color <- NA
  75. V(net)["node1"]$frame.color <- 633
  76. V(net)["node1"]$size2 <- 8
  77. V(net)$size <- nodes$node_weight
  78. E(net)$width <- edges$weight
  79. E(net)$color <- "darkgrey"
  80. E(net)[type == "normal"]$color = 633
  81. plot(net,
  82. vertex.label = NA,
  83. )
  84. ```
  85. ```{r}
  86. highschool$size <- abs(rnorm(506))
  87. graph <- read_graph("~/Downloads/lesmiserables.gml .webarchive", "gml")
  88. fills <- V(graph)$fill %>%
  89. as.factor() %>%
  90. levels
  91. graph_mod <- as_tbl_graph(graph) %>%
  92. activate(nodes) %>%
  93. mutate(size = log(centrality_degree())+0.5) %>%
  94. arrange(desc(group),desc(size))
  95. graph_mod %>%
  96. ggraph("linear") +
  97. geom_edge_arc(aes(width = value),color = hsv(0, 0, 0.2, 0.25), fold= TRUE) +
  98. geom_node_point(aes(fill = fill, size =size), shape=21 ) + theme(legend.position="none") +
  99. scale_fill_manual(values = fills) +
  100. geom_node_text(aes(label = label), angle = 90, hjust=1, size = 2.5, nudge_y = -1) +
  101. expand_limits(y=c(-10,NA)) +
  102. theme_void() + theme(legend.position="none")
  103. ```
  104. ```{r}
  105. graph_mod %>%
  106. ggraph('kk') +
  107. geom_edge_link(aes(width = value),color = hsv(0, 0, 0.2, 0.25), fold= TRUE) +
  108. geom_node_point(aes(fill = fill, size =size), shape=21 ) + theme(legend.position="none") +
  109. scale_fill_manual(values = fills) +
  110. geom_node_text(aes(label = label), repel = TRUE, size = 2) +
  111. expand_limits(y=c(-10,NA)) +
  112. theme_void() + theme(legend.position="none")
  113. ```