-1

I want to reproduce the tree diagram shown below. My major problem is how to put text (Yes/No/Exposure...) next to the lines.

, what i

  • Hardly without any effort :-). For putting YES/NO labels at nodes you can use separate nodes, something like this ` ... child{ node (EX) [<style>] {Experimental study}} ...` and after tree \node[above right=of EX] {Yes}. More demanding is construction of tree, saying i rash, probably the easiest way will be hybrid between tree and standard TikZ picture ... And welcome to SE! – Zarko Jan 28 '16 at 2:45
  • Welcome! Sine the tree isn't a problem, just post the code for that and somebody will be happy to show you how to add the labels. If you are not being altogether honest in your question - either with us or with yourself - forest could do this relatively easily. I would have no idea how to do it in qtree or using tikz-trees without a lot of messing around. tikz-qtree could also do it fairly straightforwardly, although it is not as powerful as forest and I could be mistaken about its capabilities. – cfr Jan 28 '16 at 4:01
2

Details are left as an exercise to the reader.... Focus is on the topic of the question i.e. adding labels.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-2.5mm) -| (.child anchor)\forestoption{edge label};
    },
    parent anchor=south,
    child anchor=north,
    draw,
    l sep+=2.5mm,
    font=\sffamily,
    edge={font=\scriptsize\sffamily}% style for labels can go here
  }
  [Text
    [Query?
    [Option A, edge label={node[pos=.75, left]{Yes}}, l sep+=5mm, for children={circle, inner sep=1pt, edge path={\noexpand\path [\forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};} }
        [a]
        [b]
        [c, edge label={node[midway, right, align=left]{Something\\Something else\\Yet another thing}}]
      ]
      [Option B, edge label={node[pos=.75, right]{No}}]
    ]
  ]
\end{forest}
\end{document}

edge label adds the labels in the usual way nodes are added as part of path specifications.

add labels

EDIT

Here's the code you posted in another answer, tidied up a bit and with multiline nodes enabled. I've defined a couple of styles to save typing and to make adding the Yes/No labels easier.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
  my edge label/.style={
    if n=1{
      edge label={node[pos=.75, left]{Yes}}
    }{
      edge label={node[pos=.75, right]{No}}
    }
  },
  my circle/.style={circle, inner sep=1pt, minimum width=60pt, anchor=center},
  light grey/.style={fill=gray!20},
  dark grey/.style={fill=gray!60},
}
\begin{forest}
  for tree={
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-2.5mm) -| (.child anchor)\forestoption{edge label};
    },
    parent anchor=south,
    child anchor=north,
    draw,
    l sep+=2.5mm,
    font=\sffamily,
    edge={font=\scriptsize\sffamily},
    align=center,
    light grey
  }
  [Did investigator\\assign exposures?, for children={dark grey}
    [Observational study, my edge label
      [Ramdom allocation?, for children={my circle}
        [Ramsomised\\controlled\\trial, my edge label]
        [Non-\\ramsomised\\controlled\\trial, my edge label]
      ]
    ]
    [Observational study, my edge label
      [Comparison group?, for children={dark grey}
        [Analytical\\study, my edge label
          [Direction?, l sep+=5mm, s sep+=25pt, dark grey,
            for children={%
              my circle,
              edge path={%
                \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};
              },
            }
            [Cohort\\study, edge label={node[midway, left, align=left]{Exposure - Outcome}}]
            [Case-\\control\\study, edge label={node[pos=.9, left, align=left]{Exposure - Outcome}}]
            [Cross-\\sectiuonal\\study, edge label={node[midway, right, align=left]{Exposure and\\\hspace*{2.5em}outcome at\\\hspace*{5em}the same time}}]
          ]
        ]
        [Descriptive\\study, my edge label
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

Here's the result:

modified code with multiline nodes

  • you are good soul and big propagator of use of the forest package :-) – Zarko Jan 28 '16 at 4:33
  • @cfr I have a problem: I'm trying to use multiples lines in the nodes. In your example, if I add \\ after any node, e.g. Query?\\Query? it does not show two lines. it shows: Query?Query? all together in the same line. Why? Help please – J. Gatica Jan 28 '16 at 15:36
  • 1
    @J.Gatica Just add align=left or align=center or align=right inisde the argument of for tree and then forest will set up all nodes to allow multiple lines broken by \\ . Alternatively, you can say text width=<some width> and have the lines broken automatically, but I find manual breaks tend to produce better results. – cfr Jan 28 '16 at 20:23
  • 1
    @J.Gatica Please see edit. I took the code you posted, added the align and set up some styles to make it easier to format things consistently and add the shadings etc. Hope this helps ;). – cfr Jan 28 '16 at 22:06
  • Wow! that's perfect! yes, It helps a lot. :) @cfr you are the man! Thanks again! – J. Gatica Jan 28 '16 at 22:21
2

Well here's the diagram almost finished. Thank you @cfr, your example was perfect.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
edge path={
  \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- +(0,-2.5mm) -| (.child anchor)\forestoption{edge label};
},
parent anchor=south,
child anchor=north,
draw,
l sep+=2.5mm,
font=\sffamily,
edge={font=\scriptsize\sffamily}% style for labels can go here
  }
[Did investigator assign exposures?
    [Observational study, edge label={node[pos=.75, left]{Yes}}
        [Ramdom allocation?
            [Ramsomised controlled trial, edge label={node[pos=.75, left]{Yes}}]
            [Non-ramsomised controlled trial, edge label={node[pos=.75, right]{No}}]]]
    [Observational study, edge label={node[pos=.75, right]{No}}
        [Comparison group?
            [Analytical study, edge label={node[pos=.75, left]{Yes}}
                [Direction?, l sep+=5mm, for children={circle, inner sep=1pt, edge path={\noexpand\path [\forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};} }
                    [Cohort study, edge label={node[midway, left, align=left]{Exposure - Outcome}}]
                    [Case-control study, edge label={node[midway, left, align=left]{Exposure - Outcome}}]
                    [Cross-sectiuonal study, edge label={node[midway, right, align=left]{Exposure and\\outcome at\\the same time}}]
  ]]
            [Descriptive study, edge label={node[pos=.75, right]{No}}
            ]
        ]
    ]
]
\end{forest}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.