Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. ---
  2. title: "R Notebook"
  3. output: html_notebook
  4. ---
  5.  
  6. ```{r}
  7. library(igraph)
  8. ```
  9.  
  10. ```{r}
  11. m = matrix(0, nrow=3, ncol=3)
  12. m
  13. ```
  14. ```{r}
  15. dim(m)
  16. ```
  17.  
  18. ```{r}
  19. m[1,2] = 1
  20. m[2,3] = 1
  21. m[3,1] = 1
  22. m
  23. ```
  24.  
  25. ```{r}
  26. m = matrix(c(0,1,0,0,0,1,1,0,0), nrow=3, byrow=T)
  27. m
  28. ```
  29.  
  30. ```{r}
  31. nodes = c('A','B','C')
  32. rownames(m) = nodes
  33. colnames(m) = nodes
  34. m
  35. ```
  36. ```{r}
  37. g = graph_from_adjacency_matrix(m,mode = "undirected")
  38. V(g)
  39. ```
  40. ```{r}
  41. V(g)$color = 'red'
  42. V(g)$size = 40
  43. V(g)$size2 = 50
  44. V(g)$shape = 'circle'
  45. E(g)$weight = c(1,2,3)
  46. E(g)$label = E(g)$weight
  47. V(g)[1]$color = 'yellow'
  48. plot(g, edge.width = E(g)$weight, layout=layout.circle)
  49. ```
  50. ```{r}
  51. g2 = graph(edges = c(1,2, 2,3, 3,4, 4,1))
  52. V(g2)[1]$label = 'A very long name'
  53. V(g2)[2]$label = 'Another very long name'
  54. V(g2)[3]$label = 'short name'
  55. V(g2)[c(1,2)]$color = 'green'
  56. plot(g2)
  57. ```
  58. ```{r}
  59. g=graph(n=12,edges=c(1,2))
  60. l = matrix(c(3,6,1,5,3,5,4,5,2,4,4,4,1,3,3,3,3,2,1,1,3,1,5,1), nrow=12, byrow=T)
  61. l
  62. ```
  63. ```{r}
  64. V(g)$size = 30
  65. plot(g, layout=l)
  66. ```
  67.  
  68. ```{r}
  69. g3 = graph(edge = c('A','B', 'B','C', 'C','A') )
  70. plot(g3)
  71. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement