Sub-figures stacked vertically with nanite

I am trying to fit two sub-shapes stacked vertically instead of side-by-side using knitr in a latex document. In Latex I usually do this by placing a "\ par" command between two subfloats that should be stacked on top of each other, but I don't know how to pass this command to knitr.

I could only find one topic, Subshapes or Subheadings with knitr? ... The example provided by Yihui puts numbers side by side. The final answer in this thread achieves my goal, but using quite a bit of a hack, so I'm wondering if there is an easy way to pipe this command straight into the book?

MWE:

\documentclass[a4paper]{article} 

\usepackage[font=footnotesize]{subfig}

\begin{document} 

<<GeneratePlot>>=
library(ggplot2)
library(tikzDevice)
P1 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
    geom_line()
P2 <-  ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
    geom_point()
@


<<fig1,eval=TRUE,echo=FALSE,dev='tikz', fig.width=6, fig.height=3, out.width='1\\textwidth', fig.cap='Two figures', fig.subcap=c('Top','Bottom'), fig.show='asis', fig.pos='!htpb', fig.align='center', dependson='GeneratePlot'>>= 
P1
P2
@ 

\end{document}

      

I also tried to create a plot object in the same snippet as the output, but that gives the same result and in my original document the plot objects are generated as part of a larger chunk, so it will have the same structure as the MWE.

+3


source to share


2 answers


You can do this with the non-CRAN oaReporting package .

\documentclass[a4paper]{article} 
\usepackage{float}
\usepackage{subcaption}

\begin{document} 

<<setup, include=FALSE>>=
knitr::opts_chunk$set(fig.path="figures/")
library(oaReporting)
@

<<GeneratePlot>>=
library(ggplot2)
P1 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
    geom_line()
P2 <-  ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
    geom_point()
path1 <- createPlot(P1, file="./figures/P1.pdf")
path2 <- createPlot(P2, file="./figures/P2.pdf")
captions <- c("P1", "P2")
@

<<results="asis", fig.keep="none">>=
insertFigures(paths = c(path1, path2), captions=captions,
              generalCaption = "P1 and P2",
              generalLabel = "fig:P1andP2",
              posMultipleFig = "H",
              nCol = 1, 
              width = 0.7)
@

\end{document}

      



enter image description here

+1


source


This is officially supported in knitr 1.17.19 ( current on Github ). To add \par

between two sub-shapes you can use the chunk option fig.sep = '\\par'

, for example

\documentclass[a4paper]{article}

\usepackage[font=footnotesize]{subfig}

\begin{document}

<<GeneratePlot>>=
library(ggplot2)
library(tikzDevice)
P1 <- ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
    geom_line()
P2 <-  ggplot(data=mpg, aes(x=displ, y=hwy, colour=factor(cyl)))+
    geom_point()
@


<<fig1, echo=FALSE,dev='tikz', fig.width=6, fig.height=3, out.width='1\\textwidth', fig.cap='Two figures', fig.subcap=c('Top','Bottom'), fig.show='asis', fig.pos='!htpb', fig.sep='\\par'>>=
P1
P2
@

\end{document}

      



Output:

vertically stacked knitr sub-shapes

+1


source







All Articles