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.
Related
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.
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'm plotting a directed network from an edge list, and have so far created a tree-like plot (see here).
It looks good, however all of the nodes are too close together. I would like to keep the shape of it while spreading out the nodes more. Here's the code that got me the image above:
library(igraph)
ref <- read.csv("my-ref.csv", as.is=T)
el <- graph.data.frame(ref, directed=T)
lay.kk <- layout.kamada.kawai(el, niter=1000, kkconst=50)
plot.igraph(el, lay=lay.kk, vertex.label=NA, vertex.size=2, vertex.color="black")
I've tried messing around with kkconst, but that doesn't seem to change anything. Any tips are greatly appreciated!
The Kamada-Kawai layout does not really work well for disconnected graphs because the disconnected components tend to "drift away" from each other. Since igraph scales the entire plot to fit within the canvas, the farther the components are from each other, the closer the nodes will be within the components. Try the Fruchterman-Reingold layout instead.
As Tamás suggested, you might get better results with layout.fruchterman.reingold(). You can fine tune this function with the following parameters:
require(igraph)
g <- erdoss.renyi.game(n = 100, p.or.m = 0.04)
lo <- layout.fruchterman.reingold(g, repulserad = vcount(g)^2.8,
area = vcount(g)^2.3, niter = 1000)
plot(g, layout = lo, vertex.size = 3, vertex.frame.color = NULL,
vertex.label.dist = 0.5, vertex.label.cex = 0.7, edge.width = 0.5)
These values resulted a low overlap, clear but compact layout for me. Try to change them a bit, to see their effect on the layout. Those parameters I set for plot() also help to make the visualization more clear.
There is a similar question here to which I have posted the following answer
Option 1: make the vertices smaller
node.size= c(10,10,10)
plot(net, vertex.size=node.size*0.25)
Option 2 (in case the distances between the vertices are not important to you):
# Use the tkplot option to edit your graph in GUI
tkplot (net)
tkplot GUI will allow you to interactively change the layout types.
Note: tkplot outputs the graph as eps. If you want to edit it further or export it to pdf I suggest using inkscape (I use it for all my graph editing - just save the graph as pdf in RStudio and edit it in inkscape).
For the case of eps if you are on a windows machine you will need to tweak inkscape to open this format. A very short and simple process which is detailed here:
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)
I am using ape (Analysis of Phylogenetics and Evolution) package in R that has dendrogram drawing functionality. I use following commands to read the data in Newick format, and draw a dendrogram using the plot function:
library("ape")
gcPhylo <-read.tree(file = "gc.tree")
plot(gcPhylo, show.node.label = TRUE)
As the data set is quite large, it is impossible to see any details in the lower levels of the tree. I can see just black areas but no details. I can only see few levels from the top, and then no detail.
I was wondering if there is any zoom capability of the plot function. I tried to limit the area using xLim and yLim, however, they just limit the area, and do not zoom to make the details visible. Either zooming, or making the details visible without zooming will solve my problem.
I am also appreciated to know any other package, function, or tool that will help me overcoming the problem.
Thanks.
It is possible to cut a dendrogram at a specified height and plot the elements:
First create a clustering using the built-in dataset USArrests. Then convert to a dendrogram:
hc <- hclust(dist(USArrests))
hcd <- as.dendrogram(hc)
Next, use cut.dendrogram to cut at a specified height, in this case h=75. This produces a list of a dendrogram for the upper bit of the cut, and a list of dendograms, one for each branch below the cut:
par(mfrow=c(3,1))
plot(hcd, main="Main")
plot(cut(hcd, h=75)$upper,
main="Upper tree of cut at h=75")
plot(cut(hcd, h=75)$lower[[2]],
main="Second branch of lower tree with cut at h=75")
The cut function described in the other answer is a very good solution; if you would like to maintain the whole tree on one page for some interactive investigation you could also plot to a large page on a PDF.
The resulting PDF is vectorized so you can zoom in closely with your favourite PDF viewer without loss of resolution.
Here's an example of how to direct plot output to PDF:
# Open a PDF for plotting; units are inches by default
pdf("/path/to/a/pdf/file.pdf", width=40, height=15)
# Do some plotting
plot(gcPhylo)
# Close the PDF file's associated graphics device (necessary to finalize the output)
dev.off()