R

时间:2017-01-19 21:13:57

标签: r graph

我希望沿着线的长度制作一个带箭头(或类似的V形)的定向网络图...

enter image description here

igraph库似乎使用基本polygon函数,它接受lty来指定行类型,但这些仅限于各种破折号。

有没有办法制作自定义符号(甚至使用pch中的三角形)在R中形成一条线?

制作图表的最小代码:

require(igraph)
gr = graph_from_literal( A -+ B -+ C )
plot(gr,edge.curved=TRUE)
顺便说一句,如果支持这个,我可以使用另一个网络分析库。我问ggraph的开发人员,他说不可能做到这一点。

2 个答案:

答案 0 :(得分:9)

CytoscapeRCy3库用于创建网络图。

安装cytoscape版本3及更高版本from here。然后启动cytoscape GUI(图形用户界面)会话。

此答案中使用的

版本 cytoscape: 3.4.0 RCy3: 1.5.2

操作系统:Windows-7

# load libraries
library('RCy3')

# create cytoscape connection
cy <- RCy3::CytoscapeConnection()
RCy3::deleteAllWindows(cy)       # delete all windows in cytoscape
hideAllPanels(cy)                # hide all panels

# create node and edge data and create graphNEL object
node.tbl <- data.frame(Node.Name = c('A', 'B', 'C'))
edge.tbl <- data.frame(Gene.1 = c('A', 'B'),
                       Gene.2 = c('B', 'C'))
g <- cyPlot(node.tbl, edge.tbl)
g
# A graphNEL graph with directed edges
# Number of Nodes = 3 
# Number of Edges = 2 

# create cytoscape window and display the graph
window_title <- 'example'
cw <- RCy3::CytoscapeWindow(window_title, graph=g, overwrite=FALSE)
RCy3::displayGraph(cw)

# set visual style and layout algorithm
vis_style <- 'Curved'               # getVisualStyleNames(cw)[7]
RCy3::setVisualStyle(cw, vis_style)       
RCy3::layoutNetwork(obj = cw, layout.name = "circular")
RCy3::layoutNetwork(obj = cw, layout.name = "kamada-kawai")

# get all edges
getAllEdges(cw)
# [1] "A (unspecified) B" "B (unspecified) C"

# get cytoscape supported line types
supported_styles <- getLineStyles (cw)
supported_styles
# [1] "EQUAL_DASH"       "PARALLEL_LINES"   "MARQUEE_DASH"     "MARQUEE_EQUAL"    "SOLID"            "FORWARD_SLASH"    "DASH_DOT"         "MARQUEE_DASH_DOT"
# [9] "SEPARATE_ARROW"   "VERTICAL_SLASH"   "DOT"              "BACKWARD_SLASH"   "SINEWAVE"         "ZIGZAG"           "LONG_DASH"        "CONTIGUOUS_ARROW"

# set edge line type
setEdgeLineStyleDirect(cw, "A (unspecified) B", supported_styles [16])  # "CONTIGUOUS_ARROW"

enter image description here

setEdgeLineStyleDirect(cw, "A (unspecified) B", supported_styles [9])   # "SEPARATE_ARROW"

enter image description here

# save network as image in the current working directory
fitContent (cw)
setZoom(cw, getZoom(cw) - 1)  # adjust the value from 1 to a desired number to prevent cropping of network diagram
saveImage(obj = cw, 
          file.name = 'example',
          image.type = 'png',
          h = 700)

有关详细信息,请阅读?RCy3::setEdgeLineStyleDirect

另外,对于cytoscape边缘属性,请参阅here

安装RCy3:

# Install RCy3 - Interface between R and Cytoscape (through cyRest app)
library('devtools')

remove.packages("BiocInstaller")
source("https://bioconductor.org/biocLite.R")
biocLite("BiocGenerics")
biocLite("bitops")
install_github("tmuetze/Bioconductor_RCy3_the_new_RCytoscape")

答案 1 :(得分:7)

要使用igraph执行此操作,我认为您需要深入了解plot.igraph函数,找出它为顶点之间的曲线边缘生成Bezier曲线的位置然后使用插值来获得沿这些边的点。使用该信息,您可以沿图形边缘绘制箭头段。

