Essayez de tracer un réseau avec le package ggraph
et spécifiez les coordonnées de chaque noeud. Bien que je puisse implémenter ceci avec le paquet igraph
- je ne peux pas faire ceci avec le paquet ggraph
.Tracé réseau ggraph: spécifiez les coordonnées de noeud
# reproducible example to generate a random graph
library(igraph)
g1 <- erdos.renyi.game(20, 1/2)
plot(g1)
# function to produce coordinates for each node in order of the node
# degree (number of links per node)
coord <- function(g){
n.nod <- length(V(g))
mat.c <- matrix(0, nrow = n.nod, ncol = 2)
deg <- degree(g)
uniq.deg <- unique(deg)
min.d <- min(deg); max.d<- max(deg)
spa <- 10/(max.d - min.d) # how much to increment 0 to 10 space along y-axis
divi.y.1 <- seq(from = min.d, to=max.d, by = 1)
divi.y.2 <- seq(from = 0, to=10, by = spa) # both have same length
ind.x <- c(); ind.x[1] = 0
for(x in 2:n.nod){
ind.x[x] <- ind.x[x-1] + 0.1
if(ind.x[x] >= 10){
ind.x[x] = ind.x[x-1] - 0.1
}
}
d1 <- data.frame(divi.y.1, divi.y.2)
# plotting space of grid is (0,0), (0, 10), (10, 10), (10, 0)
for(i in 1:n.nod){
# y-axis in order
inD <- which(d1$divi.y.1 == deg[i])
mat.c[i, 2] <- d1[inD,2]
}
mat.c[, 1] <- ind.x
return(data.frame(mat.c))
}
Voici la façon "ancienne" de comploter l'objet igraph
:
# plot igraph object - the old fashion way
x11()
plot(g1, layout = coord(g1), rescale = T, frame=T,
vertex.frame.color = "black", edge.color = "lightsteelblue",
edge.width = 1, vertex.label.cex = 1,
vertex.label.color = "black",
main = "Nodes in order of degree: top have more links, bottom fewer links")
Lien vers la documentation ggraph
est here. Reportez-vous également au référentiel GitHub pour le ggraph
pour les instructions d'installation du package (besoin> version R.3.3). Ci-dessous la parcelle de GGRAPH qui fonctionne (mais je ne l'ai pas spécifié les coordonnées de chaque nœud):
library(ggraph)
V(g1)$NaMe <- seq(1:20)
x11()
ggraph(g1, 'igraph', algorithm = 'kk') +
geom_edge_link(colour = "black", alpha = 0.8, show.legend = F) +
geom_node_label(aes(label = NaMe)) + ggtitle("ggraph plot: How to allocate coordinate for each node?") +
ggforce::theme_no_axes()
Voici ma tentative de rendre le terrain ggraph
avec coordonnées spécifiées pour chaque nœud. À la suite des exemples similaires et les tentatives antérieures dans ggraph()
où les coordonnées des noeuds sont passés sur le terrain, j'ai essayé ce qui suit:
g <- make_ring(10) + make_full_graph(5)
coords <- layout_(g, as_star())
plot(g, layout = coords)
# try to replicate this example:
coords2 <- add_layout_(g1, coord(g1))
également essayé d'utiliser this function. C'est difficile parce qu'il n'y a pas d'exemple dans la documentation.
Lay <- layout_igraph_manual(g1, coord(g1))
Lay <- layout_igraph_igraph(g1, coord(g1))
x11()
ggraph(g1, 'igraph', algorithm = 'kk') + add_layout_(Lay) +
# layout_igraph_circlepack() +
geom_edge_link(colour = "black", alpha = 0.8, show.legend = F) +
geom_node_label(aes(label = NaMe)) + ggtitle("ggraph plot: Cannot allocate coordinate for each node") +
ggforce::theme_no_axes()