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

查看:62
本文介绍了使用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-输出和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}
\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 时,对于我来说,结果大致类似于此:

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

再一次,我简单地修改了我在 问题中这个问题中找到的代码>有用的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天全站免登陆