【问题标题】:Align plots next to each other with knitr使用 knitr 将图彼此相邻对齐
【发布时间】:2012-05-21 10:03:43
【问题描述】:

我已经使用 knitr 几天了,非常棒! :)

目前,我正在努力在输出文件(PDF)中对齐两个相邻的图。据我了解,这应该通过在块选项中设置out.width='.4\\linewidth' 或类似的东西来实现。

生成的地块非常小,2 个很容易彼此相邻,但不知何故,所有地块都放在彼此下方。

我也无法将乳胶表(xtable-output 与 results='asis'-option)对齐到文档的左侧。写在旁边就好了。

【问题讨论】:

  • 您能否提供一个简单的最小示例 .Rnw 文件来说明它不起作用?
  • 在代码块中使用 par(mfrow = c(1, 2)) 不行吗?

标签: r latex knitr


【解决方案1】:

由于您还没有提供,我会为您提供:

\documentclass{article}
\begin{document}

Side by side images:

\begin{figure}[htpb]
<<myChunk, fig.width=3, fig.height=2.5, out.width='.49\\linewidth', fig.show='hold'>>=
par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)
plot(cars)
boxplot(cars$dist,xlab='dist')
@
\end{figure}

Ta da!

\end{document}

当我运行 knitr 时,这对我来说大致如下:

注意摆弄par 设置以确保一切看起来都不错。你不得不修补。

这个最小的可重现示例来自 knitr 网站上非常详细的 examples

编辑

要回答您的第二个问题,尽管它更像是一个纯 LaTeX 问题,但这里是一个最小的示例:

\documentclass{article}
\usepackage{wrapfig,lipsum}
%------------------------------------------
\begin{document}
This is where the table goes with text wrapping around it. You may 
embed tabular environment inside wraptable environment and customize as you like.
%------------------------------------------
\begin{wraptable}{l}{5.5cm}
\caption{A wrapped table going nicely inside the text.}\label{wrap-tab:1}
<<mychunk,results = asis,echo = FALSE>>=
library(xtable)
print(xtable(head(cars)),floating = FALSE)
@
\end{wraptable} 
%------------------------------------------
\lipsum[2] 
\par
Table~\ref{wrap-tab:1} is a wrapped table.
%------------------------------------------
\end{document}

再一次,我只是修改了我在 amazingly 有用的 tex.stackexchange.com 站点上的 this 问题中找到的代码。

【讨论】:

  • 如果您有两个大小非常不同的地块会怎样?我必须将图例的图例放在它旁边,并且不知道如何将它放在那里。
  • 如何将它输入到标准的 RMarkdown 块中?
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多