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

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

How can I draw a flow chart in ggplot2?

问题

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

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

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

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

  1. library(tidyverse)
  2. library(ggflowchart)
  3. library(rcartocolor)
  4. workflow <- tibble::tibble(from = c("RMD","MD","MD","MD","LaTeX"), to = c("MD","LaTeX","HTML","WORD","PDF"))
  5. ggflowchart(workflow)
  6. node_data <- tibble::tibble(name = c("RMD","MD","LaTeX","HTML","WORD","PDF"), label = c("RMD","MD","LaTeX","HTML","WORD","PDF"))
  7. ggflowchart(workflow,node_data)
  8. quarto <- ggflowchart(workflow,
  9. node_data,
  10. colour = "#585c45",
  11. text_colour = "#585c45",
  12. arrow_colour = "#585c45"
  13. )+
  14. scale_fill_carto_d(palette = "Antique") +
  15. labs(title = "R Markdown工作流程") +
  16. theme(
  17. legend.position = "none",
  18. plot.background = element_rect(
  19. colour = "#f2e4c1",
  20. fill = "#f2e4c1"
  21. ),
  22. plot.title = element_text(
  23. size = 20,
  24. hjust = 0.5,
  25. face = "bold",
  26. colour = "#585c45"
  27. )
  28. )

我希望图表从左到右流动(即,左侧为“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:

  1. library(tidyverse)
  2. library(ggflowchart)
  3. library(rcartocolor)
  4. 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;))
  5. ggflowchart(workflow)
  6. 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;))
  7. ggflowchart(workflow,node_data)
  8. quarto &lt;- ggflowchart(workflow,
  9. node_data,
  10. colour = &quot;#585c45&quot;,
  11. text_colour = &quot;#585c45&quot;,
  12. arrow_colour = &quot;#585c45&quot;
  13. )+
  14. scale_fill_carto_d(palette = &quot;Antique&quot;) +
  15. labs(title = &quot;R Markdown Workflow&quot;) +
  16. theme(
  17. legend.position = &quot;none&quot;,
  18. plot.background = element_rect(
  19. colour = &quot;#f2e4c1&quot;,
  20. fill = &quot;#f2e4c1&quot;
  21. ),
  22. plot.title = element_text(
  23. size = 20,
  24. hjust = 0.5,
  25. face = &quot;bold&quot;,
  26. colour = &quot;#585c45&quot;
  27. )
  28. )

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:

  1. library(DiagrammeR)
  2. flow_chart <- grViz("digraph {
  3. # horizontal alignment
  4. graph [rankdir = LR]
  5. # Nodes with labels
  6. node [shape = box]
  7. RMD [label = 'RMD']
  8. MD [label = 'MD']
  9. LaTeX [label = 'LaTeX']
  10. HTML [label = 'HTML']
  11. WORD [label = 'WORD']
  12. PDF [label = 'PDF']
  13. # Edges with labels
  14. RMD -> MD [label = 'knit']
  15. MD -> LaTeX [label = 'pandoc']
  16. MD -> HTML [label = 'pandoc']
  17. MD -> WORD [label = 'pandoc']
  18. LaTeX -> PDF [label = 'pdflatex']
  19. }")
  20. flow_chart

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

