Tikz 桶形图(分组条形图)

Tikz 桶形图(分组条形图)

我想重新创建下图。我认为最简单的方法是创建一个没有轴的简单水平条形图,并将它们链接到相应的分组条形图?但我不知道如何连接各个部分。

有人可以提供给我吗?

在此处输入图片描述

答案1

正如 frogon 所说,你把所有工作都留给回答问题的人。你至少可以想出条形图的数据。你只需使用 forest 即可。

\documentclass{article}
\usepackage[edges]{forest}
\newcounter{bary}
\newcommand{\HorBars}[2][]{\begin{tikzpicture}
\tikzset{horizontal bars/.cd,#1}
\setcounter{bary}{0}
\def\pv##1{\pgfkeysvalueof{/tikz/horizontal bars/##1}}%
\edef\tmpbars{#2}%
\edef\tmpnone{none}%
\edef\tmpfilter{\pv{filter}}%
\ifx\tmpnone\tmpfilter
\foreach \X/\Y in \tmpbars
{\stepcounter{bary}
\draw[line width=\pv{bar width},\Y] 
    (0,{(\pv{bar width}+\pv{bar distance})*\value{bary}}) 
    -- ++(\X*\pv{x unit},0);}
\else
\foreach \X/\Y [count=\Z ] in \tmpbars
{\edef\myfilter{\Y}
\ifx\myfilter\tmpfilter
\stepcounter{bary}
\draw[line width=\pv{bar width},\Y] 
    (0,{(\pv{bar width}+\pv{bar distance})*\value{bary}}) 
    -- ++(\X*\pv{x unit},0);
\fi}
\fi 
\end{tikzpicture}}
\tikzset{horizontal bars/.cd,x unit/.initial=2em,bar width/.initial=1em,bar
distance/.initial=0.1em,filter/.initial=none}
\newsavebox\sbars
\newsavebox\sbarsA
\newsavebox\sbarsB
\newsavebox\sbarsC
\newsavebox\sbarsD
\begin{document}
\edef\mybars{2/blue,5/orange,6.5/green,5.5/orange,6/green,2.5/blue,4/red,4/red,%
7/green,4.5/orange,3/blue,3.5/red}%
\savebox\sbars{\HorBars{\mybars}}%
\savebox\sbarsA{\HorBars[filter=blue]{\mybars}}%
\savebox\sbarsB{\HorBars[filter=red]{\mybars}}%
\savebox\sbarsC{\HorBars[filter=orange]{\mybars}}%
\savebox\sbarsD{\HorBars[filter=green]{\mybars}}%

\begin{forest}
forked edges,grow'=east,
for tree={where level=1{anchor=west}{},calign=fixed edge angles,l
sep+=3em,fork sep=2em,s=-2em}
[\usebox\sbars
 [\usebox\sbarsA]
 [\usebox\sbarsB]
 [\usebox\sbarsC]
 [\usebox\sbarsD]
]
\end{forest}
\end{document}

在此处输入图片描述

布局完全由 控制forest。可以更改ll sepss sepfork sep进行游戏calign

\documentclass{article}
\usepackage[edges]{forest}
\newcounter{bary}
\newcommand{\HorBars}[2][]{\begin{tikzpicture}
\tikzset{horizontal bars/.cd,#1}
\setcounter{bary}{0}
\def\pv##1{\pgfkeysvalueof{/tikz/horizontal bars/##1}}%
\edef\tmpbars{#2}%
\edef\tmpnone{none}%
\edef\tmpfilter{\pv{filter}}%
\ifx\tmpnone\tmpfilter
\foreach \X/\Y in \tmpbars
{\stepcounter{bary}
\draw[line width=\pv{bar width},\Y] 
    (0,{(\pv{bar width}+\pv{bar distance})*\value{bary}}) 
    -- ++(\X*\pv{x unit},0);}
\else
\foreach \X/\Y [count=\Z ] in \tmpbars
{\edef\myfilter{\Y}
\ifx\myfilter\tmpfilter
\stepcounter{bary}
\draw[line width=\pv{bar width},\Y] 
    (0,{(\pv{bar width}+\pv{bar distance})*\value{bary}}) 
    -- ++(\X*\pv{x unit},0);
\fi}
\fi 
\end{tikzpicture}}
\tikzset{horizontal bars/.cd,x unit/.initial=2em,bar width/.initial=1em,bar
distance/.initial=0.1em,filter/.initial=none}
\newsavebox\sbars
\newsavebox\sbarsA
\newsavebox\sbarsB
\newsavebox\sbarsC
\newsavebox\sbarsD
\begin{document}
\edef\mybars{2/blue,5/orange,6.5/green,5.5/orange,6/green,2.5/blue,4/red,4/red,%
7/green,4.5/orange,3/blue,3.5/red}%
\savebox\sbars{\HorBars{\mybars}}%
\savebox\sbarsA{\HorBars[filter=blue]{\mybars}}%
\savebox\sbarsB{\HorBars[filter=red]{\mybars}}%
\savebox\sbarsC{\HorBars[filter=orange]{\mybars}}%
\savebox\sbarsD{\HorBars[filter=green]{\mybars}}%

\begin{forest}
forked edges,grow'=east,
for tree={where level=1{anchor=west}{},
l sep+=3em,fork sep=2em,s sep=0.5em}
[\usebox\sbars,yshift=-6em
 [\usebox\sbarsA]
 [\usebox\sbarsB]
 [\usebox\sbarsC]
 [\usebox\sbarsD]
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容