Skip to content
New issue

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.

Already on GitHub? Sign in to your account

Vertical alignment of subfigures #1327

Closed
gwotto opened this issue Dec 1, 2016 · 10 comments · Fixed by #1444
Closed

Vertical alignment of subfigures #1327

gwotto opened this issue Dec 1, 2016 · 10 comments · Fixed by #1444
Milestone

Comments

@gwotto
Copy link

gwotto commented Dec 1, 2016

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
Copy link

stla commented May 12, 2017

Perhaps you can achieve this with the package oaReporting.

@stla
Copy link

stla commented May 12, 2017

@gwotto I've just replied on SO.

@eliocamp
Copy link
Contributor

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

@yihui
Copy link
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.

@eliocamp
Copy link
Contributor

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.

@yihui
Copy link
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!

@eliocamp
Copy link
Contributor

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.

@eliocamp
Copy link
Contributor

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

@yihui yihui added this to the v0.18 milestone Nov 21, 2017
@yihui
Copy link
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

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants