使用 knitr 将图彼此相邻对齐 [英] Align plots next to each other with knitr

查看:17
本文介绍了使用 knitr 将图彼此相邻对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been been using knitr for a couple of days now, it's great! :)

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

At the moment I'm struggling to align two plots next to each other in the output file (a PDF). From my understanding this should be achieved by setting out.width='.4\linewidth' or something similar in the chunk-options.

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

The resulting plots are quite small, 2 would quite easily fit next to each other, but somehow, the get all placed beneath each other.

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

I am also having trouble to align latex-tables (xtable-output with results='asis'-option) to the left of the document. It would be great to write next to it.

推荐答案

既然你没有提供,那我就给你:

Since you haven't provided one, I will do so for you:

documentclass{article}
egin{document}

Side by side images:

egin{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 时,这对我来说大致如下:

which results in something that looks roughly like this for me when I run knitr:

注意调整 par 设置以确保一切看起来都不错.你不得不修补.

Note the fiddling with the par settings to make sure everything looks nice. You will have to tinker.

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

This minimal reproducible example was derived from the very detailed examples on the knitr website.

编辑

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

To answer your second question, even though it's more of a pure LaTeX question, here is a minimal example:

documentclass{article}
usepackage{wrapfig,lipsum}
%------------------------------------------
egin{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.
%------------------------------------------
egin{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~
ef{wrap-tab:1} is a wrapped table.
%------------------------------------------
end{document}

再一次,我只是简单地修改了我在 this 问题中找到的代码惊人地 有用的 tex.stackexchange.com 网站.

Once again, I simply adapted code I found in this question at the amazingly helpful tex.stackexchange.com site.

这篇关于使用 knitr 将图彼此相邻对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