英文:
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 <- 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 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"
)
)
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 <- 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
答案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 <- 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 = T
)+
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
<!-- -->
<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("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"
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论