如何保存使用 grid 和 Gmisc 制作的流程图
How to save a flowchart made with grid and Gmisc
我想做的是用 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 时它也无法编织)。
外观差异示例:。
如有任何帮助,我们将不胜感激!
P.S。我试图用包 "DiagrammeR" 制作流程图,但在我没能找到一种方法让每个框的文本多于一行(所以它不会那么宽)后我放弃了。
看起来不一样,因为你绘制的视口不一样。您只需要 fiddle 稍微调整一下定位选项即可使其适合。下面我使用了 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"
)缩放绘图:
供参考 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()
我想做的是用 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 时它也无法编织)。
外观差异示例:
如有任何帮助,我们将不胜感激!
P.S。我试图用包 "DiagrammeR" 制作流程图,但在我没能找到一种方法让每个框的文本多于一行(所以它不会那么宽)后我放弃了。
看起来不一样,因为你绘制的视口不一样。您只需要 fiddle 稍微调整一下定位选项即可使其适合。下面我使用了 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"
)缩放绘图:
供参考 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()