如何用igraph绘制椭圆节点? [英] How to plot a ellipse node with igraph?

查看:112
本文介绍了如何用igraph绘制椭圆节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在SO中从未问过这样的问题.但是,igraph的有关自定义节点形状的帮助页面相当模糊.有人可以在igraph中提供自定义节点形状的完整示例吗?

It seems no such questions has ever been asked in SO. However, the help page of igraph on customizing node shapes is rather vague. Can someone provide a complete example of customizing node shape in igraph?

推荐答案

您没有说使用什么语言,所以我将用R来回答.

You do not say what language you use, so I am going to respond in R.

内置的形状可以用shapes()列出.不幸的是,椭圆不在其中.帮助页面?shapes提供了一些有关如何添加其他节点形状的示例-三角形,星形和添加图像.下面的响应是对示例代码的直接修改,以添加三角形.有几种绘制椭圆的功能.我使用了plotrix软件包中的一个.

The shapes that are built-in can be listed with shapes(). Unfortunately, ellipse is not among them. The help page ?shapes gives a few examples of how to add additional node shapes - a triangle, a star and adding an image. The response below is a straightforward modification of the example code for adding a triangle. There are several functions for drawing ellipses. I used the one from the plotrix package.

library(igraph)
library(plotrix)

## Need a graph as an example
set.seed(1)
N10 = erdos.renyi.game(10, 0.31)

## Function for plotting an elliptical node
myellipse <- 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/30 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }

  draw.ellipse(x=coords[,1], y=coords[,2],
    a = vertex.size, b=vertex.size/2, col=vertex.color)
}

## Register the shape with igraph
add_shape("ellipse", clip=shapes("circle")$clip,
                 plot=myellipse)

## Plot it, with different sizes and colors to illustrate
plot(N10, vertex.shape="ellipse", vertex.color=rainbow(vcount(N10)),
     vertex.size=(2:11)/2)

瞧.

这篇关于如何用igraph绘制椭圆节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