- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用ggraph
包绘制网络并指定每个节点的坐标。虽然我可以使用 igraph
包来实现这一点,但我无法使用 ggraph
包来实现这一点。
# 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))
}
这是绘制 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")
ggraph
文档的链接是 here 。另请参阅 ggraph
的 GitHub 存储库以获取软件包安装说明(需要 >R.3.3 版本)。下面是有效的 ggraph 图(但我没有指定每个节点的坐标):
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()
这是我尝试为每个节点制作具有指定坐标的ggraph
图。遵循类似的示例和早期在 ggraph()
中的尝试(将节点的坐标传递到绘图上),我尝试了以下操作:
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))
还尝试使用this function 。这很困难,因为文档中没有示例。
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()
最佳答案
我不认为你直接调用layout_igraph_manual
是作者的意图。如果将布局命名为“手动”,则可以传入手动布局顶点所需的参数。如果我理解正确的话,这让我明白了你所追求的:
coords <- coord(g1)
names(coords) <- c("x","y")
coords <- as.data.frame(coords)
ggraph(g1, layout = "manual", circular = FALSE, node.positions = coords)+
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()
根据文档,重要的是您的手动布局是一个数据框并包含列 x
和 y
。我已经对您的 coord() 函数的输出执行了此操作。您可能只想更新它以返回该表单的数据。
关于r - ggraph网络图: specify node coordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048671/
我在安装 ggraph 时遇到了一些问题,我尝试了不同的方法,例如:How to install GGRAPH package to the latest R (v.3.3.2)或者 R on OSX
我有一个多波的友谊网络数据集。我想绘制在波浪中持续存在的友谊,并使每个图的节点保持相同的坐标。我能够使用 ggraph 和 graphlayouts 的动态布局来使节点在四波中保持在相同的位置,但我想
正在阅读 Julia Silge 和 David Robinson 的 Tidytext Mining with R - https://www.tidytextmining.com/nasa.htm
当使用 ggraph 时,有没有办法加粗边缘颜色的图例线?我试图覆盖但无济于事。这是一个例子: library(tidyverse) library(igraph) library(ggraph) s
正在阅读 Julia Silge 和 David Robinson 的 Tidytext Mining with R - https://www.tidytextmining.com/nasa.htm
我想使用分面绘制图形,其中面板之间的边缘不同。面板按字母顺序自动排序(按照 ggplot 中的惯例)。一个简单的例子: library(igraph) library(ggraph) g <- mak
我是 ggraph 的新手,为了让图例正确显示节点的颜色,我费了一番功夫。我有以下示例数据集: nodes % as_tbl_graph() ggraph( graph, layout = 'fr'
我正在使用 tidygraph 和 ggraph 绘制网络。有没有办法选择性地操作节点?具体来说,尺寸和颜色,分开。 # example data rstat_nodes % mu
在 ggraph 中,如果绘图是径向的,无论是否使用 repel=T,标签都会变得拥挤。 有没有办法使标签具有交互性或允许旋转图形以读取标签? library(ggraph) mtcarsDen <-
我试图用 ggraph 绘制一个网络,我想在图形周围添加一个圆圈,边缘和节点位于圆圈内。 使用以下代码绘制圆圈效果很好(改编自 Draw a circle with ggplot2 ) gg_circ
我正在尝试以圆形布局生成带有标记节点的交互网络图。我拼凑了一个功能脚本,显示了我想要的与 ggraph 的连接,但我无法弄清楚如何相对于圆形排列中的位置定位节点标签,因此它们不会与任何连接线重叠地点。
我想弄清楚如何在 ggraph 中使用经纬度布局但似乎无法通过语法工作。考虑使用从 iris 修改的一些数据的这个 reprex数据集: data <- structure(list(Sepal.Le
我在理解 ggplot2 和 ggraph 在分面方面的区别时遇到了一些困难。考虑以下数据: library(igraph) library(ggraph) library(ggplot2) df
我正在尝试制作一个示例 self 网络图,但使用“填充”为节点着色对我来说不起作用。这可能是一个微不足道的问题,但我无法找出问题所在。这是我的代码: library(tidyverse) librar
我正在尝试制作一个示例 self 网络图,但使用“填充”为节点着色对我来说不起作用。这可能是一个微不足道的问题,但我无法找出问题所在。这是我的代码: library(tidyverse) librar
我有一个小部件表;每个小部件都有唯一的 ID、颜色和类别。我想在 ggraph 中制作此表的 circlepack 图,该图在类别上分面,层次结构类别 > 颜色 > 小部件 ID: 问题是根节点。在此
我对 R 非常陌生,我需要 ggraph 库,但无法从 rstudio 控制台安装它。这是一条消息: Warning in install.packages : package ‘ggraph’ is
我是一名优秀的程序员,十分优秀!