在ggnetwork中的弯曲边缘上定位标签

时间:2017-12-14 13:52:45

标签: r ggplot2 igraph

我正在使用r package ggnetwork绘制加权有向图。

我的目标是绘制一个标签,指示每条边的重量。 由于网络是定向的,我使用函数geom_edges()中的场曲率来避免重叠边缘。然后,我使用函数geom_edgetext()绘制每条边的权重。

我的问题是我不知道如何提供边缘的新坐标(在设置曲率之后)到geom_edgetext()

因此,标签的位置就像边缘没有弯曲一样。 任何的想法?

按照可重复的示例显示我到目前为止所处的位置:

# Load libraries
library(igraph)
library(ggplot2)
library(ggnetwork)

# Create adjacency matrix
m <- matrix(c(c(0,1,0,0,1,0,0),
               c(1,0,1,1,0,0,0),
               c(0,1,0,1,0,0,0),
               c(0,1,1,0,0,0,0),
               c(1,0,0,0,0,1,1),
               c(0,0,0,0,1,0,1),
               c(0,0,0,0,0,1,0)), nrow = 7, ncol = 7, byrow=T)

# Assign weights: column-normalize m
D <- diag(1/c(apply(m,2,sum)))
m <- m%*%D

# Create igraph object
g <- graph_from_adjacency_matrix(adjmatrix = m, mode = "directed", weighted = TRUE)

# Create ggnetwork object
n <- ggnetwork(g, layout = "fruchtermanreingold", cell.jitter = 0.25)

# Plot graph with weights
ggplot(n, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(size = 0.3, color = "grey50", arrow = arrow(length = unit(6,"pt")), curvature = 0.2) +
geom_nodes(size = 8, color = "orange") +
geom_edgetext(aes(label =  round(weight,2)), color = "grey25")+
theme_blank()+ theme(legend.position="none")

0 个答案:

没有答案