这是一种不同的方法,并不完全符合您的要求,但我希望可以满足您的需求。我将使用igraph以及ggplot2包中的ggnet2函数,而不是GGally

基本方法是获取每个图形边缘的端点坐标,然后沿着每个边缘插入点,我们将绘制箭头线段。请注意,边缘是直线,因为ggnet2不支持弯曲边缘。

library(ggplot2)
library(GGally)

# Create an adjacency matrix that we'll turn into a network graph
m = matrix(c(0,1,0,0,
             0,0,1,0,
             1,0,0,1,
             0,0,0,0), byrow=TRUE, nrow=4)

# Plot adjacency matrix as a directed network graph
set.seed(2)
p = ggnet2(network(m, directed=TRUE), label=TRUE, arrow.gap=0.03)

这是图表的样子:

also receives the properties of the generic type it's encapsulating

现在我们想要沿每条边添加箭头。为此,我们首先需要找出每条边的终点坐标。我们可以使用ggplot_build

从图表对象中获取
gg = ggplot_build(p) 

图表数据存储在gg$data

gg$data
[[1]]
           x        xend          y       yend PANEL group colour size linetype alpha
1 0.48473786 0.145219576 0.29929766 0.97320807     1    -1 grey50 0.25    solid     1
2 0.12773544 0.003986273 0.97026602 0.04720945     1    -1 grey50 0.25    solid     1
3 0.02670486 0.471530869 0.03114479 0.25883640     1    -1 grey50 0.25    solid     1
4 0.52459870 0.973637028 0.25818813 0.01431760     1    -1 grey50 0.25    solid     1

[[2]]
  alpha colour shape size         x          y PANEL group fill stroke
1     1 grey75    19    9 0.1317217 1.00000000     1     1   NA    0.5
2     1 grey75    19    9 0.0000000 0.01747546     1     1   NA    0.5
3     1 grey75    19    9 0.4982357 0.27250573     1     1   NA    0.5
4     1 grey75    19    9 1.0000000 0.00000000     1     1   NA    0.5

[[3]]
          x          y PANEL group colour size angle hjust vjust alpha family fontface lineheight label
1 0.1317217 1.00000000     1    -1  black  4.5     0   0.5   0.5     1               1        1.2     1
2 0.0000000 0.01747546     1    -1  black  4.5     0   0.5   0.5     1               1        1.2     2
3 0.4982357 0.27250573     1    -1  black  4.5     0   0.5   0.5     1               1        1.2     3
4 1.0000000 0.00000000     1    -1  black  4.5     0   0.5   0.5     1               1        1.2     4

在上面的输出中,我们可以看到gg$data[[1]]的前四列包含每条边的终点坐标(并且它们的有向图的顺序正确)。

现在我们有了每条边的端点,我们可以在两个端点之间插入点,在这两个端点处我们将在末尾用箭头绘制线段。以下功能可以解决这个问题。它为每个边缘采用一个端点数据框,并返回一个调用geom_segment(每个图形边缘一个)的调用列表,用于绘制n箭头段。然后,可以将geom_segment个调用列表直接添加到我们的原始网络图表中。

# Function that interpolates points between each edge in the graph, 
#  puts those points in a data frame, 
#  and uses that data frame to return a call to geom_segment to add the arrow heads
add.arrows = function(data, n=10, arrow.length=0.1, col="grey50") {
  lapply(1:nrow(data), function(i) {

    # Get coordinates of edge end points
    x = as.numeric(data[i,1:4])

    # Interpolate between the end points and put result in a data frame
    # n determines the number of interpolation points
    xp=seq(x[1],x[2],length.out=n)
    yp=approxfun(x[c(1,2)],x[c(3,4)])(seq(x[1],x[2],length.out=n))

    df = data.frame(x=xp[-n], xend=xp[-1], y=yp[-n], yend=yp[-1])

    # Create a ggplot2 geom_segment call with n arrow segments along a graph edge
    geom_segment(data=df, aes(x=x,xend=xend,y=y,yend=yend), colour=col,
                 arrow=arrow(length=unit(arrow.length,"inches"), type="closed"))
  }) 
}

现在让我们运行该功能,将箭头添加到原始网络图

p = p + add.arrows(gg$data[[1]], 15)

以下是图表的样子:

enter image description here