[Tex/LaTex] Figures stacked rather than side by side when using minipage

minipagesubcaption

I am trying to put a figure, next to four other figures, two up and two down. That is, one figure on the left side, four small figures on the right side (2 by 2)
But whenever I add the subcaption, they end up stacking up instead.

I'd be very grateful if anyone can help me.
Thanks!

\documentclass[]{article}
\usepackage[margin=0.8in]{geometry}
\usepackage{graphicx}
\usepackage{subcaption}    
\begin{document}   
\begin{figure}[ht!]
    \captionsetup{justification=centering}
    \centering
    \begin{minipage}{.49\linewidth}
        \centering
        \includegraphics[width=0.9\linewidth]{figfit22.png}
        \subcaption{Measured spacing as function of pitch}
    \end{minipage}
    \hfill
    \centering
    \begin{minipage}{.49\linewidth}
\centering
\includegraphics[width=0.31\linewidth]{dif42.png}\subcaption{Pitch: 24 $\mu$m}
\includegraphics[width=0.31\linewidth]{dif12.png}\subcaption{Pitch: 83 $\mu$m}
\includegraphics[width=0.31\linewidth]{dif22.png}\subcaption{Pitch: 125 $\mu$m}
\includegraphics[width=0.31\linewidth]{dif32.png}\subcaption{Pitch: 250 $\mu$m}
    \end{minipage}
\end{figure}
\end{document}

Best Answer

Use \subcaptionbox{<caption>}{<image>} instead. Note the warning in the subcaption manual:

Please note that the \subcaption command must be applied inside its own box or environment.

So when using \subcaption, each of them must have its own minipage or similar. With \subcaptionbox you don't need that.

(As I don't have your images I added the demo option to graphicx, you want to remove that.)

output of code

\documentclass[]{article}
\usepackage[margin=0.8in]{geometry}
\usepackage[demo]{graphicx}
\usepackage{subcaption}    
\begin{document}   
\begin{figure}[ht!]
    \captionsetup{justification=centering}
    \centering
    \begin{minipage}{.49\linewidth}
        \centering
        \includegraphics[width=0.9\linewidth]{figfit22.png}
        \subcaption{Measured spacing as function of pitch}
    \end{minipage}
    \hfill
    \begin{minipage}{.49\linewidth}
\centering
\subcaptionbox{Pitch: 24 $\mu$m}{\includegraphics[width=0.31\linewidth]{dif42.png}}
\subcaptionbox{Pitch: 83 $\mu$m}{\includegraphics[width=0.31\linewidth]{dif12.png}}

\subcaptionbox{Pitch: 125 $\mu$m}{\includegraphics[width=0.31\linewidth]{dif22.png}}
\subcaptionbox{Pitch: 250 $\mu$m}{\includegraphics[width=0.31\linewidth]{dif32.png}}
    \end{minipage}
\end{figure}
\end{document}