0

I am trying to draw hierarchical models with overlapping nodes. I am able to use Use tikz to draw a hierarchical model (mult-level model from statistics) to make hierarchical models, but I am not able to figure out how to draw overlapping nodes and also how to add weights to the edges. I want to draw the structure given in the following figure z

  • 1
    It is always a good idea to show us what you have been able to do. Apart from making it easier to answer the question, you are more likely to get a solution that is taylor-made for your situation and uses your conventions. – marmot May 8 at 21:50
4

A standard trick is to add the non-tree stuff via TikZ. The basis of all this is cfr's answer you link to.

\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},
      },
    },
  }
}
\tikzset{cute fancy rodent/.style={rounded corners,
      outer color=blue!20,
      inner color=blue!15,
      minimum height=1cm,
      minimum width=1cm,
      draw,
      drop shadow}}
\begin{document}

\begin{forest}
  for tree={
    math content,
  },
  delay={
    where content={}{
      if level=0{}{
        content=\ldots,
        math content,
        no edge,
        fit=band,
      },
    }{
      cute fancy rodent
    },
  },
  label levels,
  [, phantom
    [one, plain content, level label=platitudes
      [c_1
        [t_1,alias=t1a]
        []
        [t_n,alias=tna]
      ]
      []
      [c_n
        [t_1,alias=t1b]
        []
        [t_n,alias=tnb]
      ]
    ]
    [two, plain content
      [c_1
        [t_1, level label=latitudes,alias=t1c]
        []
        [t_n,alias=tnc]
      ]
      []
      [c_n
        [t_1,alias=t1d]
        []
        [t_n, level label=longitudes,alias=tnd]
      ]
    ]
    []
    [n
      [c_1, level label=magnitudes
        [t_1,alias=t1e]
        []
        [t_n,alias=tne]
      ]
      []
      [c_n
        [t_1,alias=t1f]
        []
        [t_n,alias=tnf]
      ]
    ]
  ]
\foreach \X in {a,...,f}  
{\path (t1\X) -- (tn\X) node[midway,below=2.5em,cute fancy rodent] 
(tm\X){$m_x$};
\draw (t1\X) -- (tm\X) (tm\X) -- (tn\X);
}
\end{forest}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.