# yihui/knitr

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

# Vertical alignment of subfigures #1327

Closed
opened this Issue Dec 1, 2016 · 9 comments

4 participants

### gwotto commented Dec 1, 2016 • edited by yihui

 This question is related to the StackoverFlow post http://stackoverflow.com/questions/30664820/subfigures-stacked-vertically-with-knitr that never received an aswer and http://stackoverflow.com/questions/12546365/subfigures-or-subcaptions-with-knitr where the problem is also mentioned I can use subfigures using knitr in the way described there, but they are always aligned horizontally. It would be an awsome feature to be able to introduce line breaks, so subfigures can be aligned vertically, can be aligned as a 2x2 grid etc... http://stackoverflow.com/q/40932462/559676

### stla commented May 12, 2017

 Perhaps you can achieve this with the package oaReporting.

### stla commented May 12, 2017

 @gwotto I've just replied on SO.
Contributor

### eliocamp commented Oct 10, 2017

 This could be achieved natively and more elegantly with a ncol/nrow argument. Would this be a possible improvement, @yihui ?
Owner

### yihui commented Oct 10, 2017

 @eliocamp You mean par() in base graphics, right? It is easy to layout multiple plots in any way you want, but the problem is subfigure captions. I have never tried subfigures with R Markdown documents, so I don't really know the answer at the moment.
Contributor

### eliocamp commented Oct 10, 2017

 I was talking about this problem, which is related to this one. Using par() doesn't cut it because is specific to base graphics and, as you say, does not allow for subcaptions. What I'm wondering if knitr can have a ncol chunk option that puts linebreaks between subfigures. That would solve this issue and the linked one above. So, for example, this: {r pressure, out.extra = "", echo = FALSE, fig.show = "hold", fig.cap = "Pressure", fig.subcap = c("one","two"), ncol = 1} plot(pressure) ggplot2::qplot(temperature, pressure, data = pressure)  Returns this: \begin{figure} \subfloat[one\label{fig:pressure1}]{\includegraphics{test_files/figure-latex/pressure-1} } \subfloat[two\label{fig:pressure2}]{\includegraphics{test_files/figure-latex/pressure-2} }\caption{Pressure}\label{fig:pressure} \end{figure}  And I'm saying it could return this: \begin{figure} \subfloat[one\label{fig:pressure1}]{\includegraphics{test_files/figure-latex/pressure-1} } \newline \subfloat[two\label{fig:pressure2}]{\includegraphics{test_files/figure-latex/pressure-2} }\caption{Pressure}\label{fig:pressure} \end{figure}  I think a small change in hook_plot_tex() that adds \newline after.... align2? could do it. But I'm just know reading the source code, so I might be wrong.
Owner

### yihui commented Oct 11, 2017

 @eliocamp I see. Actually I think you are getting close. A new chunk option fig.ncol sounds like a good idea to me, but I don't have time for this task. You are welcome to play with it, and send a pull request. Thanks!
Contributor

### eliocamp commented Oct 11, 2017

 I'll try that. Although now I'm thinking that maybe it would be better to use the \subfigure environment from the subcaption package instead of \subfloat. From what I see, it wraps figures correctly. If there's a reason for using \subfloat I'll try the \newline route. If not, I can try to get \subfigure working. I guess fixing \subfloat would be the more backwards-compatible way of doing things.

Merged

Contributor

### eliocamp commented Oct 11, 2017

 Scratch that. It was nonsense. There's the pull request.

Owner

### yihui commented Nov 22, 2017

 If you need an R Markdown example, please see #1444 (comment) For an Rnw example, see 119: https://github.com/yihui/knitr-examples
to join this conversation on GitHub. Already have an account? Sign in to comment