我正在尝试绘制一个统计多层次模型的表示:我正在尝试做的事情的例子可以在图 3a 或 3b 中看到,http://www.esourceresearch.org/tabid/332/Default.aspx。
我尝试过,但无法使间距正确。在下图中,和的子节点c1
重叠cn
(因此只能cn
看到的子节点)。我也不知道如何在每个级别之间适当地添加点:one
和和two
和two
之间n
,和t1
和之间tn
,等等。我尝试添加一个额外的子节点,但从外观上看,这不是可行的方法。谢谢。
我的代码
\documentclass{article}
\usepackage[landscape, left=10pt]{geometry}
\usepackage{tikz}
\begin{document}
% styles
\tikzstyle{every node} = [rectangle, draw, text centered, rounded corners, fill=blue!20, minimum height=1cm, minimum width=1cm]
\begin{tikzpicture}[sibling distance=1.25cm]
\node { one }
child { node {c1}
child { node {t1}}
child { node [draw=none] {\ldots}}
child { node {tn}}
}
child { node [draw=none] {\ldots}
}
child { node {cn}
child { node {t1}}
child { node [draw=none] {\ldots}}
child { node {tn}}
}
;
\hspace*{7cm} \node { two }
child { node {c1}
child { node {t1}}
child { node [draw=none] {\ldots}}
child { node {tn}}
}
child { node [draw=none] {\ldots}
}
child { node {cn}
child { node {t1}}
child { node [draw=none] {\ldots}}
child { node {tn}}
};
\hspace*{7cm} \node { n }
child { node {c1}
child { node {t1}}
child { node [draw=none] {\ldots}}
child { node {tn}}
}
child { node [draw=none] {\ldots}
}
child { node {cn}
child { node {t1}}
child { node [draw=none] {\ldots}}
child { node {tn}}
};
\end{tikzpicture}
\end{document}
答案1
我可以推荐专业的树木绘图软件包 Forest 吗?
\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
\begin{document}
\begin{forest}
for tree={
math content,
},
delay={
where content={}{
if level=0{}{
content=\ldots,
math content,
no edge,
fit=band,
},
}{
rounded corners,
outer color=blue!20,
inner color=blue!15,
minimum height=1cm,
minimum width=1cm,
draw,
drop shadow,
},
}
[, phantom
[one, plain content
[c_1
[t_1]
[]
[t_n]
]
[]
[c_n
[t_1]
[]
[t_n]
]
]
[two, plain content
[c_1
[t_1]
[]
[t_n]
]
[]
[c_n
[t_1]
[]
[t_n]
]
]
[]
[n
[c_1
[t_1]
[]
[t_n]
]
[]
[c_n
[t_1]
[]
[t_n]
]
]
]
\end{forest}
\end{document}
编辑
这是一个更精致的版本,它定义label levels
为应具有级别标签的树的样式。应使用level label=<label text>
和 将标签连接起来以形成该级别的标签,前面带有前缀Label <level number>
。当然,所有这些都可以调整。
我还添加了更精美的背景,但这显然是完全可选的。
\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows,fit,backgrounds}
\forestset{
declare toks={level label}{},
declare toks register={level labels},
level labels={},
declare count register={leveller},
leveller'=0,
level split/.style={
temptoksa={#1},
split register={temptoksa}{:}{tempcounta,level label split},
},
level label split/.style={
temptoksb={#1},
temptoksc={},
split register={temptoksb}{,}{temptoksc, level label splitter},
tikz+/.wrap 2 pgfmath args={
\node (label leveller ##1) [anchor=east, align=right, font=\sffamily] at (level ##1.west -| westpoint) {##2};
}{tempcounta}{temptoksc},
before computing xy/.wrap pgfmath arg={
tikz+={
\node [anchor=north east, align=right, font=\sffamily\itshape] at (label leveller ##1.north -| west of westpoint) {Level ##1};
},
}{tempcounta},
},
level label splitter/.style={
temptoksc+={\\#1},
},
label levels/.style={
tikz+={
\coordinate (westpoint) at ([xshift=-15pt]current bounding box.west);
},
before packing={
tikz+={
\coordinate (west of westpoint) at ([xshift=-15pt]current bounding box.west);
},
},
before drawing tree={
tikz+={
\scoped[on background layer]{\node [left color=blue!50!cyan!25!white, right color=blue!50!cyan!25!white, middle color=blue!50!cyan, inner sep=10pt, rounded corners, draw=blue!50!cyan, draw opacity=.5, fill opacity=.15, fit=(current bounding box)] {};}
},
},
delay={
for tree breadth-first={
if level label={}{}{
if={(level())>(leveller)}{
leveller/.option=level,
alias/.wrap pgfmath arg={level ##1}{level()},
if level labels={}{}{
level labels+={;},
},
level labels+/.option=level,
level labels+={:},
}{},
level labels+/.option=level label,
level labels+={,},
},
},
},
before typesetting nodes={
if level labels={}{}{
split register={level labels}{;}{level split},
},
},
}
}
\begin{document}
\begin{forest}
for tree={
math content,
},
delay={
where content={}{
if level=0{}{
content=\ldots,
math content,
no edge,
fit=band,
},
}{
rounded corners,
outer color=blue!20,
inner color=blue!15,
minimum height=1cm,
minimum width=1cm,
draw,
drop shadow,
},
},
label levels,
[, phantom
[one, plain content, level label=platitudes
[c_1
[t_1]
[]
[t_n]
]
[]
[c_n
[t_1]
[]
[t_n]
]
]
[two, plain content
[c_1
[t_1, level label=latitudes]
[]
[t_n]
]
[]
[c_n
[t_1]
[]
[t_n, level label=longitudes]
]
]
[]
[n
[c_1, level label=magnitudes
[t_1]
[]
[t_n]
]
[]
[c_n
[t_1]
[]
[t_n]
]
]
]
\end{forest}
\end{document}