【问题标题】:How do you resize grViz object in the DiagrammeR package你如何在 DiagrammeR 包中调整 grViz 对象的大小
【发布时间】:2021-05-26 08:57:24
【问题描述】:

我正在努力调整 DiagrammeR 包中的 grViz 对象的大小。例如,我希望对象的大小为 18 厘米宽和 14 厘米高。当我尝试包装中的高度和宽度属性时,我似乎没有得到正确的测量结果。 或者,有没有办法将 grViz 保存为图像,这样就可以在 Rmarkdown 中拉取该图像?

代码如下:

---
title: "Flowchart on Trial"
author: "Moses Otieno"
date: "25/05/2021"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(DiagrammeR) # Flowchart
```


```{r test}
nodedata2 <- list(a = 1000, b = 940, c=900, d= 500, e=400, f = 230, g = 260, h = 10, i = 180, j = 190, k = 30 )

 grViz("digraph flowchart {
      # node definitions with substituted label text
      node [fontname = Helvetica, shape = rectangle]        
      tab1 [label = '@@1']
      tab2 [label = '@@2']
      tab3 [label = '@@3']
      tab4 [label = '@@4']
      tab5 [label = '@@5']
      tab6 [label = '@@6']
      tab7 [label = '@@7']
      tab8 [label = '@@8']
      tab9 [label = '@@9']
      tab10 [label = '@@10']
      tab11 [label = '@@11']


      # edge definitions with the node IDs
      tab1 -> tab2;
      tab2 -> tab3;
      tab3 -> {tab4 tab5}; 
      tab4 -> {tab6 tab7 tab8};
      tab5 -> {tab9 tab10 tab11};

      }

      [1]: paste('Sampled n=', nodedata2$a,  'participants')
      [2]: paste('Contacted ', nodedata2$b)
      [3]: paste('Enrolled n=', nodedata2$c)
      [4]: paste('Male  n=',nodedata2$d)
      [5]: paste('Female n=',nodedata2$e) 
      [6]: paste('Drug A n=', nodedata2$f)
      [7]: paste('Drug B n= ',  nodedata2$g)
      [8]: paste('Refusals n=', nodedata2$h)
      [9]: paste('Drug A n=', nodedata2$i)
      [10]: paste('Drug B n=', nodedata2$j)
      [11]: paste('Refusals n=', nodedata2$k)
      
      ")




```

```

【问题讨论】:

    标签: r diagrammer


    【解决方案1】:

    您可以使用 - 将其保存为图像

    library(rsvg)
    library(DiagrammeRsvg)
    
    plot %>% export_svg %>% charToRaw %>% rsvg_png("graph.png")
    

    其中plotgrViz 图。您也可以在rsvg_png中指定高度和宽度。

    【讨论】:

    • export_svg(.) 中的错误:找不到函数“export_svg”
    • 我只能看到两个导出函数export_csv和export_graph_
    • 我在包DiagrammeRsvg 中只看到一个函数export_svg。它也出现在包的 github 页面上github.com/rich-iannone/DiagrammeRsvg/blob/master/R/… 尝试将包更新到最新版本。该代码对我有用。
    • 成功了!我没有加载 DiagrammeRsvg
    猜你喜欢
    • 2018-12-13
    • 2018-09-04
    • 2019-03-15
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    相关资源
    最近更新 更多