在Sweave中制作并排图表

3

我使用这段代码来在Sweave中制作图形

<<label=fig1plot, include=FALSE >>=
plot(cars)
@

\begin{figure}
\begin{center}
<<label=fig1, fig=TRUE, echo=FALSE>>=
<<fig1plot>>
@
\end{center}
\caption{Some caption}
\label{fig:fig1plot}
\end{figure}


<<label=fig2plot, include=FALSE >>=
plot(table(rpois(100,5)), type = "h", col = "red", lwd=10, main="rpois(100,lambda=5)")
@

\begin{figure}
\begin{center}
<<label=fig2, fig=TRUE, echo=FALSE>>=
<<fig2plot>>
@
\end{center}
\caption{Some caption}
\label{fig:fig2plot}
\end{figure}

现在我希望能够将这两个图并排放置,并且加上像Fig 1(a)和Fig 1(b)这样的标题。非常感谢您的时间和帮助,欢迎提出任何想法、评论和指南。

4个回答

10

您可以使用LaTeX的subcaption包:

\begin{figure}
  \begin{minipage}[b]{.5\linewidth}
     \centering\large A
     % plot 1
     \subcaption{A subfigure}\label{fig:1a}
  \end{minipage}%
  \begin{minipage}[b]{.5\linewidth}
     \centering\large B
     % plot 2
     \subcaption{Another subfigure}\label{fig:1b}
  \end{minipage}
  \caption{A figure}\label{fig:1}
\end{figure}

另请参阅:如何让图片并排且每张图片都有数字标识?


1
在我的sweave文档导言部分添加\usepackage{subcaption}后,即使没有添加任何子图,我也会收到像“\caption will not be redefined since it's already redefined by a document class or package which is unknown to the caption package. The `subcaption' package does not work correctly.”这样的消息。我猜测自那个答案被接受以来可能发生了一些变化... - liori

3

我使用subfig包来实现这个功能。请查看http://ctan.org/tex-archive/macros/latex/contrib/subfig/获取该包。示例代码如下:

\begin{figure}      
  \centering        
    \subfloat[One.]{...}    
  \hspace{.25in}%       
    \subfloat[Two.]{...} \\ 
    \subfloat[Three.]{...}  
  \hspace{.25in}%       
    \subfloat[Four.]{...}   
  \caption{Simple Case.}    
\end{figure} 

3

我使用列来实现这一点,它让我将数字分开但以我想要的方式布局。

\begin{columns}
        \begin{column}{0.48\textwidth}
<<label=fig1plot, include=FALSE >>=
plot(cars)
@

\begin{figure}
\begin{center}
<<label=fig1, fig=TRUE, echo=FALSE>>=
<<fig1plot>>
@
\end{center}
\caption{Some caption}
\label{fig:fig1plot}
\end{figure}
  \end{column}
  \begin{column}{0.48\textwidth}

<<label=fig2plot, include=FALSE >>=
plot(table(rpois(100,5)), type = "h", col = "red", lwd=10, main="rpois(100,lambda=5)")
@

\begin{figure}
\begin{center}
<<label=fig2, fig=TRUE, echo=FALSE>>=
<<fig2plot>>
@
\end{center}
\caption{Some caption}
\label{fig:fig2plot}
\end{figure}
  \end{column}
\end{columns}

2
这个在 beamer 之外可用吗? - Dirk Eddelbuettel

1

我会使用par(mfrow=c(1,2))将它们放在一起作为一个图形,只需要一个\begin{figure}代码块和\end{figure}即可。下方会有一个共同的图例,但这是常见的。


谢谢您的回复。我想要单独的图表。 - MYaseen208

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接