我正在尝试使用 ggplot2 绘制igraph网络。我使用geom_point()
作为顶点,geom_line()
作为边。 ggplot2 中的磅值和线宽均由大小指定。当我单独指定它们时,我希望得到两个单独的传说,但结果却被合并了。以下是一个例子。
# an example graph
df <- data.frame(from=letters[c(1, 1, 2:4, 4)], to=letters[c(2, 3, 5:7, 1)])
g <- graph.data.frame(df, directed=FALSE)
Vname <- V(g)$name
set.seed(100)
V(g)$varVsize <- rnorm(length(Vname))
E(g)$varEwidth <- rnorm(nrow(df))
E(g)$varEcol <- rnorm(nrow(df), 1, 1)
Vcord <- layout.fruchterman.reingold(g) # get coordinates for the Vertex
rownames(Vcord) <- V(g)$name
# 2-column edge list
el <- get.edgelist(g)
el_df <- data.frame(as.data.frame(el), Ewidth=E(g)$varEwidth, Ecol=E(g)$varEcol)
# assigning edge id so that it can be used as group
el_df$id <- 1:nrow(el_df)
## now g_df have each node in the edgelist as a row: first nodesFrom (id=1:145), then nodesTo (id=146:290, i.e.)
el_dfm <- melt(el_df, id=3:5)
xy_s <- data.frame(value = rownames(Vcord),
Vsize=V(g)$varVsize, # add Vetex attribues here: affects size and color
x = Vcord[, 1],
y = Vcord[, 2])
g_df2 <- merge(el_dfm, xy_s, by = "value")
# fig 1
ggplot(g_df2, aes(x, y)) +
geom_point(data=g_df2, aes(x, y, size=Vsize), alpha=0.4) +
geom_line(aes(color=Ecol, group = id)) +
geom_text(size = 4, aes(label = value), colour='blue')
# fig 2: Vsize and Ewidth legend are merged: transparency in geom_line is used so it will not overlap geom_point
ggplot(g_df2, aes(x, y)) +
geom_point(data=g_df2, aes(x, y, size=Vsize), alpha=0.4) +
geom_line(aes(color=Ecol, group = id, size=Ewidth), alpha=0.3) +
geom_text(size = 4, aes(label = value), colour='blue')