---
title: "ExploratoryDataAnalysis_with_sampleDataset"
author: "Marcel Mallow"
date: "2023-10-20"
output: html_document
---

Picking up from last lab's saved network matrix relations...


### Next session Importing your saved network matrices and doing a very first exploratory analysis

```{r}

library(igraph) # load the igraph package for later 

#loading our matrices
load (paste0("~/NetworkSurvey/", "Friendship_q149", ".Rdata"))
load (paste0("~/NetworkSurvey/", "Influence_q128", ".Rdata"))

#as.igraph(QID149)

friendship_network <- graph_from_adjacency_matrix(QID149_mat)
influence_network <- graph_from_adjacency_matrix(QID128_mat)


# Comparing network metrics such as density
igraph::graph.density(friendship_network)
igraph::graph.density(influence_network)

## Listing all available shapes 
# shapes()

#plot(friendship_network)


V(friendship_network)$vertex.shape <- "square"
# V(friendship_network)$name <- c("bli", "bla", "blubbi", "shrubby", "puppy", "muppy")


V(friendship_network)$name <- responses$QID169_1
V(influence_network)$name <- responses$QID169_1



plot(friendship_network) # wow, this is a mess!

# better without vertex labels?
plot(friendship_network
     ,vertex.size = 15
     ,vertex.color = "lightblue"
     ,vertex.label=NA
     )

#Interesting to see our friendship network in text form
E(friendship_network)


#Edge lists are, apart from adjacency matrices a very common way of building networks
friendship_EL <- as_edgelist(friendship_network)
influence_EL <- as_edgelist(influence_network)

```


```{r}
multiplexNetwork <- graph_from_edgelist(friendship_EL)

E(multiplexNetwork)$type <- "friendship"

E(multiplexNetwork)$type
# multiplexNetwork

multiplexNetwork_V2 <- add.edges(multiplexNetwork, edges = influence_EL)

E(multiplexNetwork_V2)$type[38:56] <- "influence"
E(multiplexNetwork_V2)$type


E(multiplexNetwork_V2)$width <- 2
plot(multiplexNetwork_V2, edge.color=c("dark red", "slategrey")[(E(multiplexNetwork_V2)$type=="friendship")+1],
      vertex.color="gray40", layout=layout_in_circle, edge.curved=.3)



```

```{r Lets simplify}

#getting rid of the self-loops

multiplexNetwork_V2 <- igraph::simplify(multiplexNetwork_V2, remove.loops = T, remove.multiple = F)

E(multiplexNetwork_V2)$width <- 2
plot(multiplexNetwork_V2, edge.color=c("dark red", "slategrey")[(E(multiplexNetwork_V2)$type=="friendship")+1],
      vertex.color="gray40", layout=layout_in_circle, edge.curved=.3)

```

