0

I am building a network in R. My .csv input file is a list with several columns and many rows. The first column contains vertices from which edges are going to other vertices. The second column contains vertices to which the edges from the vertices in the first column are going. Since the vertices listed in the first column may have several edges going to different vertices, the vertices in the first column contain many duplicates. However, in the network they will only be displayed once. See below for an example.

From_Interviewee To_Actor
A B
A C
A D
A E
A F

Now I want to add colors to the vertices listed in both, the first and the second column of my input file. The code that I have written does not seem to understand that there are duplicates in my data and that, because of that, the unique number of vertices in my network is different than the number of entries in my list. Hence, I am getting the error message that number of items to replace is not a multiple of replacement length.

# Read data file
actor_data <- read.csv(file.choose(), sep=";", header=T, na.strings = "NA")
actor_data_df <- data.frame(actor_data$From_Interviewee, actor_data$To_Actor, actor_data$Link_Weight, actor_data$Link_Category, actor_data$Link_Color, actor_data$Link_Directionality, actor_data$Interviewee_Role, actor_data$Interviewee_Color, actor_data$Actor_Role, actor_data$Actor_Color, actor_data$Actor_Category, actor_data$ID)

# Create subset to play around with data
actor_data_df_AR <- actor_data_df %>% filter(actor_data.From_Interviewee %in% c("Anthony_Recco"))

# Create undirected network
actor_net <- graph_from_data_frame(actor_data_df_AR, directed=FALSE)
plot(actor_net)

# Add edge colors
colors_e <- actor_data_df_AR$actor_data.Link_Color
E(actor_net)$color <- colors_e
plot(actor_net)

# Add edge weights
plot(actor_net,
     edge.width=actor_data_df_AR$actor_data.Link_Weight)

# Add vertice colors
colors_v <- cbind(actor_data_df_AR$actor_data.Interviewee_Color, actor_data_df_AR$actor_data.Actor_Color)
V(actor_net)$color <- colors_v
plot(actor_net,
edge.width=actor_data_df_AR$actor_data.Link_Weight)

The code snippet #Add vertice colors does not work yet because the vertices in the graph are not a vector of length equal to the number of vertices. That is because in the column Interviewee_Color the same vertices are listed several times but in the network they only appear once. Hence, colors are assigned, but they are not always correct.

How can I solve this issue?

The warning message that I am getting is this:

Warning message: In vattrs[[name]][index] <- value :   number of items to replace is not a multiple of replacement length

See above for a detailed description.

  • Take a look at this question. https://stackoverflow.com/questions/74967062/igraph-error-in-r-with-vertex-size-longer-object-length-is-not-a-multiple-of-s, I believe this explains your error and provides some suggestions. – Dave2e Jan 09 '23 at 22:53

1 Answers1

0

The problem is

actor_net <- graph_from_data_frame(actor_data_df_AR, directed=FALSE)

This leaves the definition of the vertices to graph_from_data_frame(), which is different then you would expect.

Therefore, try:

graph_from_data_frame(actor_data_df_AR, directed=FALSE, vertices = actor_data_df)

The documentation >help(graph_from_data_frame) shows enlightening examples. Hope this helps.

clp
  • 1,098
  • 5
  • 11