【问题标题】:How to save a flowchart made with grid and Gmisc如何保存使用网格和 Gmisc 制作的流程图
【发布时间】:2019-01-08 00:18:32
【问题描述】:

我想做的是用 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 时它也无法编织)。

外观差异示例:

任何帮助将不胜感激!

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

【问题讨论】:

    标签: r grid r-markdown flowchart gmisc


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

      看起来不一样,因为您绘制的视口不一样。您只需要稍微调整一下定位选项即可使其适合。下面我使用了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")缩放绘图:

      【讨论】:

      • 谢谢,看起来已经很不错了! fig.cap 似乎又可以工作了!有没有办法在不扭曲它的情况下裁剪它(当我试图改变 fig.width 和 height 时发生这种情况)?如果可能的话,我希望将流程图放在页面右侧,并在左侧添加一些文本。
      • 如果你的意思是缩放而不是裁剪,试试out.width(见更新)。
      • 完美,谢谢!我还有一个更进一步的问题:这个数字现在是我想要的大小(它是 70%)并且右对齐(@9​​87654333@)。 fig.cap 仍然在图形下方的页面中间(而不是相对于图形居中),并且文本不使用图形左侧的空间。你知道如何解决这个问题吗?我已经尝试了一些类似link 的方法,但我无法让它工作。
      • 我确实有一个解决方案。但我想请您提出另一个问题,因为在一个问题中详细说明进一步的问题并不是一个好习惯。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      相关资源
      最近更新 更多