我可以理解你的要求,将在代码部分以外的文本进行翻译。

huangapple go评论117阅读模式
英文:

How can I draw a flow chart in ggplot2?

问题

I can help you with the translation. Here's the translated content:

我想要在R中使用ggplot2包绘制这种类型的流程图。

我可以理解你的要求,将在代码部分以外的文本进行翻译。

我尝试使用ggflowchart包。我的代码如下:

library(tidyverse)
library(ggflowchart)
library(rcartocolor)
workflow <- tibble::tibble(from = c("RMD","MD","MD","MD","LaTeX"), to = c("MD","LaTeX","HTML","WORD","PDF"))
ggflowchart(workflow)
node_data <- tibble::tibble(name = c("RMD","MD","LaTeX","HTML","WORD","PDF"), label = c("RMD","MD","LaTeX","HTML","WORD","PDF"))
ggflowchart(workflow,node_data)                          
quarto <- ggflowchart(workflow,
            node_data,
            colour = "#585c45",
            text_colour = "#585c45",
            arrow_colour = "#585c45"
)+
  scale_fill_carto_d(palette = "Antique") +
  labs(title = "R Markdown工作流程") +
  theme(
    legend.position = "none",
    plot.background = element_rect(
      colour = "#f2e4c1",
      fill = "#f2e4c1"
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = "bold",
      colour = "#585c45"
    )
  )

我希望图表从左到右流动(即,左侧为“RMD”节点,右侧为“PDF”节点)。但是在这个代码中,它是从上到下流动的。我还希望沿箭头旁边有文字,就像上面的链接中所示。我该如何做?

英文:

I want to draw this kind of flow chart in R using ggplot2 package.

我可以理解你的要求,将在代码部分以外的文本进行翻译。

I tried using ggflowchart package. My code is as follows:

library(tidyverse)
library(ggflowchart)
library(rcartocolor)
workflow &lt;- tibble::tibble(from = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;), to = c(&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;))
ggflowchart(workflow)
node_data &lt;- tibble::tibble(name = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;), label = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;))
ggflowchart(workflow,node_data)                          
quarto &lt;- ggflowchart(workflow,
            node_data,
            colour = &quot;#585c45&quot;,
            text_colour = &quot;#585c45&quot;,
            arrow_colour = &quot;#585c45&quot;
)+
  scale_fill_carto_d(palette = &quot;Antique&quot;) +
  labs(title = &quot;R Markdown Workflow&quot;) +
  theme(
    legend.position = &quot;none&quot;,
    plot.background = element_rect(
      colour = &quot;#f2e4c1&quot;,
      fill = &quot;#f2e4c1&quot;
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = &quot;bold&quot;,
      colour = &quot;#585c45&quot;
    )
  )

I want the diagram to flow from left to right (i.e., the "RMD" node on left, and "PDF" on right). But in this code, it flows from up to down. I also want the writings along side the arrows, as shown in the link above. How can I do this?

答案1

得分: 3

I have no experience with ggflowchart(). Here is an alternative suggestion using DiagrammR:

library(DiagrammeR)

flow_chart <- grViz("digraph {
  
  # horizontal alignment
  graph [rankdir = LR]
  
  # Nodes with labels
  node [shape = box]
  RMD [label = 'RMD']
  MD [label = 'MD']
  LaTeX [label = 'LaTeX']
  HTML [label = 'HTML']
  WORD [label = 'WORD']
  PDF [label = 'PDF']
  
  # Edges with labels
  RMD -> MD [label = 'knit']
  MD -> LaTeX [label = 'pandoc']
  MD -> HTML [label = 'pandoc']
  MD -> WORD [label = 'pandoc']
  LaTeX -> PDF [label = 'pdflatex']
}")

flow_chart

我可以理解你的要求,将在代码部分以外的文本进行翻译。

英文:

I have no experience with ggflowchart(). Here is an alternative suggestion udinh DiagrammR:

library(DiagrammeR)

