编辑

编辑

我想尝试将多个节点(例如 1、2、3 和 4 全部)路由到一个名为四分位数的单个节点。我不希望每个节点都转到它自己的名为“四分位数”的节点。所以基本上我想问如何将 4 个节点路由到一个节点,而不是像下面这样将 4 个节点路由到 4 个节点。抱歉,我是这个论坛的新手,但这是我能解释的最好的了。还有,有没有更好的方法来制作这种类型的图表?水平制作会更好吗?感谢您的帮助。

\usetikzlibrary{shadows}

\begin{forest} for tree={%
    draw=black, minimum width=1cm, minimum height=1.3cm, rounded corners=3,
    text height=1ex, text depth=0ex,
    grow=east,reversed,
    edge={black},
    parent anchor=east,
    child anchor=west,
    if n children=0{tier=last}{}
}
[data
[L2T 
    [1[quartile]][2[quartile]][3[quartile]][4[quartile]]]
[CPS 
    [1[quartile]][2[quartile]][3[quartile]][4[quartile]]]    
[HIA
    [1[quartile]][2[quartile]][3[quartile]][4[quartile]]]       
[ATT
    [1[quartile]][2[quartile]]]
]
\end{forest}

答案1

像这样?

14比4

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
\begin{document}

\begin{forest}
  for tree={%
    draw,
    minimum width=1cm,
    minimum height=1.3cm,
    rounded corners=3,
  },
  before typesetting nodes={%
    where n children=0{%
      if={strequal(content(),"quartile")}{%
        delay={%
          replace by={[, phantom, tier=last, append]},
        },
        calign with current,
        for siblings/.wrap pgfmath arg={%
          tikz+={%
            \draw (.parent anchor) -- (#1.child anchor);
          }
        }{name()},
      }{%
        delay={%
          append={[, phantom, shape=coordinate, tier=last]}
        }
      }
    }{}
  },
  before packing={%
    for tree={%
      grow'=0,
      parent anchor=east,
      child anchor=west,
    }
  }
  [data
  [L2T
      [1]
      [2]
      [quartile]
      [3]
      [4]
  ]
  [CPS
      [1]
      [2]
      [quartile]
      [3]
      [4]
  ]
  [HIA
      [1]
      [2]
      [quartile]
      [3]
      [4]
  ]
  [ATT
      [1]
      [quartile]
      [2]
  ]
  ]
\end{forest}
\end{document}

编辑

您还可以quartile使用最终节点连接节点。但是,请注意,这现在与树完全不同,用其他东西绘制它可能更容易。尽管如此,如果您已经用 做了很多工作forest,您可以尝试类似这样的修改:

\begin{forest}
  join/.style={%
    tikz+={%
      \draw [\foresteoption{edge}] (.parent anchor) -- (#1.child anchor);
    }
  },
  for tree={%
    draw,
    minimum width=1cm,
    minimum height=1.3cm,
    rounded corners=3,
  },
  before typesetting nodes={%
    where n children=0{%
      if={strequal(content(),"quartile")}{%
        append={[, phantom, tier=very last]},
        delay={%
          replace by={[, phantom, tier=last, append]},
        },
        calign with current,
        join=bl,
        for siblings/.wrap pgfmath arg={%
          append={[, phantom, shape=coordinate, tier=last]},
          join=#1,
        }{name()},
      }{%
      }
    }{}
  },
  before packing={%
    for tree={%
      grow'=0,
      parent anchor=east,
      child anchor=west,
    }
  }
  [data
    [L2T
      [1]
      [2]
      [quartile, name=q1]
      [3]
      [4]
    ]
    [CPS
      [1]
      [2]
      [quartile, name=q2]
      [3]
      [4]
    ]
    [, phantom, tier=very last, calign with current
      [beyond last, calign with current, name=bl]
    ]
    [HIA
      [1]
      [2]
      [quartile, name=q3]
      [3]
      [4]
    ]
    [ATT
      [1]
      [quartile, name=q4]
      [2]
    ]
  ]
\end{forest}

14 2 4 + 4 2 1

更新

不过,我可能会尝试这样的方法,因为我认为它使图表更清晰一些:

方形外观

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{forest}
\useforestlibrary{edges}
\begin{document}
\forestapplylibrarydefaults{edges}
\begin{forest}
  join/.style={%
    tikz+={% adapted from edges library
      \draw  (.parent anchor)  -- ++(\forestoption{fork sep},0) |- (#1.child anchor);
    }
  },
  forked edges,
  for tree={%
    draw,
    minimum width=1cm,
    minimum height=1.3cm,
    rounded corners=3,
    anchor=west,
  },
  before typesetting nodes={%
    where n children=0{%
      if={strequal(content(),"quartile")}{%
        append={[, phantom, tier=very last]},
        delay={%
          replace by={[, phantom, shape=coordinate, calign with current, append]},
        },
        calign with current,
        tier=last,
        no edge,
        anchor=west,
        join=bl,
        for siblings/.wrap pgfmath arg={%
          append={[, phantom, shape=coordinate, tier=last]},
          join=#1,
        }{name()},
      }{%
      }
    }{}
  },
  before packing={%
    for tree={%
      grow'=0,
      parent anchor=east,
      child anchor=west,
    }
  }
  [data
    [L2T
      [1]
      [2]
      [quartile, name=q1]
      [3]
      [4]
    ]
    [CPS
      [1]
      [2]
      [quartile, name=q2]
      [3]
      [4]
    ]
    [, phantom, calign with current, shape=coordinate
      [beyond last, tier=very last, calign with current, name=bl]
    ]
    [HIA
      [1]
      [2]
      [quartile, name=q3]
      [3]
      [4]
    ]
    [ATT
      [1]
      [quartile, name=q4]
      [2]
    ]
  ]
\end{forest}
\end{document}

相关内容