我有一个自定义布局的网络。我想用ggnetwork
(未显示,请参阅小插图)对其进行自定义。我可以将节点位置传递给ggnetwork (ggplot)
,但是如何传递箭头(边缘)位置?
最小的例子:
myvec<-structure(list(lengths = c(1L, 2L, 27L, 1L, 6L, 1L, 15L, 1L,
23L, 1L, 4L, 2L, 77L, 1L, 1L, 1L, 22L, 2L, 21L, 1L, 3L, 1L, 30L,
2L, 38L, 1L, 40L, 2L, 22L, 1L, 12L, 1L, 8L, 1L, 12L, 1L, 9L,
1L, 9L, 1L, 28L, 2L, 12L, 1L, 31L, 1L, 12L, 1L, 5L, 1L, 15L,
1L, 10L, 1L, 25L, 1L, 16L, 1L, 27L, 1L, 25L, 1L, 31L, 2L, 20L
), values = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0)), class = "rle")
mymat <- structure(unlist(mapply(rep, myvec$values, myvec$lengths) ), .Dim = c(26L, 26L), .Dimnames = list(NULL, NULL) )
library(network)
net1 <- network(mymat,
matrix.type = "adjacency",
ignore.eval = FALSE
,directed=T
)
# finalxy is a modification of:
# library(sna)
# xy = gplot.layout.fruchtermanreingold(net1, NULL)
finalxy<-structure(c(-2, 0, 0, 6, 2, 6, 4, 6, 2, -6, -4, 0, 4, 0, 8, 8,
-4, -2, -2, 8, 10, 2, 0, -4, -2, 4, 4, 2, 4, 4, 0, 2, -2, -2,
2, -2, -2, 0, -4, -2, -4, -2, 0, 0, -2, 0, 0, -2, -4, -4, -4,
4), .Dim = c(26L, 2L) )
net1 %v% "xpos" = finalxy[, 1]
net1 %v% "ypos" = finalxy[, 2]
# ggnet
library(GGally)
gdata <- ggnet2( net1,
label = TRUE,
alpha = 0.9,
label.size = 3
,mode = c("xpos", "ypos")
,layout.exp = 0
,arrow.size = 12, arrow.gap = 0.025)
gdata
# ggnetwork
library(ggnetwork)
ggplot(net1, aes(x = x, y = y, xend = xend, yend = yend) ) +
geom_edges(arrow = arrow(length = unit(1, "pt"), type = "closed")
) +
geom_nodes(aes(x=xpos, y=ypos ), size = 4)