Draw edge from node to another edge in Tikz diagram

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")


```


I don't know much about Tikz device but in case you don't know, there are others tools to create diagrams and they don't us LaTeX.

In case it helps

Thanks. I actually started off using diagrammeR but got much closer to what I wanted with Tikz. One challenge with diagrammeR is that it doesn't default to placing labels in the "right" place, and there isn't really any functionality to control their placement (at least not that I found).

Here's a solution in case it would be helpful for anyone. I asked a librarian, and this is what she came up with. Here's the diagram: diagram.pdf (57.2 KB)

---
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}
   - \tikzstyle{arrow} = [->,>=stealth]
output: pdf_document
---

```{r, echo=FALSE, results='asis'}

library(glue)

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",
    mod = "$b = x, p < x$"
  )

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[arrow] (m.east) -- node[right=2mm] {<<m_y>>}(y.north);
        \\draw[arrow] (x.north) -- node[left=2mm] {<<x_m>>}(m.west);
        \\draw[arrow] (x.east) -- node[anchor=north] {<<x_y>>}(y.west);
        \\draw[arrow] (z.south) -- node[left=2mm] {<<mod>>}($ (x.north) !.5! (m.west) $);
    \\end{tikzpicture}
    \\caption{Diagram}
  \\end{center}
  \\end{figure}
  ",
  .open = "<<", .close = ">>"
  )
}

tikz_diagram_out <- diagram1(med_data)

cat("\n", tikz_diagram_out, "\n")


```

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.