[Tex/LaTex] subfigures: caption for every row and every column

floatssubcaption

I have three models that I test under different parameter configurations. I created a figure where every column corresponds to a model. I would like to add the value of the parameter at the left of every row. After reading this answer, this is what I do:

\documentclass{article}

\usepackage[draft]{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[t]{0.30\textwidth}
    \rotatebox[origin=c]{90}{$R=1$}
    \includegraphics[width=\textwidth]
    {test.png}
    \rotatebox[origin=c]{90}{$R=2$}
    \includegraphics[width=\textwidth]
    {test.png}
    \rotatebox[origin=c]{90}{$R=3$}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model A}
\end{subfigure}
\hspace{1em}
\begin{subfigure}[t]{0.30\textwidth}
    \includegraphics[width=\textwidth]  
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model B}
\end{subfigure}
\hspace{1em}
\begin{subfigure}[t]{0.30\textwidth}
    \includegraphics[width=\textwidth]  
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model C}
\end{subfigure}
\caption{Grid test}
\end{figure}

But I get this:
test

How can I make it right?

Best Answer

One possibility (the % signs suppressing spurious blank spaces are essential):

Update

A modified version (the whole construct now stays inside the text width):

\documentclass{article}
\usepackage[draft]{graphicx}
\usepackage{subcaption}
\usepackage{showframe}% just for visiual guide

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[t]{\dimexpr0.30\textwidth+20pt\relax}
    \makebox[20pt]{\raisebox{40pt}{\rotatebox[origin=c]{90}{$R=1$}}}%
    \includegraphics[width=\dimexpr\linewidth-20pt\relax]
    {test.png}
    \makebox[20pt]{\raisebox{40pt}{\rotatebox[origin=c]{90}{$R=2$}}}%
    \includegraphics[width=\dimexpr\linewidth-20pt\relax]
    {test.png}
    \makebox[20pt]{\raisebox{40pt}{\rotatebox[origin=c]{90}{$R=3$}}}%
    \includegraphics[width=\dimexpr\linewidth-20pt\relax]
    {test.png}
    \caption{model A}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.30\textwidth}
    \includegraphics[width=\textwidth]  
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model B}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.30\textwidth}
    \includegraphics[width=\textwidth]  
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model C}
\end{subfigure}
\caption{Grid test}
\end{figure}

\end{document}

The result:

enter image description here

First version

(labels protrude to the left margin):

\documentclass{article}
\usepackage[draft]{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}[t]{0.30\textwidth}
    \makebox[0pt][r]{\makebox[30pt]{\raisebox{40pt}{\rotatebox[origin=c]{90}{$R=1$}}}}%
    \includegraphics[width=\textwidth]
    {test.png}
    \makebox[0pt][r]{\makebox[30pt]{\raisebox{40pt}{\rotatebox[origin=c]{90}{$R=2$}}}}%
    \includegraphics[width=\textwidth]
    {test.png}
    \makebox[0pt][r]{\makebox[30pt]{\raisebox{40pt}{\rotatebox[origin=c]{90}{$R=3$}}}}%
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model A}
\end{subfigure}
\hspace{1em}
\begin{subfigure}[t]{0.30\textwidth}
    \includegraphics[width=\textwidth]  
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model B}
\end{subfigure}
\hspace{1em}
\begin{subfigure}[t]{0.30\textwidth}
    \includegraphics[width=\textwidth]  
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \includegraphics[width=\textwidth]
    {test.png}
    \caption{model C}
\end{subfigure}
\caption{Grid test}
\end{figure}

\end{document}

The result:

enter image description here

Adjust the lengths in the \raiseboxes and the inner \makeboxes according to your needs.