Wednesday, December 2, 2015

Visual 7 - Network Graph

conv <- sqlQuery(con, "select * from lh_tmp2 where random() < .05;", as.is = T);

tmp <- ddply(conv, .(userid), nrow);
conv <- merge(conv, tmp, by = 'userid');
conv <- conv[sort.list(conv[,3]),]

nconv <- length(unique(conv$userid));
nfact <- length(unique(conv$factid));

# if user --> positive else --> negative;
conv$userno <- as.numeric(as.factor(conv$userid));
conv$factno <- -as.numeric(as.factor(conv$factid));
m <- dim(conv)[2];

conv.list <- unique(conv$userno);

g <- graph.data.frame(conv[, (m-1):m], directed=FALSE)
indx <- as.numeric(V(g)$name)
colorlist <- c(rep("red", length(indx)));

for (j in 1: length(indx)){
  if (indx[j]<0) {
    cat1 <- unique(conv$cat1[conv$factno == indx[j]]);
    if (cat1 == "Behavioral") colorlist[j] <- "purple";
    if (cat1 == "Search") colorlist[j] <- "green";
    if (cat1 == "Secondary Sources") colorlist[j] <- "cyan";
    if (cat1 == "Site Visitation") colorlist[j] <- "cornflowerblue";
    if (cat1 == "Social") colorlist[j] <- "orange";
  }
}
V(g)$color <- colorlist
plot(g, vertex.size=2.5, vertex.label = NA, vertex.label.dist=0.25, edge.width=1, layout= layout.fruchterman.reingold)
plot(g, vertex.size=2.5, vertex.label = NA, vertex.label.dist=0.25, edge.width=1, layout= layout.graphopt)
plot(g, vertex.size=2.5, vertex.label = NA, vertex.label.dist=0.25, edge.width=1, layout= layout.kamada.kawai)

plot(g, layout=layout.circle)
# Force directed layouts
plot(g, layout=layout.fruchterman.reingold)
plot(g, layout=layout.graphopt)
plot(g, layout=layout.kamada.kawai)

No comments:

Post a Comment

Blog Archive