0

I am trying to create a social network for the geese I am studying. I have already been given working code by my superiors on the project however, as someone who is not very good at R and has no previous experience conducting a social network analysis, I am unsure what the errors mean that are coming up. To further this I cannot figure out whether the problem lies further up in the script where I have input the data wrong or whether the error can be identified here.

Here is the code I input, the output and the error message Backtraced:

 G1 <- Bird_assoc_symmat %>%
+   graph_from_adjacency_matrix(weighted = T, mode = "undirected") %>%
+   as_tbl_graph()
> G1 # Nodes seem to be correct??
# A tbl_graph: 280 nodes and 0 edges
#
# An unrooted forest with 280 trees
#
# A tibble: 280 × 1
  name 
  <chr>
1 9AWR 
2 3ZWR 
3 J4NW 
4 BHYG 
5 ICNW 
6 U9WR 
# ℹ 274 more rows
# ℹ Use `print(n = ...)` to see more rows
#
# A tibble: 0 × 2
# ℹ 2 variables: from <int>, to <int>
> Layout<-layout_with_fr(G1)
> G1_net<-ggraph(G1,layout=Layout)+geom_edge_link0(aes(edge_width=weight),alpha=0.15)+
+   scale_edge_width(range=c(0.2,3))+geom_node_point()
> G1_net ##   "Weight not found"
Error in `geom_edge_link0()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'weight' not found
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/rlang_error>
Error in `geom_edge_link0()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'weight' not found
---
Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. └─ggplot2:::print.ggplot(x)
  3.   ├─ggplot2::ggplot_build(x)
  4.   ├─ggraph:::ggplot_build.ggraph(x)
  5.   ├─base::NextMethod()
  6.   └─ggplot2:::ggplot_build.ggplot(x)
  7.     └─ggplot2:::by_layer(...)
  8.       ├─rlang::try_fetch(...)
  9.       │ ├─base::tryCatch(...)
 10.       │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 11.       │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 12.       │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 13.       │ └─base::withCallingHandlers(...)
 14.       └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 15.         └─l$compute_aesthetics(d, plot)
 16.           └─ggplot2 (local) compute_aesthetics(..., self = self)
 17.             └─base::lapply(aesthetics, eval_tidy, data = data, env = env)
 18.               └─rlang (local) FUN(X[[i]], ...)
Run rlang::last_trace(drop = FALSE) to see 5 hidden fram

Thanks.

UPDATE: I believe that the issue I am having is from the start of my code resulting in my Social Network Analysis to have no edges.

I believe the problem lies here:

Bird_list<-unique(May_resightings$Ring_ID)
Bird_matrix<-matrix(0,nrow=length(Bird_list),ncol=length(Bird_list))
rownames(Bird_matrix)<-Bird_list
colnames(Bird_matrix)<-Bird_list
Bird_matrix[1:10,1:10]
Bird_melt<-melt(Bird_matrix)

May_resightings$row_num<-rownames(May_resightings) #### I restarted R
summary(May_resightings$Time)
head(May_resightings)
May_resightings$Time<-parse_time(May_resightings$Time,format="%H:%M") 
May_resightings$Time<-as.period(May_resightings$Time)
summary(May_resightings)

colnames(May_resightings)
for(i in 1:length(unique(May_resightings$Ring_ID)))
  temp<-subset(May_resightings,Ring_ID==Bird_list[i])
temp<-temp[,c(2,3,4,24)] ##An error "undefined columns selected", they don't exist? 

summary(temp) #??????????
Axeman
  • 32,068
  • 8
  • 81
  • 94

0 Answers0