6

I'm trying to create a Horizontal Probability Tree with Level Labels using tikz. (Not tikz-qtree). Right now, I have a horizontal tree. The labels are NOT aligned to the top of the diagram. I would like to have all the labels aligned without having to manually tweak positions.

Here is the MWE:

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]


\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]

\tikzstyle{ans} = [color=red]


\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
    \node (Root) [] {Origin}
        child [] {
        node {Door A}
        child { node {A} 
                child {node[end] {$0$} }
                edge from parent
                node[left] {$0$}
        }
        child [black] { node {B} 
                child {node[ans,end] {$\frac{1}{6}$} }
                edge from parent
                node[ans,left] {$\frac{1}{2}$}
        }
        child [black] { node {C} 
                child {node[ans,end] {$\frac{1}{6}$} }
            edge from parent
            node[ans,left] {$\frac{1}{2}$}
        }
        edge from parent
        node[above] {$\frac{1}{3}$}
    }
    child {
        node {Door B}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B} 
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {C} 
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    }
    child {
        node {Door C}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B}
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        child { node {C}
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    };
   % How I'm applying labels to each level. 
  % Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={above}]
 \path (Root    -| Root)  ++(0,15mm) node {Test};
 \path (Root-1   -| Root-1)  ++(0,15mm) node {Contestant Chooses};
 \path (Root-1-1  -| Root-3-3-1)  ++(10mm,13mm) node {Host Reveals Door};
 \path (Root-1-1-1 -| Root-3-3) ++(0,15mm) node {Probability};
\end{scope}


\end{tikzpicture}

I get this

enter image description here

and want this enter image description here

Ignasi
  • 136,588
coatless
  • 381
  • What do you mean by "aligned to the top of the diagram"? – Herr K. Jun 19 '14 at 20:20
  • http://yozh.org/wp/wp-content/uploads/2010/10/decision_tree.jpg

    Notice the labels for each level are aligned to the same height at the top of the diagram.

    – coatless Jun 19 '14 at 20:27
  • Try this: \begin{scope}[every node/.style={anchor=base}] \path (Root-1-1-1 -| Root) ++(0,15mm) node(a) {Test}; \path (Root-1-1-1 -| Root-1) ++(0,15mm) node {Contestant Chooses}; \path (Root-1-1-1 -| Root-3-3-1) ++(10mm,15mm) node {Host Reveals Door}; \path (Root-1-1-1 -| Root-3-3) ++(0,15mm) node(b) {Probability}; \end{scope} – Herr K. Jun 19 '14 at 21:08
  • 1
    Please always post a complete small document. Much more useful than mere fragments. Have you considered forest? – cfr Jun 20 '14 at 03:01
  • How could we break the node to write down the probability like on the second graph? Could we have instead a small table of 2 cells and 1 column? – Y_gr Mar 27 '19 at 22:29

1 Answers1

9

You can use intersections coordinate system to align all level labels. First place the label for the top most leaf where you want:

\node[above= 1cm of Root-1-1-1] (labels-level) {Host Reveals Door};

and later, use it as an horizontal reference for all other labels

\node[at =(labels-level-|Root-1-1)] {Probability};
\node[at =(labels-level-|Root-1)] {Contestant Chooses};
\node[at =(labels-level-|Root)] {Test};

I've used positioning library to fix first label position. The complete code is

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]


\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]

\tikzstyle{ans} = [color=red]


\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
    \node (Root) [] {Origin}
        child [] {
        node {Door A}
        child { node {A} 
                child {node[end] {$0$} }
                edge from parent
                node[left] {$0$}
        }
        child [black] { node {B} 
                child {node[ans,end] {$\frac{1}{6}$} }
                edge from parent
                node[ans,left] {$\frac{1}{2}$}
        }
        child [black] { node {C} 
                child {node[ans,end] {$\frac{1}{6}$} }
            edge from parent
            node[ans,left] {$\frac{1}{2}$}
        }
        edge from parent
        node[above] {$\frac{1}{3}$}
    }
    child {
        node {Door B}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B} 
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {C} 
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    }
    child {
        node {Door C}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B}
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        child { node {C}
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    };
   % How I'm applying labels to each level. 
  % Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={text width=2cm, align=center, anchor=center, font=\bfseries,}]
 \node[above= 1cm of Root-1-1-1] (labels-level) {Host Reveals Door};
 \node[at =(labels-level-|Root-1-1)] {Probability};
 \node[at =(labels-level-|Root-1)] {Contestant Chooses};
 \node[at =(labels-level-|Root)] {Test};

\end{scope}
\end{tikzpicture}
\end{document}

and the result (change labels aspect as you want)

enter image description here

Ignasi
  • 136,588