Hi,
I'm working on the below diagram and am struggling to get the edge from the moderator node to connect with the edge between the X and Y nodes. I'd also like to be able to label the edge from the moderator to the other edge similarly to how I've labelled the other edges.
I don't know much about LaTex, so the code is a mish-mash from various sources (here, here, and here). It might seem unnecessary to include the glue and dataframe step since the values are hard coded in this example, but in my work the dataframe is set up to update if the dataset or analyses change.
Here's an attachment with the diagram diagram.pdf (77.5 KB)
Also, is there a way to display the diagram here so people don't need to download it or run the rmarkdown file just to see it?
I greatly appreciate any help.
---
title: "diagram"
header-includes:
- \usepackage{tikz}
- \usepackage{pgfplots}
- \pgfplotsset{compat=1.17}
- \tikzset{mynode/.style={draw,text width=1in,align=center} }
- \usetikzlibrary{positioning}
- \usetikzlibrary{calc}
output: pdf_document
---
```{r, echo=FALSE, results='asis'}
library(glue)
diagram1 <- function(data) {
glue::glue_data(med_data,
"
\\begin{figure}
\\begin{center}
\\begin{tikzpicture}
\\node[mynode, minimum width=3.5cm, minimum height=1cm] (m){Mediator};
\\node[mynode,below left=of m, minimum width=3.5cm, minimum height=1.25cm](x) {X Variable};
\\node[mynode,below right=of m, minimum width=3.5cm, minimum height=1.25cm](y) {Y Variable};
\\node[mynode,above left=of m, minimum width=3.5cm, minimum height=1.25cm](z) {Moderator};
\\draw[-latex] (x.north) -- node[auto,font=\\footnotesize, align=left] {<<x_m>>} (m.west);
\\draw[-latex] (m.east) -- node[auto,font=\\footnotesize, align=left] {<<m_y>>} (y.north);
\\draw[-latex] (x.east) --
node[below=7mm,font=\\footnotesize,align=left] {<<x_y>>} (y.west);
\\draw[-latex, shorten >=2pt] (z) -- ($ (x) !.5! (m) $);
\\end{tikzpicture}
\\caption{Diagram}
\\end{center}
\\end{figure}
",
.open = "<<", .close = ">>"
)
}
med_data <-
data.frame(
x_m = "$b = 1.2$, $p = 0.001$",
x_y = "$b = -0.47$, $p = 0.098$",
m_y = "$b = 0.8$, p < .001"
)
tikz_diagram_out <- diagram1(med_data) #med_data
# requires chunk header to be set to results = 'asis'
cat("\n", tikz_diagram_out, "\n")
```