gpt4 book ai didi

r - 在 R 中的 igraph 中,是否可以在顶点对象周围创建虚线?

转载 作者:行者123 更新时间:2023-12-04 11:56:27 24 4
gpt4 key购买 nike

在 R 中的 igraph 中,我目前有一个图,如下所示:

enter image description here

这是由代码制成的:

g <- make_undirected_graph(edges = c(1, 3, 2, 1, 2, 4, 3, 4, 4, 5), n = 5)

我想在顶点上添加虚线,它们是圆圈。有一个 edge.label选项,但没有 vertex.label选项。有没有另一种方法可以做到这一点?谢谢。

最佳答案

您可以定义自己的形状:https://igraph.org/r/doc/shapes.htmlhttps://r.789695.n4.nabble.com/Drawing-a-dotted-circle-td4655331.html 处给出了一个带有虚线边框的点的示例。 .在 https://lists.gnu.org/archive/html/igraph-help/2013-03/msg00030.html 中给出的创建新形状的完整示例.另请参阅 ?add_shape 中的更多示例.下面的示例调整了来自lists.gnu.org 的代码以组合所有内容。

新建函数 igraph形状

myimg <- function(coords, v=NULL, params) {
vertex.color <- params("vertex", "color")
if (length(vertex.color) != 1 && !is.null(v)) {
vertex.color <- vertex.color[v]
}
vertex.size <- 1/200 * params("vertex", "size")
if (length(vertex.size) != 1 && !is.null(v)) {
vertex.size <- vertex.size[v]
}
vertex.frame.color <- params("vertex", "frame.color")
if (length(vertex.frame.color) != 1 && !is.null(v)) {
vertex.frame.color <- vertex.frame.color[v]
}
vertex.frame.width <- params("vertex", "frame.width")
if (length(vertex.frame.width) != 1 && !is.null(v)) {
vertex.frame.width <- vertex.frame.width[v]
}
ltype <- params("vertex", "ltype")
if (length(ltype) != 1 && !is.null(v)) {
ltype <- ltype[v]
}

mapply(coords[,1], coords[,2], vertex.color, vertex.frame.color,
vertex.size, vertex.frame.width, ltype,
FUN=function(x, y, bg, fg, size, lwd, lty) {
symbols(x=x, y=y, bg=bg, fg=fg, lwd=lwd, lty=lty,
circles=size, add=TRUE, inches=FALSE)
})
}

然后你做 igraph使用 add_shape 识别形状.您可以使用 parameters 设置默认参数值争论。
library(igraph)

g <- make_undirected_graph(edges = c(1, 3, 2, 1, 2, 4, 3, 4, 4, 5), n = 5)

add_shape("myimg", plot=myimg,
parameters = list(
vertex.frame.color=1,
vertex.frame.width=1,
vertex.ltype=1))

然后绘图
plot(g,  vertex.shape="myimg", 
vertex.frame.color=1:5,
vertex.frame.width=5,
vertex.ltype=1:5,
vertex.color=6:10,
vertex.size=seq(50, 80, length=5))

要让所有的边框都加点,只需使用 vertex.ltype="dotted"vertex.ltype=3 .

enter image description here

关于r - 在 R 中的 igraph 中,是否可以在顶点对象周围创建虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58543917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com