[英]Drawing simple mediation diagram in R
雖然@baptiste解決方案也可能有效,但我一直在尋找一種可發布的格式。
library(diagram)
中的函數plotmat
是讓我最接近我的示例的函數:
對於可重現的示例,請使用:
library(diagram)
data <- c(0, "'.47*'", 0,
0, 0, 0,
"'.36*'", "'.33* (.16)'", 0)
M<- matrix (nrow=3, ncol=3, byrow = TRUE, data=data)
plot<- plotmat (M, pos=c(1,2),
name= c( "Math self-efficacy","Math ability", "Interest in the math major"),
box.type = "rect", box.size = 0.12, box.prop=0.5, curve=0)
您可以使用“心理”包來測試您的節制/調解,它也會為您提供一個情節。
mediate(Output ~ Independent1 + (Mediator), data =mydata)
我對 ggraph 一無所知,但這可能是一條可能的路線。 以下是 5 分鍾的谷歌搜索使我得到的結果:
library(ggraph)
require(igraph)
test <- data.frame(from = c(1,2,3), to=c(2,3,1), coef = letters[1:3])
simple <- graph_from_data_frame(test)
V(simple)$name <- LETTERS[1:3]
ggraph(simple, layout = 'auto') +
geom_edge_link(aes(label = coef),
angle_calc = 'along',
label_dodge = unit(2.5, 'mm'),
arrow = arrow(length = unit(4, 'mm')),
end_cap = circle(3, 'mm')) +
geom_node_label(aes(label = name),size = 5) +
theme_graph()
我不知道如何強制執行幾何圖形(如果可能),但好消息是布局可以自動泛化。
這是使用DiagrammeR
構建的中介圖的兩個版本和使用 TikZ 構建的第三個版本。 每個人都有優點和缺點:
DiagrammeR + Graphviz:
抖音
DiagrammeR 本機語法(僅提供完整性)
R
用戶編輯我已經使用函數med_diagram
對第一個和第二個示例進行了編碼,該函數使用glue
包來組裝相關的 graphviz 或 TikZ 代碼。 該函數需要一個 data.frame,它需要一行,其中包含相關標簽和系數的列。 對於 DiagrammeR 特定的函數,還有一些參數可以調整設計的各種元素。
med_data <-
data.frame(
lab_x = "Math\\nAbility",
lab_m = "Math\\nself-efficacy",
lab_y = "Interest in the\\nmath major",
coef_xm = ".47*",
coef_my = ".36*",
coef_xy = "0.33* (.16)"
)
med_diagram <- function(data, height = .75, width = 2, graph_label = NA, node_text_size = 12, edge_text_size = 12, color = "black", ranksep = .2, minlen = 3){
require(glue)
require(DiagrammeR)
data$height <- height # node height
data$width <- width # node width
data$color <- color # node + edge border color
data$ranksep <- ranksep # separation btwn mediator row and x->y row
data$minlen <- minlen # minimum edge length
data$node_text_size <- node_text_size
data$edge_text_size <- edge_text_size
data$graph_label <- ifelse(is.na(graph_label), "", paste0("label = '", graph_label, "'"))
diagram_out <- glue::glue_data(data,
"digraph flowchart {
fontname = Helvetica
<<graph_label>>
graph [ranksep = <<ranksep>>]
# node definitions with substituted label text
node [fontname = Helvetica, shape = rectangle, fixedsize = TRUE, width = <<width>>, height = <<height>>, fontsize = <<node_text_size>>, color = <<color>>]
mm [label = '<<lab_m>>']
xx [label = '<<lab_x>>']
yy [label = '<<lab_y>>']
# edge definitions with the node IDs
edge [minlen = <<minlen>>, fontname = Helvetica, fontsize = <<edge_text_size>>, color = <<color>>]
mm -> yy [label = '<<coef_my>>'];
xx -> mm [label = '<<coef_xm>>'];
xx -> yy [label = '<<coef_xy>>'];
{ rank = same; mm }
{ rank = same; xx; yy }
}
", .open = "<<", .close = ">>")
DiagrammeR::grViz(diagram_out)
}
med_diagram(med_data)
TikZ 和 PGF 需要在序言中作為 LaTeX 包加載。 下面的示例代碼包括這些包和一些附加命令,例如,為圖中使用的“mynode”設置全局規范。 我收到了一個警告,在我的序言中包含\\pgfplotsset{compat=1.17}
,但這對其他人來說可能沒有必要。 請注意,此代碼基於此處提供的示例: https : //tex.stackexchange.com/a/225940/34597
---
title: "Sample Rmd"
author: "Your name here"
output: pdf_document
header-includes:
- \usepackage{tikz}
- \usepackage{pgfplots}
- \pgfplotsset{compat=1.17}
- \tikzset{mynode/.style={draw,text width=1in,align=center} }
- \usetikzlibrary{positioning}
---
```{r, load_packages, include = FALSE}
library(glue)
```
```{r, load_function}
med_diagram_tikz <- function(data) {
glue::glue_data(med_data,
"
\\begin{figure}
\\begin{center}
\\begin{tikzpicture}[font=\\sffamily]
\\node[mynode] (m){<<lab_m>>};
\\node[mynode,below left=of m](x) {<<lab_x>>};
\\node[mynode,below right=of m](y) {<<lab_y>>};
\\draw[-latex] (x.north) -- node[auto] {<<coef_xm>>} (m.west);
\\draw[-latex] (m.east) -- node[auto] {<<coef_my>>} (y.north);
\\draw[-latex] (x.east) --
node[below=2mm, align=center] {<<coef_xy>>} (y.west);
\\end{tikzpicture}
\\end{center}
\\end{figure}
",
.open = "<<", .close = ">>"
)
}
```
```{r create_diagram, echo = FALSE, results = 'asis'}
med_data <-
data.frame(
lab_x = "Math\\\\Ability",
lab_m = "Math\\\\self-efficacy",
lab_y = "Interest in the\\\\math major",
coef_xm = ".47*",
coef_my = ".36*",
coef_xy = "0.33* (.16)"
)
tikz_diagram_out <- med_diagram_tikz(med_data)
# requires chunk header to be set to results = 'asis'
cat("\n", tikz_diagram_out, "\n")
```
中介圖的第三個版本使用R
用戶更容易理解的語法,但我不喜歡邊緣標簽文本的奇怪放置方式(因此是上面的兩個替代版本)。
library(DiagrammeR)
# Create a node data frame (ndf)
ndf <- create_node_df(
n = 3,
label = c( "Math\nself-efficacy","Math\nability", "Interest in\nthe math major"),
shape = rep("rectangle", 3),
style = "empty",
fontsize = 6,
fixedsize = TRUE,
height = .5,
width = .75,
color = "gray80",
x = c(1, 2, 3),
y = c(1, 2, 1)
)
# Create an edge data frame (edf)
edf <- create_edge_df(
from = c(1, 1, 2),
to = c(2, 3, 3),
label = c(".47*", ".33* (.16)", ".36*"),
fontsize = 6,
minlen = 1,
color = "gray80",
)
# Create a graph with the ndf and edf
graph <- create_graph(
nodes_df = ndf,
edges_df = edf
)
graph %>%
render_graph()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.