Download R source file
rm(list = ls())
gc()
library(dplyr);
library(readr);
library(magrittr)
library(dplyr)
library(tidyr)
setwd("C:/SNH18wd")
AHS_Base <- read_csv ('C:/SNH18wd/ahs_wpvar.csv',
col_names = TRUE);
AHS_adjlist <- AHS_Base %>%
select(ego_nid, mfnid_1:mfnid_5, ffnid_1:ffnid_5, grade, sex, commcnt) %>%
filter(commcnt==1);
AHS_Edges <- AHS_adjlist %>%
rename( id = `ego_nid`,
gender = `sex`) %>%
gather(Alter_Label, Target, mfnid_1:mfnid_5, ffnid_1:ffnid_5, na.rm = TRUE)
AHS_Edges=AHS_Edges %>% filter (Target != 99999);
AHS_Edges=AHS_Edges %>%select(id, Target);
library(sna)
library(GGally)
library(ggplot2)
library(network)
g=as.network(AHS_Edges)
g %v% "grade" <- AHS_adjlist$grade
g %v% "sex" <- AHS_adjlist$sex
g %v% "degree" <- degree(g)
plot.network(g,vertex.col="grade",
vertex.cex="degree",
jitter=T)
g %v% "logdegree" <- log(degree(g)+1)
plot.network(g,vertex.col="grade",
vertex.cex="logdegree",
jitter=T)
library(networkD3)
gn=data.frame(NodeID=as.numeric(AHS_adjlist$ego_nid-1),Nodesize=(degree(g)))
ge=data.frame(AHS_Edges-1)
gn$group <- AHS_adjlist$grade
forceNetwork(Links=ge, Nodes = gn,
Source = "id", Target = "Target", Group="group",
Nodesize = "Nodesize", NodeID = "NodeID",
opacity = 0.9, bounded=FALSE, opacityNoHover=.2)
library(igraph)
gi<- graph_from_data_frame(ge, directed=TRUE, vertices=gn)
plot(gi)
plot(gi, edge.arrow.size=.2,vertex.label=NA)
plot(gi, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_fr)
imc<-cluster_infomap(gi)
membership(imc)
plot(gi, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_fr,
vertex.color=imc$membership)
plot(gi, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_fr,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"))
plot(gi, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_kk,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"))
plot(gi, edge.arrow.size=.2,vertex.label=NA,
layout=layout_with_kk,
vertex.color=gn$group,
vertex.size=degree(gi,mode = "in"))
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_with_kk,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_nicely,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_with_mds,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_in_circle,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_randomly,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_on_sphere,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
layouts <- grep("^layout_", ls("package:igraph"), value=TRUE)[-1]
layouts
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_with_graphopt,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
plot(gi, edge.arrow.size=.1,vertex.label=NA,
layout=layout_with_dh,
vertex.color=imc$membership,
vertex.size=degree(gi,mode = "in"),
edge.curved=.2)
detach("package:igraph", unload=TRUE)
plot.network(g,vertex.col="grade",
vertex.cex="logdegree",
arrowhead.cex = .5,
jitter=T)
?plot.network
library('visNetwork')
??visNetwork
library(plyr)
links <- rename(ge,c("id" = "from", "Target" = "to"))
nodes <- rename(gn,c('NodeID'="id"))
visNetwork(nodes, links, width="100%", height="400px",main="Network!")
nodes$shape <- "dot"
nodes$shadow <- TRUE
nodes$size <- gn$Nodesize
nodes$borderWidth <- .5
nodes$color.background <- c("slategrey", "tomato", "gold", "red", "blue", "green","lightgray","lavender")[imc$membership]
nodes$color.border <- "black"
nodes$color.highlight.background <- "orange"
nodes$color.highlight.border <- "darkred"
visNetwork(nodes, links)