6

I'm a LaTeX rookie so I'm hoping someone will be able to help me. I've been stuck with this figure for quite some time and haven't found a solution.

Specifically, I have made 2 pie charts next to each other with a joined legend. The default option seems to stack the legend box in between the charts. How do I:

  1. Move the legend down south to underneath the boxes (I've try \legend.style which did not seem to do anything)
  2. Add a text to the top of each box to specify which group the data comes from (i.e. label Group 1 for the left box, and Group 2 for the right box).

Thank you very much for your help!

My MWE is:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgf-pie}

\begin{document}\begin{figure}[h!]
\begin{tikzpicture}
\tikzset{lines/.style={draw=white},}


\pie [square, scale font,
            color = {blue!10, blue!20, blue!40}, sum=auto, text=legend, after number = {\%}, every only number node/.style={text=black},style={lines}]{58/Unidentifiable ML,25/MOP only,11/SMO only,6/MOP \& SMP} 

\pie [pos={11,0}, square, scale font,
            color = {blue!10, blue!20, blue!40}, sum=auto, after number = {\%},every only number node/.style={text=black},style={lines}]{56/,18/,10/,16/}


\end{tikzpicture}
\caption{How do I move the legend down south}
\label{fg:R1pie}
\end{figure}

\end{document}

2 Answers 2

2

I think bar chart or circular chart is more convenient, but if you stick to that kind of rectangles, then below is a way with plain TikZ. Plain TikZ makes controls easier. Other thing I like in this figure is that it illustrates the way of TikZ drawing: putting node and pic along path.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.8]  
\tikzset{box/.pic={
\fill[#1] (-.2,-.2) rectangle (.2,.2);
}}

\begin{scope}[yshift=2mm]
\path
(0,0)     pic[fill=blue!10]{box} node[right=2mm]{MOP \& SMP}
++(90:.6) pic[fill=blue!20]{box} node[right=2mm]{SMO only}
++(90:.6) pic[fill=blue!30]{box} node[right=2mm]{MOP only}
++(90:.6) pic[fill=blue!40]{box} node[right=2mm]{Unidentifiable ML};
\end{scope}

\newcommand{\block}[6]{
\fill[#1,draw=#2] #3 rectangle #4;
\path ($.5*#4+.5*#3$) node[scale=#5]{#6};
}

\begin{scope}[shift={(180:7)},scale=6,local bounding box=L]
\block{blue!10}{white}{(0,0)}{(.58,1)}{2.5}{$58\%$}
\block{blue!20}{white}{(.58,0)}{(1,25/42)}{2}{$25\%$}
\block{blue!30}{white}{(.58,25/42)}{(1,36/42)}{1.5}{$11\%$}
\block{blue!40}{white}{(.58,36/42)}{(1,1)}{1.2}{$6\%$}
\path (L.north) node[above,scale=2]{\bfseries Group 1};
\end{scope}

\begin{scope}[shift={(0:4.5)},scale=6,local bounding box=R]
\block{blue!10}{white}{(0,0)}{(.56,1)}{2.5}{$56\%$}
\block{blue!20}{white}{(.56,0)}{(1,18/44)}{2}{$18\%$}
\block{blue!30}{white}{(.56,18/44)}{(1,28/44)}{1.5}{$10\%$}
\block{blue!40}{white}{(.56,28/44)}{(1,1)}{2}{$16\%$}
\path (R.north) node[above,scale=2]{\bfseries Group 2};
\end{scope}
\end{tikzpicture}
\end{center}
\end{document} 
1
  • 1
    This works beautifully! Thank you 3000! Commented Aug 10, 2019 at 14:05
2

Maybe by changing the paramaters of the original code from pgf-pie.sty

% legend
  \iflegend
  \coordinate[xshift=0.8cm,
  yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm] (legendpos) at
  (current bounding box.east);

as a "code-style":

\tikzset{
legend to the south/.code={
\coordinate[xshift=-1.5cm,
yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm-4em] (legendpos) at
(current bounding box.south);},
}

and then: \pie[style={legend to the south}, ...]

enter image description here

\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgf-pie}

\begin{document}
\tikzset{
legend to the south/.code={
\coordinate[xshift=-1.5cm,
yshift=(\value{pgfpie@sliceLength}*0.5+1)*0.5cm-4em] (legendpos) at
(current bounding box.south);},
}

\begin{tikzpicture}[
every path/.style={draw=white, very thick}
]
\pie[square, 
scale font,
color = {blue!10, blue!20, blue!40}, 
sum=auto, text=legend, 
after number = {\%}, 
style={legend to the south}
]{58/Unidentifiable ML,25/MOP only,11/SMO only,6/MOP \& SMP} 
\end {tikzpicture}
\end{document}
1
  • Thank you so much. I have been searching for this and this is the exact solution needed. Would have a voted up twice if I could. Cheers!!!
    – theOne
    Commented Apr 15, 2020 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .