R中的图形G =(V,E) [英] Drawing graph G=(V,E) in R

查看:288
本文介绍了R中的图形G =(V,E)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ggplot或一些R内置函数在R中绘制标准G =(V,E)图.

I would like to draw standard G=(V,E) graph in R either by ggplot or some R built-in functions.

我有一个包含顶点坐标的数据框:

I have a data frame containing vertices coords:

> V
      x        y
1  589.3438 6422.883
2 8762.6921 7789.147
3 7973.0883 4552.745
4 4100.8408 8108.702
5 6049.3329 6547.239

和代表边缘的零一对称矩阵:

and a zero-one symetric matrix representing edges:

> E
       [,1] [,2] [,3] [,4] [,5]
 [1,]    0    0    0    1    0
 [2,]    0    0    1    0    1
 [3,]    0    1    0    0    1
 [4,]    1    0    0    0    1
 [5,]    0    1    1    1    0

我使用以下方法绘制顶点:

I plot vertices using:

plotGraph <- function() {
  qplot(x,
        y,
        data=V,
        xlim=c(0,SIZE),
        ylim=c(0,SIZE),
        main="Graph"
  )
}

如何在同一图上绘制图形边缘?或者如何绘制从(x1,y1)到(x2,y2)的一条边?

任何帮助将不胜感激.

推荐答案

编辑(2017年7月7日):

EDIT (July 7, 2017):

自从我最初回答问题以来,已经发布了新的改进的网络/图形绘图包ggraph,我认为它应该取代下面的选项,因此,我正在编辑我的答案以添加ggraph选项:

Since I have answered the question originally, a new and improved network/graph plotting package, ggraph, has been published, and I think it should supersede the options below, so I'm editing my answer to add the ggraph option:

首先,进行一些操作以将顶点和边作为igraph图形对象获得:

First, a bit of manipulation to get the vertices and edges as an igraph graph object:

library(igraph)
library(tidyverse)
library(ggraph)

V <- read.table(text = "x        y
                        589.3438 6422.883
                        8762.6921 7789.147
                        7973.0883 4552.745
                        4100.8408 8108.702
                        6049.3329 6547.239", 
    header = T) %>%
  rownames_to_column("name")

E <- matrix(c(0,    0,    0,    1,    0,
              0,    0,    1,    0,    1,
              0,    1,    0,    0,    1,
              1,    0,    0,    0,    1,
              0,    1,    1,    1,    0), nrow = 5, byrow = T) %>%
  data.frame() %>% 
  rename_all(list(function(x) 1:5)) %>% 
  rownames_to_column(var = "from") %>% 
  gather(to, val, 2:6) %>% 
  filter(val == 1) %>%
  select(from, to)

g <- graph_from_data_frame(E, vertices = V, directed = F)

现在出现了ggraph魔术.为了说明其功能,我将各种边缘和节点geom混合并匹配,以提供ggraph可能的采样.

now comes the ggraph magic. To illustrate its power, I've mixed and matched various edge and node geoms to provide a sampling of what's possible with ggraph.

ggraph(g) + 
  geom_edge_link() + 
  geom_node_label(aes(label = name))
#> Using `nicely` as default layout

ggraph(g) + 
  geom_edge_arc() + 
  geom_node_point()
#> Using `nicely` as default layout

ggraph(g) + 
  geom_edge_diagonal() + 
  geom_node_text(aes(label = name), color = "blue")
#> Using `nicely` as default layout

原始答案:

如果使用igraph是一种选择,我建议您使用.当使用图形时,这是一个非常有用的软件包.这是我使用igraph的方法:

If using igraph is an option, I would recommend it. It's a very useful package when working with graphs. Here's how I would do it using igraph:

library(igraph)

# convert V to a matrix and E to a graph
V <- data.matrix(V)
g <- graph_from_adjacency_matrix(E, mode="undirected")

plot.igraph(g, layout = V)

或者,如果要使用ggplot调味的方法,则可以使用GGally包中的ggnet2:

Alternatively, if you want a ggplot-flavored method, you can use ggnet2 from the GGally package:

library(GGally)

V <- data.matrix(V)
# with ggnet2 you don't have to convert E to a graph

ggnet2(net = E, mode = V ) 

这篇关于R中的图形G =(V,E)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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