我有一个如下所示的图形/树:
我使用以下代码创建了该图形:
\begin{tikzpicture}[sibling distance=10em,
every node/.style = {shape=circle, draw, align=center}]]
\node [fill=black] {}
child { node (ja1) [fill=black] {}
child { [sibling distance=10em] node (ja2) [fill=black] {}
child { [sibling distance=5em] node [fill=black] {}
child { node (ja4l) [fill=black] {} edge from parent node [left, draw=none] {Ja} }
child { node (nein4l) [fill=black] {} edge from parent node [right, draw=none] {Nein} }
edge from parent node [left, draw=none] {Ja}} % Dritte Ebene
child { [sibling distance=5em] node [fill=black] {}
child { node (ja4r) [fill=black] {}edge from parent node [left, draw=none] {Ja} }
child { node (nein4r) [fill=black] {} edge from parent node [right, draw=none] {Nein} }
edge from parent node [right, draw=none] {Nein} } % Dritte Ebene
edge from parent node [left, draw=none] {Ja} } % Zweite Ebene
child { node (nein2) [fill=black] {} edge from parent node [right, draw=none] {Nein} }
edge from parent node [left, draw=none] {Ja} } % Erste Ebene
child { node (nein1) [fill=black] {} edge from parent node [right, draw=none] {Nein}}; % Erste Ebene
\node (nop) [draw=none, below = of nein1, shape=rectangle, xshift=5em] {Keine Provenance};
\node (pla) [draw=none, below = of nein2, shape = rectangle, xshift=5em] {Plausibilität};
\node (nvz) [draw=none, below = of nein4r, shape = rectangle, xshift=2.5em] {Nachvollziehbarkeit};
\node (rek) [draw=none, below = of ja4r, shape=rectangle, yshift=-2.5em] {Rekonstruierbarkeit};
\node (snv) [draw=none, below = of nein4l, shape=rectangle, yshift=-p5em] {Starke Nachvollziehbarkeit};
\node (srk) [draw=none, below = of ja4l, shape=rectangle, xshift=-2.5em] {Starke Rekonstruierbarkeit \\ Fehlernachvollziehbarkeit};
\draw (nein1) -- (nop);
\draw (nein2) -- (pla);
\draw (nein4r) -- (nvz);
\draw (ja4r) -- (rek);
\draw (nein4l) -- (snv);
\draw (ja4l) -- (srk);
\end{tikzpicture}
我的问题是我想向此决策树添加文本条件。由于该条件出现多次(例如,在树左侧的四片叶子中),因此我不想将条件的文本添加到节点,而是添加到图形的左侧。类似这样:
我怎样才能实现这个目标?
答案1
以下是仅使用 TikZ 的建议:
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
punkt/.style={shape=circle, fill=black},
nurtext/.style={shape=rectangle, draw=none, fill=none, align=center}
]
\node (root) [punkt] {}
[every child node/.append style = {punkt},
sibling distance=10em,
edge from parent/.append style = {nodes={inner xsep=.75em}},
level 4/.style={
sibling distance=5em,
edge from parent/.append style = {nodes={inner xsep=.5em}}}
]
child { node {}
child { node {}
child { node {}
child { node {}
child { node [nurtext] { Starke Rekonstruierbarkeit \\ Fehlernachvollziehbarkeit } }
child [missing]
edge from parent node [left] {Ja} }
child { node {}
child [level distance=2.5cm] { node [nurtext] { Starke Nachvollziehbarkeit } }
edge from parent node [right] {Nein} }
edge from parent node [left] {Ja}} % Dritte Ebene
child { node {}
child { node {}
child [level distance=2cm] { node [nurtext] { Rekonstruierbarkeit } }
edge from parent node [left] {Ja} }
child { node {}
child [missing]
child { node [nurtext] { Nachvollziehbarkeit } }
edge from parent node [right] {Nein} }
edge from parent node [right] {Nein} } % Dritte Ebene
edge from parent node [left] {Ja} } % Zweite Ebene
child { node {}
child [missing]
child { node [nurtext] {Plausibiltiät} }
edge from parent node [right] {Nein} }
edge from parent node [left] {Ja} } % Erste Ebene
child { node {}
child [missing]
child { node [nurtext] {Keine Provenance} }
edge from parent node [right] {Nein} }; % Erste Ebene
\draw
(root-1-1-1-1-1.west) coordinate (links)
(links|-root)--(root)--+(1,0)
(links|-root-1)--(root-1)--(root-2)--+(1,0)
(links|-root-1-1)--(root-1-2)--+(1,0)
(links|-root-1-1-1)--(root-1-1-1)--(root-1-1-2)--+(1,0)
(links|-root-1-1-1-1)--(root-1-1-1-1)--(root-1-1-1-2)--(root-1-1-2-1)--(root-1-1-2-2)--+(1,0)
;
\path [ nodes={right} ]
(links|-root)
--node{Bedingung 1}
(links|-root-1)
--node{Bedingung 2}
(links|-root-1-1)
--node{Bedingung 3}
(links|-root-1-1-1)
--node{Bedingung 4}
(links|-root-1-1-1-1)
;
\end{tikzpicture}
\end{document}
答案2
这使用基于forest
called justtrees
(版本 0.05) 的实验包。如果您感兴趣,请告诉我。
已编辑添加分隔线并改善间距和对齐。
基本上,它允许您以类似以下的方式指定树和注释。
[, left just=, right just=, name=n1
[, left just=Condition 1, right just=, name=n2
[, left just=Condition 2, right just=, name=n3
[, left just=Condition 3, right just=, name=n4
[, left just=Condition 4, right just=, name=n5
[Starke Rekonstruierbarkeit\\Fehlernachvollziehbarkeit]
]
[
[[[Starke Nachvollziehbarkeit]]]
]
]
[
[
[[Rekonstruierbarkeit]]
]
[
[,phantom]
[Nachvollziehbarkeit]
]
]
]
[
[,phantom]
[Plausibilität]
]
]
[
[,phantom]
[Keine Provenance]
]
]
空节点left just
和空right just
节点被添加到我们想要创建空节点的位置,以便绘制分隔线。如果我们真的想要在这个级别上添加注释,我们会说left just=<annotation>
。phantom
节点只是为了帮助放置兄弟节点,我们不希望在其父节点下方直接有一个子节点。
树的外观定制方式如下。为了重复使用,显然可以将其包装在样式中以方便使用。
调整注释的对齐方式:
just format={yshift=.75\baselineskip},
根据级别等有条件地配置节点等的样式:
for tree={
align=center,
if level=1{
before typesetting nodes={circle, draw, fill, inner sep=2.5pt}
}{},
if n children=0{
yshift=5pt,
}{
parent anchor=center,
child anchor=center,
if n children=2{
for children={
if n children=0{}{
before typesetting nodes={circle, draw, fill, inner sep=2.5pt},
if n=1{% create the 'Ja' and 'Nein' labels
edge label={node [midway, left, xshift=-5pt] {Ja}}
}{
if n'=1{
edge label={node [midway, right, xshift=5pt] {Nein}}
}{}
},
}
}
}{},
}{}
},
画出分隔线:
end draw/.code={
\foreach \i in {1,...,5}{
\draw (left just 2.south west |- n\i) coordinate (this \i) -- (right just \i.south west |- this \i);
}
\end{tikzpicture}
}
结果如下所示:
完整代码:
\documentclass[tikz,border=10pt]{standalone}
\usepackage{justtrees}% version 0.05
\begin{document}
\begin{justtree}
{
just format={yshift=.75\baselineskip},
for tree={
align=center,
if level=1{
before typesetting nodes={circle, draw, fill, inner sep=2.5pt}
}{},
if n children=0{
yshift=5pt,
}{
parent anchor=center,
child anchor=center,
if n children=2{
for children={
if n children=0{}{
before typesetting nodes={circle, draw, fill, inner sep=2.5pt},
if n=1{
edge label={node [midway, left, xshift=-5pt] {Ja}}
}{
if n'=1{
edge label={node [midway, right, xshift=5pt] {Nein}}
}{}
},
}
}
}{},
}{}
},
end draw/.code={
\foreach \i in {1,...,5}{
\draw (left just 2.south west |- n\i) coordinate (this \i) -- (right just \i.south west |- this \i);
}
\end{tikzpicture}
}
}
[, left just=, right just=, name=root, name=n1
[, left just=Condition 1, right just=, name=n2
[, left just=Condition 2, right just=, name=n3
[, left just=Condition 3, right just=, name=n4
[, left just=Condition 4, right just=, name=n5
[Starke Rekonstruierbarkeit\\Fehlernachvollziehbarkeit]
]
[
[[[Starke Nachvollziehbarkeit]]]
]
]
[
[
[[Rekonstruierbarkeit]]
]
[
[,phantom]
[Nachvollziehbarkeit]
]
]
]
[
[,phantom]
[Plausibilität]
]
]
[
[,phantom]
[Keine Provenance]
]
]
\end{justtree}
\end{document}