I have no experience using R so apologies if this is a stupid question but I am on a tight deadline and have not been able to understand a clear answer from my own research.
I've just created a network graph and have changed some of the attributes, but others I can't get working:
How do I combine multiple arrows leading into one node into one higher-weighted arrow?
Why are the edges not changing to black, or any other colour that I try?
Is there a way for me to space out the nodes more so that they aren't all on top of each other around the centre, while 80% of the graph space isn't being used?
I've attached my code below, if you could implement the changes into the code exactly as I should write it to work then that would be very much appreciated.
library("igraph")
library("networkD3")
library("tidyverse")
library("igraph")
library("ggraph")
my_data <- read.csv("email-Eu-core-temporal-Dept3-CSV2.csv", header=TRUE)
my_network_frame <- data.frame(my_data$source_node, my_data$target_node)
my_network <- graph.data.frame(my_network_frame, directed=TRUE)
V(my_network)
E(my_network)
plot(my_network, edge.arrow.size=.5, vertex.color="red", vertex.size=10,
vertex.frame.color="black", vertex.label.color="black", vertex.label.cex=0.5,
edge.colour="black", edge.width="0.5")
Thank you.
Related
I'm a novice but trying to make a graph of collaborations based on shared publications. I've got a plot that is getting there but the nodes are far too close to discern any of the edges. There are two nodes that aren't connected to any others but removing them doesn't seem to change the spread. Is there a way to spread these nodes out more?
Here's what I have so far:
plot(net, edge.width=sqrt(links$weight),
edge.arrow.size=0, edge.lty=1, mode=0, vertex.size=7,
vertex.label.dist=2, vertex.label.cex=0.7)
Thanks!
Nevermind I found the layout options
i'm now using lesmis.gml to do network analysis homework.
I can't adjust graph node's distance: there's more than 70 nodes and the nodes are too close.
graph is variable g and g2.
graph looks weird like this.(image)
here's my code using R.
I tried to use Gephi, but my laptop doesn't run it well. It shuts off.
install.packages('igraph')
install.packages('statnet')
library('igraph')
library('statnet')
g<-read.graph("lesmis.gml", format=c("gml"))
g
graph.density(g)
igraph::degree(g,mode="out")
plot(g)
vcount(g)
centralization.degree(g)
V(g)$size<-igraph::degree(g)*5
plot(g)
clo<-igraph::closeness(g)
clo
clo.score<-round((clo-min(clo))*length(clo)/max(clo))+1
clo.colors<-rev(heat.colors(max(clo.score)))
V(g)$color<-clo.colors[clo.score]
plot(g)
btw<-igraph::betweenness(g)
btw
btw.score<-round(btw)+1
btw.score
btw.colors<-rev(heat.colors(max(btw.score)))
V(g)$color<-btw.colors[btw.score]
plot(g)
clusters(g)
clusters(g)$csize
cliques(g)
sapply(cliques(g), length)
largest_cliques(g)
cliques(g)
sapply(cliques(g),length)
a<-largest_cliques(g)
a
clique1<-a[[1]]
g2<-induced.subgraph(graph=g,vids=clique1)
plot(g2)
vcol<-rep("grey80",vcount(g))
vcol[unlist(largest_cliques(g))]<-"gold"
plot(as.undirected(g),vertex.lavel=V(g)$name, vertex.color=vcol)
windows()
I have two suggestions. Before presenting them, I will set up the basics so that what I do is (mostly) repeatable. This is just a streamlined version of what you had in your code, with a change to the vertex size as you had it.
library(igraph)
g<-read.graph("temp/lesmis.gml", format=c("gml"))
V(g)$size<-igraph::degree(g)/2
btw<-igraph::betweenness(g)
btw.score<-round(btw)+1
btw.colors<-rev(heat.colors(max(btw.score)))
V(g)$color<-btw.colors[btw.score]
I think that this is what #nhl was suggesting. There are quite a few layout functions in igraph. Just try a bunch of them and see what looks good. I kind of liked the large graph layout.
set.seed(1234)
LO_LGL = layout_with_lgl(g)
plot(as.undirected(g), layout=LO_LGL, margin=c(-0.25,-0.25))
Once you get something that is pretty close, you might try using tkplot which will allow you to select nodes and move them around to make the graph more readable.
tkplot(as.undirected(g), layout=LO_LGL)
I used the previous layout as a starting place and adjusted the vertices by hand to make the graph clearer. It is not perfect, but you can see some of the communities.
I have the following sample data (total of 700 observations), code and resulting graph in R. How can I modify the code to make the graph more readable?
The data can be read as "V1 is a follower of V2", on down.
g <- graph.data.frame(graph_subset, directed=TRUE)
plot(g, edge.width=E(g)$weight)
I agree with #MrFlick's comment and #zx8754. You could also try modifying the layout:
plot(g, mode = "circle", edge.width=E(g)$weight)
There are several modes to modify the layout: circle, eigen, random, spring, kamadakawi and so on. Kamadakawi is my best option.
Hope it helps!
Try:
plot(g, layout=layout_in_circle, edge.width=E(g)$weight)
this must change the layout, you can also modify the size of vertex with vertex.cex=1.5 and hide the labels with displaylabels=F. There are more commands, find them in the tutorial that #zx8754 mentioned, section 4.2.
Is it possible to display two colors inside a node instead of one in a network representation in R?
For example a node for which half of the circle is blue and half is red.
Any help is greatly appreciated. Thanks!
#Rodrigo is actually on the right track.
See:
library(igraph)
?vertex.shape.pie
Which allows things like:
test <- graph.data.frame(data.frame(one=1,two=1))
plot(
test,
vertex.shape="pie",
vertex.pie=list(c(10,10)),
vertex.pie.color=list(c("red","blue"))
)
What about
pie(c(1,1),init.angle=90)
I don't know if it can be adjusted to your needs, though.
I am trying to build graphs using tree-like data, where nodes typically split into >2 edges. I have tried various layouts, and I see that the layout.reingold.tilford parameter will generate tree-like graphs with non-bifurcating data. However the outputs are not particularly attractive. I would rather use something like the layout.lgl or layout.kamada.kawai since these produce more radial structures. I cannot see how to change the parameters in R such that these trees have no overlapping edges though. Is this possible?
I imported a simple data file in Pajek format, with 355 nodes and 354 edges. I'm currently printing it using:
plot.igraph(g,vertex.size=3,vertex.label=NA,layout=layout.lgl)
This gives me an output like this, which is nice, but still has overlapping edges. I have read that you can manually fix this using tkplot, or another program like cytoscape, however I have quite a few of these to build, and the size of them makes manual correction a hassle.
Many thanks.
Just want to add a comment but my rep is too low. The method that #bdemarest posted does not work on igraph version > 0.7. The newer version does not support the area parameter, so I cannot get the same effect. And getting the old version to build took me a while, so I though I'd share some insights. You can manually install igraph 0.7 from source if you download it from igraph nightly builds. On my machine (Mac OS 10.10), I encountered some problems building it, due to gfortran, so I found this link that solved the problem. Hope that helps anyone who wants to create similar graphs in R.
You may want to try layout.fruchterman.reingold(). It seems to do a good job keeping the edges from crossing. I've tested it with a 355 node version of the barabasi graph suggested by #Tamás.
library(igraph)
g = barabasi.game(355, directed=FALSE)
png("plot1.png", height=6, width=12, units="in", res=200)
par(mfrow=c(1, 2))
plot.igraph(g,vertex.size=3,vertex.label=NA,
layout=layout.fruchterman.reingold(g, niter=10000))
mtext("layout.fruchterman.reingold, area = vcount^2", side=1)
plot.igraph(g,vertex.size=3,vertex.label=NA,
layout=layout.fruchterman.reingold(g, niter=10000, area=30*vcount(g)^2))
mtext("layout.fruchterman.reingold, area = 30 * vcount^2", side=1)
dev.off()
layout.reingold.tilford has a parameter called circular. Setting this to TRUE will convert the final layout into a radial one by treating the X coordinate as the angle (after appropriate rescaling) and the Y coordinate as the radius. Ironically enough, this does not guarantee that there will be no edge crossings in the end, but it works nicely if your subtrees are not exceedingly wide compared to the rest of the graph:
> g <- barabasi.game(100, directed=F)
> layout <- layout.reingold.tilford(g, circular=T)
> plot(g, layout=layout)