I always have trouble getting charts properly centered. They always seem to stagger off to the right hand side of the page. Notice how the example images are perfectly centered as if they're a single figure, but the charts aren't. Their captions' appear to be centered properly, but the charts are off kilter with respect to their captions. I tried to fix it with minipages and makeboxes and so on, but I can't figure it out. What's the solution? Cheers.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}
\usepackage{xcolor,tikz}
\usetikzlibrary{backgrounds}
\definecolor{NR}{HTML}{D43F3A}
\definecolor{NO}{HTML}{EE9336}
\definecolor{NY}{HTML}{FDC431}
\definecolor{NG}{HTML}{3FAE49}
\definecolor{NB}{HTML}{0071B9}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfkeys{/donut/.cd,
inner radius/.initial=1cm,
inner radius=1cm,
outer radius/.initial=3.14cm,
outer radius=3.14cm,
text color/.initial=white,
text color=white}
\newcommand{\donutchart}[2][]{% Calculate total
\pgfmathsetmacro{\totalnum}{0}
\foreach [count=\n] \value/\colour/\name in {#2} {
\pgfmathparse{\value+\totalnum}
\global\let\totalnum=\pgfmathresult
\xdef\numitems{\n}}
\begin{tikzpicture}
\pgfmathsetmacro{\wheelwidth}{\pgfkeysvalueof{/donut/outer
radius}-\pgfkeysvalueof{/donut/inner radius}}
\pgfmathsetmacro{\midradius}{(\pgfkeysvalueof{/donut/outer radius}
+\pgfkeysvalueof{/donut/inner radius})/2}
\begin{scope}[#1]
\pgfmathsetmacro{\cumnum}{0}
\foreach \value/\colour/\name in {#2} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
\begin{scope}[on background layer]
\filldraw[draw=white,fill=\colour]
(-\cumnum:\pgfkeysvalueof{/donut/outer radius})
arc(-\cumnum:-(\newcumnum):\pgfkeysvalueof{/donut/outer radius}) --
(-\newcumnum:\pgfkeysvalueof{/donut/inner radius})
arc(-\newcumnum:-(\cumnum):\pgfkeysvalueof{/donut/inner radius}) -- cycle;
\end{scope}
\draw node [text=\pgfkeysvalueof{/donut/text color},
font=\bfseries\sffamily] at
(\midangle:{\pgfkeysvalueof{/donut/inner radius}+\wheelwidth/2}) {\name};
\global\let\cumnum=\newcumnum}
\end{scope}
\end{tikzpicture}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\donutchart[rotate=45,]{1/NR/, 1/NO/, 20/NY/, 5/NG/, 73/NB/}
\caption{Caption 1}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\centering
\donutchart[rotate=45]{1/NR/, 1/NO/, 20/NY/, 5/NG/, 73/NB/}
\caption{Caption 2}
\end{minipage}\hfill
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{example-image-a}
\caption{Caption 3}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=1\textwidth]{example-image-b}
\caption{Caption 4}
\end{minipage}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
References:
Drawing a white line/gap after each sector in pie/donut chart
How do I create a donut (or ring) chart in Overleaf?


\begin{tikzpicture}before\pgfmathsetmacro{\totalnum}{0}.) – Oct 29 '19 at 22:47