我正在迈出第一步:knitr
,试图建立融洽关系。在报告中,我包括R
生成一个 ggplot2 对象的代码,我想将其直接包含在某些文本下方。为了使其更详细,该图形是一对两个独立的图,我想将它们平行放置,一个挨着另一个。
到目前为止,我一直在使用R
来处理代码:生成并保存一张 .pdf 图片,然后从文件中读取该图片并将其包含在 \includegraphics
的报告中命令。然而,这对我来说不再是一个解决方案 - 我希望与 R
的报告同时生成图。代码(特别是:不要将其保存为 .pdf)
但是,我尝试使用的代码无法正常工作 - 它生成了 2 个图,但它们是:
1) 位置不正确 - 下面 2 页(甚至不是文档的结尾!)
2) 我不知道如何将它们按定义的大小排成一行。
请大家帮忙!先感谢您!! [下面我的工作不正常R
代码]
\textit{Pic 1 title} Some pic description
\begin{figure}[h]
\subfigure[pic1 name]{
<<echo = F, eval = T, message=F, fig=TRUE>>=
# a function returning a ggplot2 object (with a proper parameters instead of "...")
plot.matrix.from.file(...)
@
% below there is a fragment of the code I used before (which includes a graphics directly from a .pdf file)
%\includegraphics[scale=0.4]{data/simulated.data/obs_pred_mean_Gini_r.pdf}
\label{pic1 label}
}
\subfigure[pic2 name]{
<<echo = F, eval = T, message=F>>=
# a function returning a ggplot2 object (with a proper parameters instead of "...")
plot.matrix.from.file(...)
@
% below there is a fragment of the code I used before (which includes a graphics directly from a .pdf file)
%\includegraphics[scale=0.4]{data/simulated.data/obs_pred_var_Gini_r.pdf}
\label{pic2 label}
}
\caption{caption for the pair of plots}
\end{figure}
最佳答案
我在使用 subcaption
包时没有发现任何问题。请参阅example 104 .
\documentclass{article}
\usepackage{subcaption}
\begin{document}
You can include sub-figures using the \textbf{subcaption} package. For example,
Figure \ref{fig:test} contains \ref{fig:test-a} and \ref{fig:test-b}.
\begin{figure}
\begin{subfigure}{.5\textwidth}
<<test-a, echo=FALSE, results='asis', fig.width=5, fig.height=5>>=
plot(1:10)
@
\caption{This is Figure a. \label{fig:test-a}}
\end{subfigure}
\begin{subfigure}{.5\textwidth}
<<test-b, echo=FALSE, results='asis', fig.width=5, fig.height=5>>=
plot(rnorm(100))
@
\caption{This is Figure b. \label{fig:test-b}}
\end{subfigure}
\caption{This figure contains two subfigures. \label{fig:test}}
\end{figure}
\end{document}
输出符合预期:
关于R knit - 直接在定义的位置包含图形(在 R 代码中渲染的 ggplot2 对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724254/