This is a simple question, but I can't seem to find documentation for it. I have a graph object that was created by taking the union of two graphs. I would like to output the layout function that was created during the merge.
a <- barabasi.game(10)
b <- barabasi.game(20)
ab <- union(a,b)
Ideally, I'd like to visualize the union in a way that places subgraphs (a,b) in their own "space." Is there a default function in igraph for outputting the layout of a graph object?
For igraph, layouts are matrices of coordinates. If you call any layout method, you get a matrix:
loa <- layout.fruchterman.reingold(a)
lob <- layout.fruchterman.reingold(a)
If you assign these matrices to the layout graph attribute, igraph will use them automatically at plotting, or you can pass them to the plot method directly:
b$layout <- loa
plot(a)
plot(b, layout = lob)
If you take the union of two graphs, their layout attributes, if they have, won't be merged, but renamed to layout_1 and layout_2. If you want to keep the non-overlapping parts separated, and merge the layouts, I have this idea:
a <- barabasi.game(10)
b <- barabasi.game(20)
a$layout <- layout.norm(layout_with_fr(a), -1, 0, -1, 1) # each subgraph
b$layout <- layout.norm(layout_with_fr(b), 0, 1, -1, 1) # in their own space
V(a)$x <- a$layout[,1]
V(a)$y <- a$layout[,2]
V(b)$x <- b$layout[,1]
V(b)$y <- b$layout[,2]
V(a)$color <- 'blue'
ab <- union(a, b)
V(ab)$x <- vapply(seq(vcount(ab)),
function(vid){
ifelse(is.na(V(ab)$x_1[vid]),
V(ab)$x_2[vid],
V(ab)$x_1[vid])
}, 0.0)
V(ab)$y <- vapply(seq(vcount(ab)),
function(vid){
ifelse(is.na(V(ab)$y_1[vid]),
V(ab)$y_2[vid],
V(ab)$y_1[vid])
}, 0.0)
ab$layout <- cbind(V(ab)$x, V(ab)$y)
V(ab)$color[is.na(V(ab)$color)] <- 'yellow'
plot(ab, rescale = FALSE)
Here I created two layouts, one scaled to the west, other to the east half of the coordinate system. Then I merged the layouts, taking by default the coordinates from a, and from b if the vertex was not part of b. After making a new layout matrix from x and y coordinates, I plotted the graph with rescale = FALSE, so the coordinates remain unchanged.
Note: likely you want to merge your graphs not based on numberic vertex IDs, but by names. For this, create a name vertex attribute, and pass the byname = TRUE parameter to the union method.
Related
How would I put a circle around certaiin variables in the following plot?
library(dagitty)
g = dagitty('dag{
A [pos="-1,0.5"]
W [pos="0.893,-0.422"]
X [adjusted,pos="0,-0.5"]
Y [pos="1,0.5"]
A -> Y
X -> A
X -> W
X -> Y
}')
png("mp.png", width = 500, height = 500,res=300)
plot(g)
dev.off()
In the web based tool you can indicate eg latent or adjusted and it changes the color of the circle, but this is not quite what I am looking for, although if it were possible to get these in the plot from R that would be sufficient, although I don't really like the way the variable is next to the circle in the web based version. I really wanted to circle observed variables and not circle unobserved ones.
I wrote a function which takes the points you want to circle as input, extracts the position of said points and circles them.
library(dagitty)
g = dagitty('dag{
A [pos="-1,0.5"]
W [pos="0.893,-0.422"]
X [adjusted,pos="0,-0.5"]
Y [pos="1,0.5"]
A -> Y
X -> A
X -> W
X -> Y
}')
circle_points <- function(points_to_circle, g) {
#few regexs to extract the points and the positions from "g"
#can surely be optimized, made nicer and more robust but it works for now
fsplit <- strsplit(g[1], "\\]")[[1]]
fsplit <- fsplit[-length(fsplit)]
fsplit <- substr(fsplit, 1, nchar(fsplit)-1)
fsplit[1] <- substr(fsplit[1], 6, nchar(fsplit))
vars <- sapply(regmatches(fsplit,
regexec("\\\n(.*?)\\s*\\[", fsplit)), "[", 2)
pos <- sub(".*pos=\\\"", "", fsplit)
#build dataframe with extracted information
res_df <- data.frame(vars = vars,
posx = sapply(strsplit(pos, ","), "[",1),
posy = sapply(strsplit(pos, ","), "[",2))
df_to_circle <- res_df[res_df$vars %in% points_to_circle,]
#y-position seems to be inverted and has to be multiplied by -1
points(c(as.numeric(df_to_circle$posx)),
c(as.numeric(df_to_circle$posy) * -1),
cex = 4)
}
plot(g)
circle_points(c("A", "Y"), g)
This results in:
You can of course work with the cex parameter, adding colors etc. It seems that the positioning of the circles is a bit off-centered so maybe manipulate the x and y positions in circle_points by a slim margin.
I did not find any information in dagitty, but bnlearn package can add circle/or other shape easily. But I just noticed you only want to add circle to observed traits rather than latent variables (better mentioned this in your title). Then my code might not be what you are looking for. I still attached the code here for your reference. Alternatively, you can distinguish observed/latent traits in different color. This can be easily done using bnlearn (https://www.bnlearn.com/examples/graphviz-plot/)
library(bnlearn)
tree = model2network("[X][W|X][A|X][Y|A:X]")
graphviz.plot(tree, main = "DAG structure", shape = "circle",
layout = "circo")
Suppose I have two datasets: (1) a data frame: coordinates of localities, each with ID; and (2) a linguistic distance matrix which reflects the linguistic distance between these localities.
# My data are similar to this structure
# dataframe
id <- c("A","B","C","D","E")
x_coor <- c(0.5,1,1,1.5,2)
y_coor <- c(5.5,3,7,6.5,5)
my.data <- data.frame(id = id, x_coor = x_coor, y_coor = y_coor)
# linguistic distance matrix
A B C D
B 308.298557
C 592.555483 284.256926
D 141.421356 449.719913 733.976839
E 591.141269 282.842712 1.414214 732.562625
Now, I want to visualize the linguistic distance between every two sites onto a map by the thickness or color of the line connect the adjacent localities in R.
Just like this:
enter image description here
My idea is to generate the delaunay triangulation by deldir or tripack package in R.
# generate delaunay triangulation
library(deldir)
de=deldir(my.data$x_coor,my.data$y_coor)
plot.deldir(de,wlines="triang",col='blue',wpoints = "real",cex = 0.1)
text(my.data$x_coor,my.data$y_coor,my.data$id)
this is the plot:
enter image description here
My question is how to reflect the linguistic distance by the thickness or color of the edges of triangles? Is there any other better method?
Thank you very much!
What you want to do in respect of the line widths can be done "fairly
easily" by the deldir package. You simply call plot.deldir() with the
appropriate value of "lw" (line width).
At the bottom of this answer is a demonstration script "demo.txt" which shows how to do this in the case of your example. In particular this script shows
how to obtain the appropriate value of lw from the "linguistic distance
matrix". I had to make some adjustments in the way this matrix was
presented. I.e. I had to convert it into a proper matrix.
I have rescaled the distances to lie between 0 and 10 to obtain the
corresponding values of the line widths. You might wish to rescale in a different manner.
In respect of colours, there are two issues:
(1) It is not at all clear how you would like to map the "linguistic
distances" to colours.
(2) Unfortunately the code for plot.deldir() is written in a very
kludgy way, whence the "col" argument to segments() cannot be
appropriately passed on in the same manner that the "lw" argument can.
(I wrote the plot.deldir() code a long while ago, when I knew far less about
R programming than I know now! :-))
I will adjust this code and submit a new version of deldir to CRAN
fairly soon.
#
# Demo script
#
# Present the linguistic distances in a useable way.
vldm <- c(308.298557,592.555483,284.256926,141.421356,449.719913,
733.976839,591.141269,282.842712,1.414214,732.562625)
ldm <- matrix(nrow=5,ncol=5)
ldm[row(ldm) > col(ldm)] <- vldm
ldm[row(ldm) <= col(ldm)] <- 0
ldm <- (ldm + t(ldm))/2
rownames(ldm) <- LETTERS[1:5]
colnames(ldm) <- LETTERS[1:5]
# Set up the example data. It makes life much simpler if
# you denote the "x" and "y" coordinates by "x" and "y"!!!
id <- c("A","B","C","D","E")
x_coor <- c(0.5,1,1,1.5,2)
y_coor <- c(5.5,3,7,6.5,5)
# Eschew nomenclature like "my.data". Such nomenclature
# is Micro$oft-ese and is an abomination!!!
demoDat <- data.frame(id = id, x = x_coor, y = y_coor)
# Form the triangulation/tessellation.
library(deldir)
dxy <- deldir(demoDat)
# Plot the triangulation with line widths proportional
# to "linguistic distances". Note that plot.deldir() is
# a *method* for plot, so you do not have to (and shouldn't)
# type the ".deldir" in the plotting command.
plot(dxy,col=0) # This, and plotting with "add=TRUE" below, is
# a kludge to dodge around spurious warnings.
ind <- as.matrix(dxy$delsgs[,c("ind1","ind2")])
lwv <- ldm[ind]
lwv <- 10*lwv/max(lwv)
plot(dxy,wlines="triang",col='grey',wpoints="none",
lw=10*lwv/max(lwv),add=TRUE)
with(demoDat,text(x,y,id,col="red",cex=1.5))
I am trying to plot a network that changes in time. The network starts with a certain number of nodes and edges and each time step some of the nodes and edges are removed.
I want to be able to plot the network so that the nodes are in the same place in each. However when I try this. sometimes the nodes shift position in the plot frame even if the relation to each other is the same.
I am making the network change into a gif so even small changes are annoying. I think the change may occur when a large fraction of the nodes are removed but I am not sure.
The code below illustrates this using an ER graph.
library(igraph); library(dplyr)
#generate random graph
set.seed(500)
RandomGraph <- sample_gnm(1000, 2500)
#name nodes
V(RandomGraph)$name <- paste0("Node", 1:1000)
#Get the coordinates of the Nodes
Coords <- layout_with_fr(RandomGraph) %>%
as_tibble %>%
bind_cols(data_frame(names = names(V(RandomGraph))))
#Delete random vertices
deletevertex <-sample( V(RandomGraph)$name, 400)
RandomGraph2 <-delete.vertices(RandomGraph, deletevertex)
#get the coordinates of the remaining Nodes
NetCoords <- data_frame(names = names(V(RandomGraph2))) %>%
left_join(Coords, by= "names")
#plot both graphs
RandomGraph%>%
plot(.,vertex.size=.8, edge.arrow.size=.4, vertex.label = NA, layout = as.matrix(Coords[,1:2]))
RandomGraph2%>%
plot(.,vertex.size=.8, edge.arrow.size=.4, vertex.label = NA, layout = as.matrix(NetCoords[,2:3]))
#They nodes have the same relationship to each other but are not laid out in the same position in the frame
As you can see the plots have placed nodes in the same place relative to each other but not relative to the frame.
How can I have the plot position fixed.
plot.igraph rescales each axis by default (from -1 to +1 on both x and y).
You just need to turn that off: rescale = F and then explicitly set appropriate xlim and ylim values.
For your example code..
RandomGraph%>%
plot(.,vertex.size=.8, edge.arrow.size=.4, vertex.label = NA, layout = as.matrix(Coords[,1:2]),rescale=F,xlim=c(-25,30),ylim=c(-20,35))
RandomGraph2%>%
plot(.,vertex.size=.8, edge.arrow.size=.4, vertex.label = NA, layout = as.matrix(NetCoords[,2:3]),rescale=F,xlim=c(-25,30),ylim=c(-20,35))
The problem is that
identical(range(Coords[1]), range(NetCoords[2]))
# [1] FALSE
Since igraph normalizes the coordinates on a range between -1 and 1 before plotting, this leads to slightly different coordinates for NetCoords compared to Coords. I'd just calculate the normalized coordinates for all nodes beforehand:
coords_rescaled <- sapply(Coords[-3], function(x) -1+((x-min(x))*2)/diff(range(x)))
rownames(coords_rescaled) <- Coords$names
And then assign the normalized coordinates (or the required subset) and set rescale=FALSE (as #jul) already suggested:
par(mfrow=c(1,2), mar=c(1,.5,1,.5))
RandomGraph%>%
plot(.,edge.arrow.size=.4, layout = coords_rescaled, rescale=F);box()
RandomGraph2%>%
plot(.,edge.arrow.size=.4, layout = coords_rescaled[NetCoords$names, ], rescale=F);box()
Let g be an igraph object. For example, g <- make_graph(~A-C-B, C-D, E-D-F). And let us set up a vertex attribute called level
V(g)[c("A", "B")]$level <- 1
V(g)[c("C")]$level <- 2
V(g)[c("D")]$level <- 3
V(g)[c("E", "F")]$level <- 4
Are there any tools in igraph to build a layout for g such that it respects level in a meaning that a vertex with less level is always placed to the left and vertices with same level have the same (or close) abscissa.
So, for the given graph I'd like to see a picture like this:
Since a layout in igraph is just a matrix of {x,y} coordinates, you can set the x-coordinates equal to your levels.
g <- make_graph(~A-C-B, C-D, E-D-F)
V(g)$level <- c(1,2,1,3,4,4)
l <- matrix(c(V(g)$level,1,2,3,2,3,1),nrow=length(V(g)$level),ncol=2)
plot(g, layout=l)
I just did the y-axis by hand, but you can construct it as you see fit.
Using Sugiyama layout
Sugiyama layout works by adding layers. There are a lot of options with the layout, but, basically, it tries to create a hierarchical representation of the graph.
l <- layout_with_sugiyama(g, layers = -V(g)$level)$layout
#note the "-", this ensures that the smaller level values get small x coordinates
plot(g,layout=l[,c(2,1)])
Was wondering if the following is possible:
Currently, I have a subset of nodes in a graph, (graph A), which belongs in another separate and larger graph (graph B).
I would like to preserve the layout from graph B pertaining to this subset of nodes when running a layout generation algorithm on graph A. Could be any of the layout algorithms.
layout.circle(graph, params)
layout.sphere(graph, params)
layout.fruchterman.reingold(graph, ..., dim=2, params)
layout.kamada.kawai(graph, ..., dim=2, params)
layout.spring(graph, ..., params)
layout.reingold.tilford(graph, ..., params)
layout.fruchterman.reingold.grid(graph, ..., params)
layout.lgl(graph, ..., params)
layout.graphopt(graph, ..., params=list())
layout.mds(graph, dist=NULL, dim=2, options=igraph.arpack.default)
layout.svd(graph, d=shortest.paths(graph), ...)
You can use the minx, maxx, miny and maxy arguments of layout.fruchterman.reingold() or layout.kamada.kawai() to fix some vertices completely. These arguments specify vertex-specific lower and/or upper limits for the coordinates.
For the vertices you want to fix, set them exactly to the value to fix, and for the other vertices set minx to some small negative values (-Inf might work, too), and set maxx to some large value, (again, maybe Inf works, too).
You might need to use the rescale=FALSE argument in plot.igraph() to avoid rescaling the complete layout, for both the first and second graph.
Edit:
From the manual:
'minx' If not 'NULL', then it must be a numeric vector that gives
lower boundaries for the 'x' coordinates of the vertices. The
length of the vector must match the number of vertices in the
graph.
'maxx' Similar to 'minx', but gives the upper boundaries.
For example:
g <- graph.star(10, center=1)
minx <- rep(-Inf, vcount(g))
maxx <- rep( Inf, vcount(g))
minx[1] <- 0
maxx[1] <- 0
lay <- layout.fruchterman.reingold(g, minx=minx, maxx=maxx, miny=minx, maxy=maxx)
plot(g, layout=lay)
fixes the first vertex into (0,0) (might be modified by rescaling, to avoid rescaling, use rescale=FALSE in plot() and set the plotting limits).
The layout in igraph is defined as a n (number of nodes) by 2 matrix where the first column indicates the x-coordinate (on a arbitrary scale) and the second column the y-coordinate. You can store the result of any of these functions to obtain this matrix, and then pass that to the layout argument when you plot another graph.
library("igraph")
# A 3-node network:
g <- graph.adjacency(matrix(1,3,3))
# Obtain a layout:
l <- layout.circle(g)
# A different 3-node network:
g2 <- graph.adjacency(matrix(0,3,3))
# Plot second network with layout based on first network:
plot(g2,layout=l)
Edit
If you have a subset of a graph you can simply index this matrix. E.g.:
# A 3-node network:
g <- graph.adjacency(matrix(1,3,3))
# Obtain a layout:
l <- layout.circle(g)
# A 2-node subset:
g2 <- graph.adjacency(matrix(1,2,2))
# Plot second network with layout based on first network:
plot(g2,layout=l[1:2,])