如何在 knit pdf 文档中的一行上并排插入三个或更多 r 图?

问题描述 投票:0回答:1

我正在使用 Rstudio 生成一个以 r 图作为子图的图。 我知道如何编织 pdf (.rnw),通过使用

fig.show = 'hold'
参数将两个 r 图并排放在同一行上,在同一页上插入 6 个数字。

请在下面找到一个最小的工作示例。

我的 r 图实际上是复杂的 Kaplan-Meier 生存曲线,带有使用 survminer 包的 ggsurvplot 函数创建的注释。

\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}

这将产生图 1。

但是,如何生成一页图,其中一行上有 3 个、4 个或更多 r 绘图子图?

我找不到执行此操作的块命令。

预先感谢您的帮助。

查尔斯。

r plot knitr sweave subfigure
1个回答
0
投票

两个并排的图,每个图都有自己的标题。希望这有帮助

\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}
© www.soinside.com 2019 - 2024. All rights reserved.