How to insert three or more r plots side by side on one row in a knit to pdf document?

79 Views Asked by At

I am using Rstudio to produce a figure with r plot as subfigures. I know how to knit to pdf (.rnw) to insert 6 figures on the same page by putting two r plots side by side on the same line using the fig.show = 'hold' argument.

Please find below a minimal working example.

My r plots are in fact complex Kaplan-Meier survival curves with annotations created with the ggsurvplot function of the survminer package.

\documentclass{article}
\usepackage[left= 1 cm, top= 1 cm, right= 1 cm, bottom= 3 cm]{geometry}

\pagenumbering{gobble}

\begin{document}

<< read_R_script, eval = TRUE, include = FALSE, echo = TRUE, tidy = TRUE>>=
require (cars)
@

\begin{figure}
\centering
<< os_km_curves_1, eval = TRUE, include = TRUE, echo= FALSE, out.width = "40%", fig.show = 'hold', message = FALSE>>=

### figure 1A
plot (mpg ~ hp, data = mtcars, pch = 19, col = "red", cex = 1, main = "plot 1")

### figure 1B
plot (mpg ~ hp, data = mtcars, pch = 18, col = "blue", cex = 1.1, main = "plot 2")

### figure 1C
plot (mpg ~ hp, data = mtcars, pch = 17, col = "green", cex = 1.2, main = "plot 3")

### figure 1D
plot (mpg ~ hp, data = mtcars, pch = 16, col = "black", cex = 1.3, main = "plot 4")

### figure 1E
plot (mpg ~ hp, data = mtcars, pch = 15, col = "orange", cex = 1.4, main = "plot 5")

### figure 1F
plot (mpg ~ hp, data = mtcars, pch = 14, col = "violet", cex = 1.5, main = "plot 6")

@
\caption{Two figures in one line}
\end{figure}

\end{document}

This produce the figure 1. knit

However, how can I produce a one page figure with 3, 4 or more r plots sub figures on one row ?

I can't find a chunk command to execute this.

Thank you in advance for your help.

Charles.

1

There are 1 best solutions below

0
rEbbasta On

Two plots side by side each with its own caption. Hope this hepls

\documentclass{article}
\usepackage{caption}

\begin{document}
\SweaveOpts{concordance=TRUE}
\SweaveOpts{prefix.string=figures/fig} % sets figures directory for output pdf images

\begin{figure}
\begin{minipage}{.4\textwidth}
\centering
\captionbox{caption 1}{
\fbox{
<<fig=TRUE, echo=FALSE,height=12,width=25>>==
plot(c(1,2,3), c(1,2,3))
@
} 
}
\end{minipage}
\begin{minipage}{.4\textwidth}
\centering
\captionbox{caption 1}{
\fbox{
<<fig=TRUE, echo=FALSE,height=12,width=25>>==
plot(c(1,2,3), c(1,2,3))
@
} 
}
\end{minipage}
\end{figure}


\end{document}