Astetr
Astetr

Reputation: 79

Setting specific nodes and edges to create a graph network

I have this dataset:

article1    article2    article3
R1  R1  R20
R2  R13 R5
R3  R7  R21
R4  R8  R22
R5  R14 R8
R11 R15 R23
R12 R9  R24
R6  R10 R25
    R2  R10
    R6  R9
    R16 R26
    R17 R27
    R4  R28
    R18 R29
    R19 R3
        R30
        R31
        R1
        R32
        R7
        R33

I would like to create a graph network using igraph and have as nodes articles1,2,3 and as edges R1,2,...

How is it possible to set the nodes and edges in igraph and produce a graph.

I have calculated the frequency of every R in dataset and here is the list:

R   frequency
R1  3
R2  2
R3  2
R4  2
R5  2
R6  2
R7  2
R8  2
R9  2
R10 2
R11 1
R12 1
R13 1
R14 1
R15 1
R16 1
R17 1
R18 1
R19 1
R20 1
R21 1
R22 1
R23 1
R24 1
R25 1
R26 1
R27 1
R28 1
R29 1
R30 1
R31 1
R32 1
R33 1

Upvotes: 1

Views: 104

Answers (1)

zx8754
zx8754

Reputation: 56119

Try this example:

library(dplyr)
library(tidyr)
library(igraph)

graphDat <- df1 %>% 
  gather(key = "From", "To", na.rm = TRUE) %>% 
  graph_from_data_frame()

plot(graphDat)

enter image description here

Here is the great resource for more network plot options.

data

df1 <- read.table(text = "article1  article2    article3
R1  R1  R20
                  R2    R13 R5
                  R3    R7  R21
                  R4    R8  R22
                  R5    R14 R8
                  R11   R15 R23
                  R12   R9  R24
                  R6    R10 R25
                  NA    R2  R10
                  NA    R6  R9
                  NA    R16 R26
                  NA    R17 R27
                  NA    R4  R28
                  NA    R18 R29
                  NA    R19 R3
                  NA    R30 NA
                  NA    R31 NA
                  NA    R1  NA
                  NA    R32 NA
                  NA    R7  NA
                  NA    R33 NA
                  ", header = TRUE, stringsAsFactors = FALSE)

Upvotes: 2

Related Questions