flow_chart &lt;- grViz(&quot;digraph {
  
  # horizontal alignment
  graph [rankdir = LR]
  
  # Nodes with labels
  node [shape = box]
  RMD [label = &#39;RMD&#39;]
  MD [label = &#39;MD&#39;]
  LaTeX [label = &#39;LaTeX&#39;]
  HTML [label = &#39;HTML&#39;]
  WORD [label = &#39;WORD&#39;]
  PDF [label = &#39;PDF&#39;]
  
  # Edges with labels
  RMD -&gt; MD [label = &#39;knit&#39;]
  MD -&gt; LaTeX [label = &#39;pandoc&#39;]
  MD -&gt; HTML [label = &#39;pandoc&#39;]
  MD -&gt; WORD [label = &#39;pandoc&#39;]
  LaTeX -&gt; PDF [label = &#39;pdflatex&#39;]
}&quot;)

flow_chart

我可以理解你的要求,将在代码部分以外的文本进行翻译。

答案2

得分: 2

在这个阶段,我认为在ggflowchart包内部无法在箭头旁边添加文本,也许DiagrammeR是一个替代选项。

要启用水平流程,只需使用horizontal = TRUE。使用annotate在ggplot上放置自定义标签(注意我示例中第一个箭头上的“labels”),但这将是非常手动的。

library(ggflowchart)
library(rcartocolor)
library(tidyverse)

workflow <- tibble::tibble(from = c("RMD","MD","MD","MD","LaTeX"), to = c("MD","LaTeX","HTML","WORD","PDF"))
node_data <- tibble::tibble(name = c("RMD","MD","LaTeX","HTML","WORD","PDF"), label = c("RMD","MD","LaTeX","HTML","WORD","PDF"))
               
quarto <- ggflowchart(workflow,
                      node_data,
                      colour = "#585c45",
                      text_colour = "#585c45",
                      arrow_colour = "#585c45",
                      horizontal = TRUE
)+
  scale_fill_carto_d(palette = "Antique") +
  labs(title = "R Markdown Workflow") +
  theme(
    legend.position = "none",
    plot.background = element_rect(
      colour = "#f2e4c1",
      fill = "#f2e4c1"
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = "bold",
      colour = "#585c45"
    )
  ) +
  annotate(geom = "text", x = 0.1, y = 2.5, label = "labels")
quarto
英文:

At this stage I don't think it's possible to add text next to the arrows within the ggflowchart package, maybe DiagrammeR is an alternative option for that.

To enable horizontal flow, just use horizontal = TRUE. Use annotate to place custom labels onto the ggplot (note the "labels" on the first arrow in my example), but that would be very manual.

library(ggflowchart)
library(rcartocolor)
library(tidyverse)

workflow &lt;- tibble::tibble(from = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;), to = c(&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;))
node_data &lt;- tibble::tibble(name = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;), label = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;))
               
quarto &lt;- ggflowchart(workflow,
                      node_data,
                      colour = &quot;#585c45&quot;,
                      text_colour = &quot;#585c45&quot;,
                      arrow_colour = &quot;#585c45&quot;,
                      horizontal = T
)+
  scale_fill_carto_d(palette = &quot;Antique&quot;) +
  labs(title = &quot;R Markdown Workflow&quot;) +
  theme(
    legend.position = &quot;none&quot;,
    plot.background = element_rect(
      colour = &quot;#f2e4c1&quot;,
      fill = &quot;#f2e4c1&quot;
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = &quot;bold&quot;,
      colour = &quot;#585c45&quot;
    )
  ) +
  annotate(geom = &quot;text&quot;, x = 0.1, y = 2.5, label = &quot;labels&quot;)
quarto

我可以理解你的要求,将在代码部分以外的文本进行翻译。<!-- -->

<sup>Created on 2023-05-14 with reprex v2.0.2</sup>

答案3

得分: 0

With the current dev version of {ggflowchart} (version >= 1.0.0.9006), it's possible to directly add arrow labels. Install dev version:

remotes::install_github("nrennie/ggflowchart")

You can add arrow labels by adding a label column to the data argument. The colour of the labels can be edited using arrow_label_fill:

library(tidyverse)
library(ggflowchart)
library(rcartocolor)
workflow <- tibble::tibble(from = c("RMD","MD","MD","MD","LaTeX"),
                           to = c("MD","LaTeX","HTML","WORD","PDF"),
                           label = c("knitr", NA, NA, NA, NA))
node_data <- tibble::tibble(name = c("RMD","MD","LaTeX","HTML","WORD","PDF"),
                            label = c("RMD","MD","LaTeX","HTML","WORD","PDF"))

ggflowchart(workflow,node_data, horizontal = TRUE, arrow_label_fill = "#f2e4c1") +
  scale_fill_carto_d(palette = "Antique") +
  labs(title = "R Markdown Workflow") +
  theme(
    legend.position = "none",
    plot.background = element_rect(
      colour = "#f2e4c1",
      fill = "#f2e4c1"
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = "bold",
      colour = "#585c45"
    )
  )

我可以理解你的要求,将在代码部分以外的文本进行翻译。

英文:

With the current dev version of {ggflowchart} (version >= 1.0.0.9006), it's possible to directly add arrow labels. Install dev version

remotes::install_github(&quot;nrennie/ggflowchart&quot;)

You can add arrow labels by adding a label column to the data argument. The colour of the labels can be edited using arrow_label_fill:

library(tidyverse)
library(ggflowchart)
library(rcartocolor)
workflow &lt;- tibble::tibble(from = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;),
                           to = c(&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;),
                           label = c(&quot;knitr&quot;, NA, NA, NA, NA))
node_data &lt;- tibble::tibble(name = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;),
                            label = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;))

ggflowchart(workflow,node_data, horizontal = TRUE, arrow_label_fill = &quot;#f2e4c1&quot;) +
  scale_fill_carto_d(palette = &quot;Antique&quot;) +
  labs(title = &quot;R Markdown Workflow&quot;) +
  theme(
    legend.position = &quot;none&quot;,
    plot.background = element_rect(
      colour = &quot;#f2e4c1&quot;,
      fill = &quot;#f2e4c1&quot;
    ),
    plot.title = element_text(
      size = 20,
      hjust = 0.5,
      face = &quot;bold&quot;,
      colour = &quot;#585c45&quot;
    )
  )

我可以理解你的要求,将在代码部分以外的文本进行翻译。

huangapple
  • 本文由 发表于 2023年5月14日 14:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定