用于对齐和每个节点的姊妹距离的树、组合森林和 Tikz 包

用于对齐和每个节点的姊妹距离的树、组合森林和 Tikz 包

我尝试创建树形图,并且确切地知道我想要实现什么(这似乎很简单),但进展并不顺利。使用 tikz 包时,我确实实现了我想要的结果,但是,第二级节点不在同一个对齐方式中。在查看了遇到同样问题的人之后,我尝试了 forest 包,它确实创建了每个级别的清晰对齐方式(就像我想要的那样)。但是,在 forest 包中,似乎姐妹距离在每个级别上都是不可更改的,这是我更喜欢的。

基本上,我希望结果看起来像我包含的第一个树形图,但与第二个树形图处于同一水平对齐,以便“非西方”和“西方”节点完全处于相同的垂直对齐上。我该如何解决这个问题?有没有办法将这两个代码合并成我想要的结果?

在此处输入图片描述

两张图的代码均为:

\usetikzlibrary{trees}
\newlength\treeheight
\footnotesize
\noindent\begin{tikzpicture}[
  grow=right,
  anchor=west,
  growth parent anchor=east, 
  parent anchor=east, 
  level 1/.style={sibling distance=1.8cm, level distance=1cm},
  level 2/.style={sibling distance=1cm, level distance=1cm},
  level 3/.style={sibling distance=0.5cm, level distance=1cm}]
  \node {Total sample}[edge from parent fork right]
    child {node {Native}}
        child { node {First-generation}
      child{ node {Non-Western}
        child{ node {Turkey}}
        child{ node {Morocco}}
        child{ node {Surinam}}
     child{ node {Netherlands Antilles}}
        child{ node {Other}}
      }
      child{ node {Western}}}
    child {node {Second-generation}
      child{ node {Western}}
      child{ node {Non-Western}
             child{ node {Turkey}}
        child{ node {Morocco}}
        child{ node {Surinam}}
             child{ node {Netherlands Antilles}}
          child{ node {Other}}}
    }

  ;
  \end{tikzpicture} 

\footnotesize
\usetikzlibrary{shadows}
\begin{forest}
    for tree={% style of tree nodes
        text width = 2.7cm, text badly centered,
              % style of tree (edges, distances, direction)
           anchor = east,
             grow = east,
    forked edge,            % for forked edge
            s sep = 1mm,    % sibling distance
            l sep = 8mm,    % level distance
         fork sep = 6mm,    % distance from parent to branching point
               }
[Total sample
    [Second generation
        [Non Western
            [Turkey]
            [Morocco]
            [Surinam]
            [Antilles]
            [Other]

        ]
        [Western]
    ]
%
    [Natives, before computing xy={s/.average={s}{siblings}}]
%
    [First generation
        [Non Western
            [Turkey]
            [Morocco]
            [Surinam]
            [Antilles]
            [Other]

        ]
        [Western]
    ]
]
    \end{forest}

答案1

通过forest添加for tree选项tier/.option=level您将获得:

在此处输入图片描述

\documentclass[border=2pt]{standalone}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
    for tree={% style of tree nodes
        text width = 27mm, text badly centered,
              % style of tree (edges, distances, direction)
           anchor = east,
             grow = east,
    forked edge,            % for forked edge
            s sep = 1mm,    % sibling distance
            l sep = 8mm,    % level distance
         fork sep = 4mm,    % distance from parent to branching point
tier/.option=level
               }
[Total sample
    [Second generation
        [Non Western
            [Turkey]
            [Morocco]
            [Surinam]
            [Antilles]
            [Other]
%
        ]
        [Western]
    ]
%
    [Natives, before computing xy={s/.average={s}{siblings}}]
%
    [First generation
        [Non Western
            [Turkey]
            [Morocco]
            [Surinam]
            [Antilles]
            [Other]
%
        ]
        [Western]
    ]
]
    \end{forest}
\end{document}

注意,我会将节点宽度减小到例如 21 毫米并得到以下结果:

在此处输入图片描述

相关内容