I need to set the position of vertex labels inside or right next to them. Which parameter should i change in plot to change that?
i've tried to change vertex.label.cex and vertex.label.dist parameters but labels still located in the corner of the plot and look messy.
plot(g,
vertex.color= "yellow",
vertex.size = degree(g)*0.3,
vertex.label=names,
vertex.label.cex = degree(g)/1000,
edge.width= 0.5,
vertex.label.dist=0.1)
You had the right idea with vertex.label.dist. You just used too small a value.
Since you did not provide your graph, I will illustrate with a random graph (and some modifications to your plot statement to match this graph).
library(igraph)
set.seed(1234)
g = erdos.renyi.game(15, 0.2)
plot(g)
par(mfrow=c(1,2))
set.seed(4321)
plot(g,
vertex.color= "yellow",
vertex.size = degree(g),
edge.width= 0.5,
vertex.label.dist=0.1,
margin=-0.2,
main="vertex.label.dist=0.1")
set.seed(4321)
plot(g,
vertex.color= "yellow",
vertex.size = degree(g),
edge.width= 0.5,
vertex.label.dist=1,
margin=-0.2,
main="vertex.label.dist=1")
Notice that with the larger value of vertex.label.dist the labels are visibly offset.
Related
i have just recently started learning network code using the material and data by Ognyanova, found at:
http://www.kateto.net/wp-content/uploads/2015/06/Polnet%202015%20Network%20Viz%20Tutorial%20-%20Ognyanova.pdf
guided by some of the code in the document, I am trying to assign edge colors and then edge line types to an edge attribute, but so far unsuccessful. below is an example of the current code I tried but get many errors including this:
Error in rgb(x1/255, x[2]/255, x[3]/255) :
color intensity NA, not in [0,1]
I have looked at many other related answered questions but unable to get the right code
V(Network)$color <- colrs[V(Network)$media.type]
ct <- colorRamp(c("black", "green"))
E(Network)$color = sapply(ct(E(Network)$type), function(x) rgb(x[1]/255, x[2]/255, x[3]/255))
plot(Network, edge.arrow.size = 0.4, edge.curved = .3, vertex.label.cex = .7, vertex.label = V(Network)$media)
I have also tried this, but got a graph generated without edges though as shown in image below. any help will be appreciated. thanks
> colrs <- c("gray50", "tomato", "gold")
V(Network)$color <- colrs[V(Network)$media.type]
ct <- c("black", "green")
edge.cl <- ct[E(Network)$type]
plot(Network, edge.arrow.size = 0.4, edge.curved = .3, vertex.label.cex = .7, vertex.label = V(Network)$media, edge.color = edge.cl)
enter image description here
I have an igraph network which I want to color with RColorBrewer. Vertices have a "sector" attribute, which I want to use to color them. To do this, I have defined a color palette based on sectors:
color.range <- brewer.pal(nlevels(as.factor(V(g)$sector)), name = "Dark2")
V(g)$color <- color.range[as.factor(V(g)$sector)]
When I plot my graph, colors of the legend do not match the information in the "sector" attribute of my vertices:
plot.igraph(g,
vertex.label = ifelse(V(g)$indegree > 5, V(g)$sector, NA),
vertex.label.family = "Arial",
vertex.label.color = "black",
vertex.label.cex = 0.5,
vertex.frame.color = NA,
vertex.color = V(g)$color,
vertex.size = 3,
layout = layout.fruchterman.reingold,
edge.arrow.mode = 1,
edge.arrow.size = 0.2)
legend("topleft",
legend = levels(as.factor(V(g)$sector)),
col = levels(as.factor(V(g)$color)),
pch = 19,
cex = 0.8,
title = "",
bg="transparent",
bty = "n")
On the plot I see the categories of some high degree vertices and comparing them with the legend, colors do not match:
example plot where colors on graph do not match legend
My question is: how can I define a color range that will actually match my sector categories OR how can I create a legend that represents colors properly. Unfortunately I don't know which one is the problem.
I think that you are computing the colors for the legend incorrectly. If instead, I use
col = color.range[as.numeric(levels(as.factor(V(g)$sector)))]
the colors in the legend match the labeled points in the graph.
For me the solution of G5W as not completely working as
levels(as.factor(V(g)$sector))
returned the levels as a list of strings that could not be turned into numbers, so I have got an error:
Warning message:
In legend("topleft",legend = levels(as.factor(V(g)$sector)),col =
color.range[as.numeric(levels(as.factor(V(g)$sector)))],: NAs introduced by coercion
I have added one more as.factor() instead and it seems to work now:
col = color.range[as.numeric(as.factor(levels(as.factor(V(g)$sector))))],
I have plotted a network plot with igraph. The opacity of the vertices is reduced to 0.5.
Now I would like to add a legend to the plot manually, where the points have the same color/transparency as the vertices in the plot.
plot(g, vertex.color = adjustcolor(V(g)$color, alpha = 0.5))
legend('topleft',legend = names, pt.cex = 2, pch = 21, pt.bg = colors)
How can I alter the transparency of the points in the legend?
Since you do not provide data, I make up some sample data. Also, both "names" and "colors" are the names of R functions so I changed them to "Names" and "Colors".
You can make the adjustment that you want just by using adjustcolor again.
library(igraph)
## Some sample data
set.seed(1234)
g = erdos.renyi.game(10, 0.3)
Colors = rainbow(4)
V(g)$color = sample(Colors,10, replace=TRUE)
Names = paste("N", 1:4, sep="")
## Your plot and adjusted legend
plot(g, vertex.color = adjustcolor(V(g)$color, alpha = 0.5))
legend('topleft', legend = Names, pt.cex = 2, pch = 21,
pt.bg = adjustcolor(Colors, alpha = 0.5))
I can use the code below to generate and draw communities:
wc <- walktrap.community(subgraph)
modularity(wc)
membership(wc)
layout <-layout.fruchterman.reingold(subgraph)
plot(wc, subgraph, layout=layout, vertex.label=NA, vertex.size=5, edge.arrow.size=.2)
However, the colors of the communities are automatic, I have two questions:
Could I custom the community color?
Could I add some text in the community area?
Yes, you can do both of those things. Changing the colors of the nodes according to which module they are in (as well as changing the colors of the polygons around the modules) is straightforward using arguments in plot.igraph. Adding text to modules is not so trivial, and the easiest solution is as far as I know is to add text to the plot manually.
library(igraph)
# Generate random graph and community structure
set.seed(23)
g <- sample_gnm(15, 45)
wc <- walktrap.community(g)
# Plot
par(mfrow=c(1,2), mar=rep(1,4))
layout <-layout.fruchterman.reingold(g)
plot(wc, g, layout=layout, vertex.label=NA, vertex.size=5, edge.arrow.size=.2)
# Change colors of nodes, polygons, and polygon borders
new_cols <- c("white", "red", "black")[membership(wc)]
plot(wc, g, col=new_cols, mark.border="black", mark.col=c("tan", "pink", "lightgray"),
layout=layout, vertex.label=NA, vertex.size=5, edge.arrow.size=.2)
# Add labels
text(c(-1.15, 0.8, 0.9), c(0.35, -0.7, 0.8), c("A", "B", "C"))
I'm trying to plot a graph with characters as labels. The issue it that the text is a bit long and the vertices overlap between them. Is there a way to minimize the area of the circular vertex by fitting the text inside? and also, how to avoid overlapping between them? This is the edge list and the data frame of attributes that I'm using(The second column of the df has the labels that I'm trying to fit. Here is my code:
## READ THE EDGE LIST AS A GRAPH OBJECT ##
library('igraph')
cn <- as.matrix(cn, header= FALSE)
g <- graph.edgelist(cn,directed= TRUE)
plot(g, layout = layout.reingold.tilford(g, root=1))
## GET ATTRIBUTES ##
V(g)$name=as.character(att$Name)
V(g)$desc=as.character(att$Description)
## GET LABELS ##
V(g)$label <- V(g)$name
Here I tried to use the method proposed in this post.
## FIT THE LABELS INSIDE THE VERTEX ##
ly <- layout.reingold.tilford(g, root=1) ly <- layout.auto(g)
plot(0, type="n", ann=FALSE, axes=FALSE, xlim=extendrange(ly[,1]), ylim=extendrange(ly[,2])) plot(g, layout=ly, rescale=FALSE, add=TRUE,
vertex.size=(strwidth(V(g)$label) + strwidth("oo")) * 100)
It is working for fitting the text inside the vertexes, but it is not minimizing the area of the circle. For instance, the string of characters "Random Process" would fit better in two lines instead of one and so on.. Also, it has problems with vertex overlapping. I would like to keep the structure of a tree diagram. Here is my outcome so far:
Try with this:
plot(G, vertex.size=8, vertex.color = rainbow(10, .8, .8, alpha= .8),
vertex.label.color = "black", vertex.label.cex = 0.4, vertex.label.degree = -pi/2,
edge.arrow.size = 0.3, edge.arrow.width = 0.4, edge.color = "black")
With vertex.label.degree and vertex.label.cex you fit the text on the node. Best!