带有时间线的树形图

带有时间线的树形图

我尝试在 LaTex 中生成以下图表:

在此处输入图片描述

使用以下代码,我可以实现类似但不相同的功能:


\begin{tikzpicture}[grow=right, sloped]
\node[bag] {X}
    child {
        node[bag] {negative}
            child {
                node[end, label=right:
                    {$\beta$}] {}
                edge from parent
                node[above] {$1-\pi$}
            }
            child {
                node[end, label=right:
                    {1}] {}
                edge from parent
                node[above] {$\pi$}
            }
            edge from parent
            node[above] {$1-p$}
    }
    child {
        node[bag] {positive}
        child {
                node[end, label=right:
                    {$R$}] {}
                edge from parent
                node[above] {$1$}
            }

    edge from parent
            node[above] {$p$}};
\end{tikzpicture}

输出为

在此处输入图片描述

有没有办法添加时间线,并使线条更好地对齐?

答案1

我要求您针对新问题提出新问题。由于您宁愿编辑此问题,因此我认为已编辑的问题属于新问题,值得给出新答案。

解决新问题的可能性之一是将新级别添加到三级,删除此级别的边并减少l sep到此级别。

在此处输入图片描述

梅威瑟:

\documentclass[border=3.141592]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style = {% Edge Label Style       % <---
    pos=0.5, sloped, node font=\footnotesize,
    inner sep=2pt, anchor=#1},
        }
    \begin{forest}
for tree = {
% nodes
    font=\sffamily,
% tree
    grow'=0,    % <---
    parent anchor=east,
    child  anchor=west,
% nodes distances
    s sep=8mm,
 where level=2{l sep=3mm}{l sep=12mm},          % <---
% edges
 where level=3{no edge}
              {edge={semithick,line cap=round}},% <---
% edge labels
EL/.style = {% Edge Label
    if n=1{edge label={node[ELS=south]{$#1$}}} 
          {edge label={node[ELS=north]{$#1$}}} 
            },
            }% end for tree
%
[X, name=t0
    [Positive, name=t1, EL=p
        [$R$, name=t2, EL=1
            [good, name=state] % <---
        ]
    ]
    [Negative, EL=1-p
        [$1$, EL=\pi
            [bad]              % <---
        ]
        [$\beta$, EL=1-\pi
            [disaster]         % <---
        ]
    ]
]
\path   (state) node[above=3mm,font=\sffamily] {state} % <---
        (t0 |- state) node[above=3mm] {$t=0$}
        (t1 |- state) node[above=3mm] {$t=1$}
        (t2 |- state) node[above=3mm] {$t=2$};
    \end{forest}
\end{document}

编辑: 根据您的新需求,必须在代码中添加按级别对齐。这可以通过选项˙tier/.option = level和更改节点锚点来实现:

\documentclass[border=3.141592]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style = {% Edge Label Style       % <---
    pos=0.5, sloped, node font=\footnotesize,
    inner sep=2pt, anchor=#1},
        }
    \begin{forest}
for tree = {
% nodes
    font=\sffamily,
% tree
    grow'=0,    % <---
    anchor=west,
    tier/.option = level,
% nodes distances
    s sep=8mm,
 where level=2{l sep=3mm}{l sep=12mm},
% edge 
 where level=3{no edge}
              {edge={semithick,line cap=round}},
% edge labels
EL/.style = {% Edge Label
    if n=1{edge label={node[ELS=south]{$#1$}}} % <---
          {edge label={node[ELS=north]{$#1$}}} % <---
            },
            }% end for tree
%
[X, name=t0
    [Positive, name=t1, EL=p
        [$R$, name=t2, EL=1
            [good, name=state]
        ]
    ]
    [Negative, EL=1-p
        [$1$, EL=\pi
            [bad]
        ]
        [someverylongword, EL=1-\pi
            [disaster]
        ]
    ]
]
\path   (state |- t2) node[above=3mm] {state}
        (t0 |- t2) node[above=3mm] {$t=0$}
        (t1 |- t2) node[above=3mm] {$t=1$}
        (t2) node[above=3mm] {$t=2$};
    \end{forest}
\end{document}

在此处输入图片描述

这就是你所追求的吗?

答案2

编辑: 现在添加时间线:

像这样:

在此处输入图片描述

forest简单:

\documentclass[border=3.141592]{standalone}
\usepackage{forest}

\begin{document}
    \begin{forest}
for tree = {
% nodes
    font=\sffamily,
% tree
    grow=0,
    parent anchor=east,
    child  anchor=west,
    s sep=8mm,
    l sep=12mm,
% edge
    edge={semithick,line cap=round},
% edge labels
EL/.style args={#1/#2}{
    edge label={node [pos=0.5, sloped, inner sep=2pt,
                      font=\footnotesize\sffamily,
                      anchor=#1] {$#2$}}
           }
            }% end for tree
[X, name=t0
    [Negative, EL=south/1-p
        [$\beta$, EL=north/1-\pi] 
        [$1$, EL=south/\pi]
    ]
    [Positive, name=t1, EL=south/p
        [$R$, name=t2, EL=south/1]
    ]
]
\path   (t0 |- t2) node[above=3mm] {$t=0$}
        (t1 |- t2) node[above=3mm] {$t=1$}
        (t2) node[above=3mm] {$t=2$};
    \end{forest}
\end{document}

上述的基本用法forest可以通过边缘标签的自动定位来改进:

\documentclass[border=3.141592]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style = {% Edge Label Style       % <---
    pos=0.5, sloped, node font=\footnotesize,
    inner sep=2pt, anchor=#1},
        }
    \begin{forest}
for tree = {
% nodes
    font=\sffamily,
% tree
    grow'=0,    % <---
    parent anchor=east,
    child  anchor=west,
    s sep=8mm,
    l sep=12mm,
% edge
    edge={semithick,line cap=round},
% edge labels
EL/.style = {% Edge Label
    if n=1{edge label={node[ELS=south]{$#1$}}} % <---
          {edge label={node[ELS=north]{$#1$}}} % <---
            }
            }% end for tree
%
[X, name=t0
    [Positive, name=t1, EL=p
        [$R$, name=t2, EL=1]
    ]
    [Negative, EL=1-p
        [$1$, EL=\pi]
        [$\beta$, EL=1-\pi]
    ]
]
\path   (t0 |- t2) node[above=3mm] {$t=0$}
        (t1 |- t2) node[above=3mm] {$t=1$}
        (t2) node[above=3mm] {$t=2$};
    \end{forest}
\end{document}

上述 MWE 的结果与之前相同。

相关内容