英文:

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

  1. library(DiagrammeR)
  2. flow_chart &lt;- grViz(&quot;digraph {
  3. # horizontal alignment
  4. graph [rankdir = LR]
  5. # Nodes with labels
  6. node [shape = box]
  7. RMD [label = &#39;RMD&#39;]
  8. MD [label = &#39;MD&#39;]
  9. LaTeX [label = &#39;LaTeX&#39;]
  10. HTML [label = &#39;HTML&#39;]
  11. WORD [label = &#39;WORD&#39;]
  12. PDF [label = &#39;PDF&#39;]
  13. # Edges with labels
  14. RMD -&gt; MD [label = &#39;knit&#39;]
  15. MD -&gt; LaTeX [label = &#39;pandoc&#39;]
  16. MD -&gt; HTML [label = &#39;pandoc&#39;]
  17. MD -&gt; WORD [label = &#39;pandoc&#39;]
  18. LaTeX -&gt; PDF [label = &#39;pdflatex&#39;]
  19. }&quot;)
  20. flow_chart

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

答案2

得分: 2

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

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

  1. library(ggflowchart)
  2. library(rcartocolor)
  3. library(tidyverse)
  4. workflow <- tibble::tibble(from = c("RMD","MD","MD","MD","LaTeX"), to = c("MD","LaTeX","HTML","WORD","PDF"))
  5. node_data <- tibble::tibble(name = c("RMD","MD","LaTeX","HTML","WORD","PDF"), label = c("RMD","MD","LaTeX","HTML","WORD","PDF"))
  6. quarto <- ggflowchart(workflow,
  7. node_data,
  8. colour = "#585c45",
  9. text_colour = "#585c45",
  10. arrow_colour = "#585c45",
  11. horizontal = TRUE
  12. )+
  13. scale_fill_carto_d(palette = "Antique") +
  14. labs(title = "R Markdown Workflow") +
  15. theme(
  16. legend.position = "none",
  17. plot.background = element_rect(
  18. colour = "#f2e4c1",
  19. fill = "#f2e4c1"
  20. ),
  21. plot.title = element_text(
  22. size = 20,
  23. hjust = 0.5,
  24. face = "bold",
  25. colour = "#585c45"
  26. )
  27. ) +
  28. annotate(geom = "text", x = 0.1, y = 2.5, label = "labels")
  29. 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.

  1. library(ggflowchart)
  2. library(rcartocolor)
  3. library(tidyverse)
  4. 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;))
  5. 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;))
  6. quarto &lt;- ggflowchart(workflow,
  7. node_data,
  8. colour = &quot;#585c45&quot;,
  9. text_colour = &quot;#585c45&quot;,
  10. arrow_colour = &quot;#585c45&quot;,
  11. horizontal = T
  12. )+
  13. scale_fill_carto_d(palette = &quot;Antique&quot;) +
  14. labs(title = &quot;R Markdown Workflow&quot;) +
  15. theme(
  16. legend.position = &quot;none&quot;,
  17. plot.background = element_rect(
  18. colour = &quot;#f2e4c1&quot;,
  19. fill = &quot;#f2e4c1&quot;
  20. ),
  21. plot.title = element_text(
  22. size = 20,
  23. hjust = 0.5,
  24. face = &quot;bold&quot;,
  25. colour = &quot;#585c45&quot;
  26. )
  27. ) +
  28. annotate(geom = &quot;text&quot;, x = 0.1, y = 2.5, label = &quot;labels&quot;)
  29. 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:

  1. 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:

  1. library(tidyverse)
  2. library(ggflowchart)
  3. library(rcartocolor)
  4. workflow <- tibble::tibble(from = c("RMD","MD","MD","MD","LaTeX"),
  5. to = c("MD","LaTeX","HTML","WORD","PDF"),
  6. label = c("knitr", NA, NA, NA, NA))
  7. node_data <- tibble::tibble(name = c("RMD","MD","LaTeX","HTML","WORD","PDF"),
  8. label = c("RMD","MD","LaTeX","HTML","WORD","PDF"))
  9. ggflowchart(workflow,node_data, horizontal = TRUE, arrow_label_fill = "#f2e4c1") +
  10. scale_fill_carto_d(palette = "Antique") +
  11. labs(title = "R Markdown Workflow") +
  12. theme(
  13. legend.position = "none",
  14. plot.background = element_rect(
  15. colour = "#f2e4c1",
  16. fill = "#f2e4c1"
  17. ),
  18. plot.title = element_text(
  19. size = 20,
  20. hjust = 0.5,
  21. face = "bold",
  22. colour = "#585c45"
  23. )
  24. )

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

英文:

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

  1. 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:

  1. library(tidyverse)
  2. library(ggflowchart)
  3. library(rcartocolor)
  4. workflow &lt;- tibble::tibble(from = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;),
  5. to = c(&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;),
  6. label = c(&quot;knitr&quot;, NA, NA, NA, NA))
  7. 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;),
  8. label = c(&quot;RMD&quot;,&quot;MD&quot;,&quot;LaTeX&quot;,&quot;HTML&quot;,&quot;WORD&quot;,&quot;PDF&quot;))
  9. ggflowchart(workflow,node_data, horizontal = TRUE, arrow_label_fill = &quot;#f2e4c1&quot;) +
  10. scale_fill_carto_d(palette = &quot;Antique&quot;) +
  11. labs(title = &quot;R Markdown Workflow&quot;) +
  12. theme(
  13. legend.position = &quot;none&quot;,
  14. plot.background = element_rect(
  15. colour = &quot;#f2e4c1&quot;,
  16. fill = &quot;#f2e4c1&quot;
  17. ),
  18. plot.title = element_text(
  19. size = 20,
  20. hjust = 0.5,
  21. face = &quot;bold&quot;,
  22. colour = &quot;#585c45&quot;
  23. )
  24. )

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

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:

确定