2

我想做的是用 R 创建一个基本流程图,将其添加到我的 R Markdown 文件中并使其可引用(如果可能的话,就像我在代码块标题中对我的 ggplot2 图形 -> fig.cap = " " 所做的那样)。

流程图:

library(grid)
library(Gmisc)

grid.newpage()

# set some parameters to use repeatedly
leftx <- .25
midx <- .5
rightx <- .75
width <- .4
gp <- gpar(fill = "lightgrey")

# create boxes
(Pharmazie <- boxGrob("Verbrauchsdaten von der\n Spitalpharmazie (Excel-Tabelle)", 
                  x=leftx, y=0.876, box_gp = gp, width = width))

(Finanzen <- boxGrob("Belegzahlen vom Ressort\n Finanzen (Excel-Tabelle)", 
                 x=rightx, y=.876, box_gp = gp, width = width))

(A <- boxGrob("Import der Daten aus Excel ins\n Microsoft Access (Datenbanksoftware)", 
          x=midx, y=0.76, box_gp = gp, width = width))

(B <- boxGrob("Zusammenführen der Informationen\n und erstellen neuer, berechneter Tabellen", 
          x=midx, y=.64, box_gp = gp, width = width))

(C <- boxGrob("Export der neu erstellten Tabellen\n in Form von Excel-Tabellen", 
          x=midx, y=.52, box_gp = gp, width = width))

(D <- boxGrob("Import der neuen Tabellen in R", 
          x=midx, y=.414, box_gp = gp, width = width))

(E <- boxGrob("Berechnung und grafische Darstellung\n der Grafiken und Tabellen", 
          x=midx, y=.308, box_gp = gp, width = width))


connectGrob(Pharmazie, A, "L")
connectGrob(Finanzen, A, "L")
connectGrob(A, B, "N")
connectGrob(B, C, "N")
connectGrob(C, D, "N")
connectGrob(D, E, "N")

我遇到的问题是我找不到将流程图保存到变量/ png 文件的方法(稍后将其导入我的 R Markdown 文件),或者更好的是,将其直接包含在代码块中(它当我在代码块中执行代码与在脚本中执行代码时看起来不同,在尝试给它一个 fig.cap 时它也无法编织)。

它看起来有多么不同的示例:在此处输入图像描述

任何帮助将不胜感激!

PS我试图用“DiagrammeR”包制作流程图,但在我没有设法找到一种方法让每个框的文本超过一行后我放弃了(所以它不会那么宽泛)。

4

2 回答 2

2

作为参考png(),您可以轻松地将流程图保存到 png 文件中。其他格式也是可能的,例如pdf(). 下面是如何将小插图示例保存到 png 文件,尽管在运行代码时您不会在视口中看到它。注意dev.off()最后。

#r flowchart with gmisc
#https://cran.r-project.org/web/packages/Gmisc/vignettes/Grid-based_flowcharts.html

#install.packages("Gmisc")
library(Gmisc)
library(grid)
#example x

#get file ready to receive image
png("vignette flowchart.png", width=500, height = 500, units = "px")

grid.newpage()

# Initiate the boxes that we want to connect
side <- boxPropGrob("Side", "Left", "Right", 
                    prop=.3, 
                    x=0, y=.9,
                    bjust = c(0,1))

start <- boxGrob("Top", 
                 x=.6, y=coords(side)$y, 
                 box_gp = gpar(fill = "yellow"))

bottom <- boxGrob("Bottom", x=.6, y=0, 
                  bjust="bottom")


sub_side_left <- boxGrob("Left", 
                         x = coords(side)$left_x, 
                         y = 0,
                         bjust = "bottom")
sub_side_right <- boxGrob("Right", 
                          x = coords(side)$right_x, 
                          y = 0,
                          bjust = "bottom")

odd <- boxGrob("Odd\nbox", 
               x=coords(side)$right, 
               y=.5)

odd2 <- boxGrob("Also odd", 
                x=coords(odd)$right + 
                  distance(bottom, odd, type="h", half=TRUE) -
                  unit(2, "mm"), 
                y=0,
                bjust = c(1,0))

exclude <- boxGrob("Exclude:\n - Too sick\n - Prev. surgery", 
                   x=1, y=coords(bottom)$top + 
                     distance(start, bottom, 
                              type="v", half=TRUE), 
                   just="left", bjust = "right")

# Connect the boxes and print/plot them



connectGrob(start, bottom, "vertical")
connectGrob(start, side, "horizontal")
connectGrob(bottom, odd, "Z", "l")
connectGrob(odd, odd2, "N", "l")
connectGrob(side, sub_side_left, "v", "l")
connectGrob(side, sub_side_right, "v", "r")
connectGrob(start, exclude, "-", 
            lty_gp = gpar(lwd=2, col="darkred", fill="darkred"))

# Print the grobs
start
bottom
side
exclude
sub_side_left
sub_side_right
odd
odd2

#save and close file
dev.off()
于 2020-05-07T00:54:16.940 回答
1

它看起来不同,因为您正在绘制的视口不一样。您只需要稍微调整一下定位选项即可使其适合。下面我使用fig.widthfig.height创建了一个包装函数,在该函数中我还垂直对齐了框(顶端)。y这使得使用坐标从上到下构建图表变得更加容易。

---
output: pdf_document
---


```{r echo = F, message = F, fig.width=7, fig.height = 6}
library(grid)
library(Gmisc)

# grid.newpage()
# set some parameters to use repeatedly
leftx <- .2
midx <- .5
rightx <- .8

myBoxGrob <- function(text, ...) {
  boxGrob(label = text, bjust = "top", box_gp = gpar(fill = "lightgrey"), ...)
}

# create boxes
(Pharmazie <- myBoxGrob("Verbrauchsdaten von der\n Spitalpharmazie (Excel-Tabelle)", x=leftx, y=1, width = 0.38))
(Finanzen <- myBoxGrob("Belegzahlen vom Ressort\n Finanzen (Excel-Tabelle)", x=rightx, y=1, width = 0.38))
(A <- myBoxGrob("Import der Daten aus Excel ins\n Microsoft Access (Datenbanksoftware)", x=midx, y=0.87, width = 0.5))
(B <- myBoxGrob("Zusammenführen der Informationen\n und erstellen neuer, berechneter Tabellen", x=midx, y=.70, width = 0.5))
(C <- myBoxGrob("Export der neu erstellten Tabellen\n in Form von Excel-Tabellen", x=midx, y=.53, width = 0.5))
(D <- myBoxGrob("Import der neuen Tabellen in R", x=midx, y=.36,  width = 0.5))
(E <- myBoxGrob("Berechnung und grafische Darstellung\n der Grafiken und Tabellen", x=midx, y=.21, width = 0.5))


connectGrob(Pharmazie, A, "L")
connectGrob(Finanzen, A, "L")
connectGrob(A, B, "N")
connectGrob(B, C, "N")
connectGrob(C, D, "N")
connectGrob(D, E, "N")
```

在此处输入图像描述

out.width如果流程图是可接受的,您可以使用块选项(此处)缩放绘图=".5\\textwidth"

在此处输入图像描述

于 2019-01-08T13:08:39.183 回答