0

How can i get all the same coloured boxes to line up nice and neatly?

Is there a better way of doing what I am trying to do?

This is my first attempt at tikz...

Thank you!

enter image description here

%% Package for creating diagrams
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{1} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=red!30]
\tikzstyle{2} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=blue!30]
\tikzstyle{3} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=orange!30]
\tikzstyle{4} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=green!30]
\tikzstyle{5} = [rectangle, rounded corners, minimum width=2cm, minimum height=1cm,text centered, draw=black, fill=red!]

\tikzstyle{arrow} = [thick,->,>=stealth]


\begin{tikzpicture}

\node (disciplines) [1] {Disciplines};

\node (humanities) [2, right of=disciplines, xshift=2cm, yshift=2.5cm] {Humanities};
\node (visual_arts) [3, right of=humanities, xshift=3cm] {Visual Arts};
\node (applied_arts) [4, right of=visual_arts, xshift=3cm] {Applied Arts};
\node (architecture) [5, right of=applied_arts, xshift=3cm] {Architecture};

\draw [arrow] (disciplines) -- (humanities);
\draw [arrow] (humanities) -- (visual_arts);
\draw [arrow] (visual_arts) -- (applied_arts);
\draw [arrow] (applied_arts) -- (architecture);

\node (social_sciences) [2, right of=disciplines, xshift=2cm] {Social Sciences};
\node (sociology) [5, right of=social_sciences, xshift=9cm, yshift=0.625cm] {Architectural Sociology};
\node (psychology) [3, right of=social_sciences, xshift=3cm, yshift=-0.625cm] {Psychology};
\node (behavioural_sciences) [5, right of=psychology, xshift=6cm] {Behavioural Sciences};

\draw [arrow] (disciplines) -- (social_sciences);
\draw [arrow] (social_sciences) -- (sociology);
\draw [arrow] (social_sciences) -- (psychology);
\draw [arrow] (psychology) -- (behavioural_sciences);

\node (applied_sciences) [2, right of=disciplines, xshift=2cm, yshift=-2.5cm] {Applied Sciences};
\node (engineering_and_tech) [3, right of=applied_sciences, xshift=4cm] {Engineering and technology};
\node (building_physics) [5, right of=engineering_and_tech, xshift=5cm] {Building Physics};

\draw [arrow] (disciplines) -- (applied_sciences);
\draw [arrow] (applied_sciences) -- (engineering_and_tech);
\draw [arrow] (engineering_and_tech) -- (building_physics);

\end{tikzpicture}

3 Answers3

3

Here is a solution with matrix of nodes.

enter image description here

Note that

  1. Its recommended to use tikzset instead of tikzstyle. Read: Should \tikzset or \tikzstyle be used to define TikZ styles?
  2. text width=3cm is set to make the nodes have same width.
  3. An intermediate node is added in the second row so that the arrow to final column can be drawn horizontally.
  4. Some vertical spacing adjustment is done at 2nd and third rows to get uniformly spaced nodes in the final column.
  5. Give a Minimal Working Example in the question whenever possible instead of code snippet. See the MWE that produced the above figure.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
1/.style={fill=red!30},
2/.style={fill=blue!30},
3/.style={fill=orange!30},
4/.style={fill=green!30},
5/.style={fill=red},
arrow/.style={thick,->,>=stealth},
}

\begin{document}
\begin{tikzpicture}
\matrix(m)[matrix of nodes,column sep=1cm, row sep=0.5cm,
nodes={rectangle, rounded corners, text width=3cm, minimum height=1cm,text centered, draw=black,anchor=west},
]{
 & \node[2](humanities){Humanities}; &\node[3](visual_arts){Visual Arts}; & \node[4](applied_arts){Applied Arts}; & \node[5](architecture){Architecture};\\
 &  & \node[draw=none](int){}; & & \node[5](sociology){Architectural Sociology};\\[-0.75cm]
\node[1](disciplines){Disciplines}; &\node[2](social_sciences){Social Sciences}; & & &\\[-0.75cm]
 &   & \node[3](psychology){Psychology}; & & \node[5](behavioural_sciences){Behavioural Sciences};\\
& \node[2](applied_sciences){Applied Science}; & \node[3](engineering_and_tech){Engineering and  technology}; & & \node[5](building_physics){Building Physics};\\
};

