r - Set color for segments in ggnet -


i hvar following data set:

structure(c(2l, 6l, 2l, 6l, 7l, 7l, 2l, 7l, 6l, 8l, 8l, 4l, 8l,  2l, 9l, 8l, 7l, 6l, 9l, 1l, 9l, 4l, 9l, 3l, 2l, 10l, 9l, 10l,  8l, 10l, 7l, 6l, 10l, 1l, 2l, 12l, 9l, 8l, 12l, 1l, 11l, 10l,  2l, 44l, 79l, 10l, 8l, 47l, 45l, 51l, 9l, 11l, 74l, 75l, 77l,  69l, 75l, 77l, 78l, 2l, 44l, 44l, 46l, 46l, 8l, 6l, 1l, 1l, 6l,  7l, 1l, 4l, 7l, 8l, 8l, 1l, 4l, 8l, 3l, 8l, 8l, 9l, 9l, 9l, 1l,  9l, 5l, 9l, 3l, 9l, 9l, 9l, 10l, 8l, 10l, 7l, 10l, 10l, 1l, 10l,  10l, 9l, 12l, 12l, 1l, 12l, 12l, 12l, 12l, 7l, 7l, 44l, 44l,  44l, 44l, 44l, 44l, 44l, 44l, 44l, 44l, 7l, 7l, 7l, 7l, 44l,  10l, 9l, 42l, 43l, 46l, 46l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, -1l, 1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, -1l, 1l, 1l, 1l, -1l, -1l, 1l, 1l, 1l, 1l,  1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, -1l), .dim = c(66l,  3l), .dimnames = list(null, c("from", "to", "impact"))) 

the data set indicates, connection between , positive (1) or negative (-1) impact.

i plot network graph ggnet (or ggplot2) plot graph.

so far have done following steps:

library(network) library(ggplot2) library(ggnet) library(grid)  net <- network(df2[,c(1,2)], directed = false)  ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=f, segment.alpha = 0.5, color = "black") +   theme(legend.position = "none") +   theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm")) 

this lead result: enter image description here

i wondering how colorize edges based on impact in dataset (1 = green , -1 = red). wondering why there many unconnected nodes...

can me this?

thanks lot.

first, using ggally::ggnet available through cran. believe equivalent on github.

library(network) library(ggally) library(ggplot2) # dependencies library(grid) library(sna) library(intergraph) 

the reason have many unconnected nodes in network diagram because node names have supplied not unbroken sequence of integers. example, if supply edge between nodes named 1 , 10, network() assume presence of 8 unconnected nodes names 2:9. example,

netwk1 <- network(cbind(1,2), directed = f) get.vertex.attribute(netwk1, attrname="vertex.names") netwk2 <- network(cbind(1,10), directed = f) get.vertex.attribute(netwk2, attrname="vertex.names") 

so if convert node names unbroken sequence, lose unconnected nodes. like:

df2[,1:2]=as.numeric(as.factor(df2[,c(1,2)])) net <- network(df2[,c(1,2)], directed = f) ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, label.nodes=t, segment.alpha = 0.5, color = "black") +   theme(legend.position = "none") +   theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm")) 

you can color edges segment.color argument in ggnet() :

edge_color = ifelse(df2[,3]==-1, "red", "green") ggnet(net, mode = 'kamadakawai', size = 6, alpha = .5, segment.color=edge_color, label.nodes=t, segment.alpha = 0.5, color = "black") +   theme(legend.position = "none") +   theme(plot.margin = unit(c(0.1,0.1,0.1,0.1), "cm")) 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -