手动绘制图形

his*_*eim 11 plot drawing r figure diagrammer

我生成了一个图表:

library(DiagrammeR)
grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [layout = neato, overlap = true, fontsize = 10, outputorder = edgesfirst]

  # several 'node' statements
  node [shape = circle,
  fontname = Helvetica]
  A [pos = '1,1!']; 
  B [pos = '0,2!']; 
  C [pos = '1.5,3!']; 
  D [pos = '2.5,1!']; 
  E [pos = '4,1!']; 
  F [pos = '4,2!']; 
  G [pos = '5,1!']; 
  H [pos = '6,2!']; 
  I [pos = '1.5,-0.1!'];

  # several 'edge' statements
  A->B B->C
  D->E D->F E->F E->G F->G G->H F->H
  }
  ")
Run Code Online (Sandbox Code Playgroud)

哪个产生:

在此输入图像描述

现在我想在节点A,B和C周围画一个带虚线的方框.

我怎样才能在R中实现这一目标?该解决方案的一个关键要求是它是可重现的,即我可以多次运行脚本并获得相同的结果.

Whi*_*ing 6

这是另一种基于igraph的方法.它受到这个igraph 代码示例的启发.

我假设使用igraph而不是DiagrammeR是一种选择 - 也许情况并非如此......

我们将顶点的定位留给标准布局算法,并查询它以得到顶点位置.然后使用这些位置围绕任意一组"选定"顶点绘制虚线矩形.无需用户交互.

我们从图形拓扑开始.

library(igraph)

set.seed(42)

df <- data.frame(from = c('A', 'B', 'I', 'D', 'D', 'E', 'E', 'F', 'F', 'G'),
                 to = c('B', 'C', 'I', 'E', 'F', 'G', 'F', 'H', 'G', 'H'))

g <- graph.data.frame(df, directed = TRUE)
Run Code Online (Sandbox Code Playgroud)

根据品味,可以自由设置图中顶点和箭头的大小.

vertexsize <- 50
arrowsize <- 0.2
Run Code Online (Sandbox Code Playgroud)

我们要求Fruchterman-Reingold布局引擎计算顶点的坐标.

coords <- layout_with_fr(g)
Run Code Online (Sandbox Code Playgroud)

然后绘制图表.

plot(g,
     layout = coords,
     vertex.size = vertexsize,
     edge.arrow.size = arrowsize,
     rescale = FALSE,
     xlim = range(coords[,1]),
     ylim = range(coords[,2]))
Run Code Online (Sandbox Code Playgroud)

如果我们想看看发生了什么,我们可以添加坐标轴并打印顶点坐标:

axis(1)
axis(2)

V(g) # ordered vertex list
coords # coordinates of the vertices (in the same coordinate system as our dotted rectangle)
Run Code Online (Sandbox Code Playgroud)

我们现在弄清楚我们想要一个矩形的顶点的边界框.

selectedVertices = c("A", "B", "C")
vertexIndices <- sapply(selectedVertices, FUN = function(x) { return(as.numeric(V(g)[x])) } )
llx <- min(coords[vertexIndices, 1])
lly <- min(coords[vertexIndices, 2])
urx <- max(coords[vertexIndices, 1])
ury <- max(coords[vertexIndices, 2])
Run Code Online (Sandbox Code Playgroud)

差不多了.我们已经在coords []中得到了顶点中心的坐标,但我们还需要plot()坐标系中顶点的大小.从plot.igraph源代码中我们可以看到plot()的vertex.size选项除以200,然后用作绘制顶点的半径.在绘制虚线矩形时,我们使用50%更大的值作为顶点坐标边界框周围的边距.

margin <- (vertexsize / 200) * 1.5
rect(llx - margin, lly - margin, urx + margin, ury + margin, lty = 'dotted')
Run Code Online (Sandbox Code Playgroud)

这是我们得到的结果:

在此输入图像描述


raw*_*awr 5

您可以使用@ StevenBeaupre的小部件解决方案,但是有一些使用R图形绘制网络的软件包.一个是igraph如果您愿意使用其他解决方案.

这将使图表

library('igraph')
set.seed(11)
g <- data.frame(from = c('A', 'B', 'I', 'D', 'D', 'E', 'E', 'F', 'F', 'G'),
                to = c('B', 'C', 'I', 'E', 'F', 'G', 'F', 'H', 'G', 'H'))
(gg <- graph.data.frame(g, directed = TRUE))
plot(gg, vertex.color = 'white')
Run Code Online (Sandbox Code Playgroud)

并且有很多方法可以为r图形添加一个盒子; 在这里您可以单击绘图添加框而无需计算任何内容

rekt <- function(...) {
  coords <- c(unlist(locator(1)), unlist(locator(1)))
  rect(coords[1], coords[2], coords[3], coords[4], ..., xpd = NA)
}

rekt(border = 'red', lty = 'dotted', lwd = 2)
Run Code Online (Sandbox Code Playgroud)

我明白了

在此输入图像描述