Quiero dibujar un árbol de decisión horizontal. ¿Alguien puede ayudarme a modificar mi código?
Soy nuevo aqui. Entonces, si mi pregunta no está clara, hágamelo saber :)
¡Muchas gracias!
\documentclass[11pt]{article}
\usepackage{forest}
\forestset{
declare toks={elo}{}, % Edge Label Options
anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
dot/.default=2pt,
decision edge label/.style n args=3{
edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
},
decision/.style={if n=1
{decision edge label={left}{east}{#1}}
{decision edge label={right}{west}{#1}}
},
decision tree/.style={
for tree={
s sep=0.5em,l=8ex,
if n children=0{anchors=north}{
if n=1{anchors=south east}{anchors=south west}},
math content,
},
anchors=south, outer sep=2pt,
dot=3pt,for descendants=dot,
delay={for descendants={split option={content}{;}{content,decision}}},
}
}
\begin{document}
\begin{forest} decision tree
[N,plain content
[I;{p_1=0.5},plain content,elo={yshift=4pt}
[{5,1};a]
[II;b,plain content
[{1,2};m]
[{2,3};n]
]
]
[II;{p_2=0.5},plain content,elo={yshift=4pt}
[;c
[{1,0};z]
[{2,2};t]
]
[;d
[{3,1};z]
[{0,0};t]
]
] {\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,above]{I};}
]
\end{forest}
\end{document}
Respuesta1
Desafortunadamente, mi conocimiento sobre el forest
es demasiado débil para poder procesar su MWE. Por lo tanto, como punto de partida sobre cómo dibujar un árbol de decisión horizontal, permítanme mostrarles forest
un código de árbol más simple:
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
/tikz/every label/.append style={inner sep=1pt,font=\footnotesize},
for tree={
grow'=east, % <---
% node style
circle,
minimum size=3pt,
inner sep=0pt,
outer sep=0pt,
if n children=0{}{fill,fit=band},
% distances of nodes
l sep=13mm,
s sep=0mm,
% edges
edge={draw},
edge path'={(!u.parent anchor) -- (.child anchor)},
tier/.option=level,
% labels on edges
delay={edge label/.wrap value={node[midway, above, sloped, inner sep=1pt,
font=\scriptsize]{#1}}
}, % <---
}, % end for tree
[ , label=west:$N$
[ , label=II, edge label={$p=0.5$}
[ , edge label=$d$
[{0,0}, edge label={$t$}]
[{3,1}, edge label={$z$}]
]
[ , edge label={$c$}
[{2,2}, edge label={$t$}]
[{1,0}, edge label={$z$}]
]
]{\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,label=left:I] {};}
[ , label=I, edge label={$p=0.5$}
[ , label=II, edge label={$b$}
[{2,3}, edge label={$n$}]
[{1,2}, edge label={$m$}]
]
[{5,1}, edge label=$a$]
]
]
\end{forest}
\end{document}
lo que da:
Espero que la extensión de mi código de árbol a su solución sofisticada no le cause muchos problemas. En el primer tiempo libre intentaré descubrir cuál es su sofisticado MWE.
\documentclass[11pt]{article}
\usepackage{forest}
\forestset{
declare toks={elo}{}, % Edge Label Options
anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
dot/.default=2pt,
decision edge label/.style n args=3{
edge label/.expanded={node[midway,#1=-2pt,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
},
decision/.style={if n=1
{decision edge label={above left}{south east}{#1}}
{decision edge label={below left}{north east}{#1}}
},
decision tree/.style={
for tree={grow'=east,
s sep=0.5em,l=8ex,
if n children=0{anchors=west}{
if n=1{anchors=south}{anchors=north}},
math content,
},
anchors=south, outer sep=2pt,
dot=3pt,for descendants=dot,
delay={for descendants={split option={content}{;}{content,decision}}},
}
}
\begin{document}
\begin{forest} decision tree
[N,plain content
[I;{p_1=0.5},plain content,elo={yshift=4pt}
[{5,1};a]
[II;b,plain content
[{1,2};m]
[{2,3};n]
]
]
[II;{p_2=0.5},plain content,elo={yshift=4pt}
[;c
[{1,0};z]
[{2,2};t]
]
[;d
[{3,1};z]
[{0,0};t]
]
] {\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,left]{I};}
]
\end{forest}
\end{document}