我想要做的是用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文件中)的方法,或者甚至更好地将其直接包含在代码中块(当我在代码块中执行代码时与在脚本中执行代码时看起来有所不同,在尝试给它一个无花果帽时它也无法编织)。
任何帮助将不胜感激!
P.S。我尝试使用“ DiagrammeR”软件包制作流程图,但在我找不到设法将文本放在每个框多行的方法之后放弃了。(这样就不会那么广泛了)。>
答案 0 :(得分:1)
它看起来有所不同,因为您要绘制的视口并不相同。您只需要稍微弄些定位选项即可使其适应。在下面,我使用fig.width
和fig.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"
)缩放绘图:
答案 1 :(得分:1)
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()