\draw [arrow] (disciplines) -- (humanities);
\draw [arrow] (humanities) -- (visual_arts);
\draw [arrow] (visual_arts) -- (applied_arts);
\draw [arrow] (applied_arts) -- (architecture);

\draw [arrow] (disciplines) -- (social_sciences);
\draw [arrow] (social_sciences) --(int.center)-- (sociology);
\draw [arrow] (social_sciences) -- (psychology);
\draw [arrow] (psychology) -- (behavioural_sciences);

\draw [arrow] (disciplines) -- (applied_sciences);
\draw [arrow] (applied_sciences) -- (engineering_and_tech);
\draw [arrow] (engineering_and_tech) -- (building_physics);
\end{tikzpicture}
\end{document}
nidhin
  • 7,932
1

Such diagrams are called trees, and a convenient tool to draw them is forest, which is based on TikZ. There the alignment can be achieved by setting appropriate tiers.

\documentclass[tikz,border=3mm]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shadows.blur}
\begin{document}
\begin{forest}
for tree={grow'=0,rounded corners,text centered,draw,
    edge={-stealth,semithick},
    forked edges,
    anchor=center,
    l sep=2em,  
    fork sep=1em,
    font=\sffamily,
    blur shadow,
    text width={width("and technology")},
    minimum height=1cm,
    where level=0{fill=red!30}{},
    where level=1{fill=blue!30}{},
    where level=2{fill=orange!30}{},
    where level=3{fill=green!30}{},
    if n children=0{fill=red,tier=murmel}{}
    }
 [Disciplines
  [Humanities
   [Visual arts
    [Applied arts
     [Architecture]
    ]
   ]
  ]
  [Social sciences
   [Architectural Sociology]
   [Psychology
    [Behavioural Sciences]
   ]
  ]
  [Applied sciences
   [Engineering and technology
    [Building Physics]
   ]
  ]
 ]
\end{forest}
\end{document}

enter image description here

1

Your "flowchart" on a bit different way:

enter image description here

With use of the forest, its forked edge option, grouping s sep distance, anchoring of nodes at west. The coloring of nodes and tier option are borrowed from Schrödinger's cat's first version of his answer, styles of nodes are similar as are in @nidhin answer. The MWE is:

\documentclass[tikz,border=3mm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
% nodes style
    draw, rounded corners,
    text centered,
    text width=24mm,
    minimum height=1cm,
    anchor=west,
% tree style
    grow' = 0,
    forked edge,    % for forked edge
    s sep = 4mm,    % "sibling" distance
    l sep = 6mm,    % "level" distance
 fork sep = 3mm,    % distance from parent to branching point
% nodes coloring and grouping 
    where level=0{fill=red!30}{s sep=2mm},
    where level=1{fill=blue!30}{},
    where level=2{fill=orange!30}{},
    where level=3{fill=green!30}{},
    if n children=0{fill=red, tier=level}{},
    }
% tree body
 [Disciplines, calign=center
  [Humanities
   [Visual arts
    [Applied arts
     [Architecture]
    ]
   ]
  ]
  [Social sciences, calign=center
        [Architectural Sociology]
   [Psychology
    [Behavioural Sciences]
   ]
  ]
  [Applied sciences
   [Engineering and technology
    [Building Physics]
   ]
  ]
 ]
\end{forest}
\end{document}
Zarko
  • 296,517
  • This is to a large extent copied from my answer. –  Nov 07 '19 at 17:10
  • partly you are right. I mentioned this in my answer ... – Zarko Nov 07 '19 at 17:38
  • Well, the main point that this is a tree and one can use forest, and to align the nodes, which can be achieved with tier. All you do is to replace tier=murmel by tier=level, and add some stylistic options that are not central. –  Nov 07 '19 at 17:46