使用knitr和RStudio在LaTeX pdf中嵌入ggplot2输出

19

过去,我使用RStudio创建ggplot2图形,然后从RSudio中导出它们为PDF文件。这非常好用。

现在我正在尝试使用knitr进行自动化处理,但我无法确定在哪里设置图形的高度和宽度,以创建高质量的输出。

这是我的当前尝试,但“并排”图形不是,并且旋转的横向图形分辨率似乎很低。

我会感激任何建议。似乎ggplot2和knitr都在积极开发中,这很棒,但是互联网搜索让我很困惑。LaTeX对我来说是新的。我也会欣赏针对这些工具集的一般工作流策略。提前致谢。

\documentclass[letterpaper]{article}
\usepackage{lscape}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@ 

Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
Second page.

Side by side images:

<<third, echo = FALSE, out.width="2in", fig.cap='Side by side'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\newpage
\begin{landscape}
This page is rotated
<<fourth, echo = FALSE, out.width="4in", fig.cap='Landscape'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\end{landscape}
\end{document}
2个回答

10

我可以帮你完成大部分:

\documentclass[letterpaper]{article}
\usepackage{lscape}
\usepackage{float}
\begin{document}
<<load, echo=FALSE, results='hide', warning=FALSE, message=FALSE>>=
require(ggplot2)
@ 

Two on the first page.
<<first, echo=FALSE, fig.height=3, fig.cap="This is first", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

Blah, blah, blah.
<<second, echo=FALSE, fig.height=3, fig.cap="This is second", fig.pos='h'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

\newpage
Second page.

Side by side images:

\begin{figure}[H]
<<third, echo = FALSE, out.width="0.48\\linewidth",fig.width = 3.5,fig.height=2>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\caption{Side by side}
\end{figure}

\newpage
\begin{landscape}
This page is rotated.
<<fourth, echo = FALSE, fig.width = 4,fig.height = 3,out.width = "0.9\\linewidth">>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@
\end{landscape}
\end{document}

在我使用系统自带的PDF浏览器(预览,OS X)时,质量看起来不错。RStudio内置的PDF浏览器在过去曾出现过一些渲染问题,所以我不使用它。

我不确定如何强制使横向页面上的图位于文本下方。通常,我会像之前的图片一样使用float软件包,但似乎在横向时不起作用。建议您在这个问题上咨询tex.stackexchange.com的专家,因为这是相当LaTeX特定的。

注意fig.widthfig.heightout.width之间的相互影响。尝试调整两者,看看图像的大小与图像内元素的缩放比例发生了什么变化。其中一个影响图形创建时的实际大小,另一个则影响将其包含在LaTeX文档中时图像的缩放方式(我想是这样的)。

此外,请注意我在并排图片的figure环境中使用了\caption{},否则它将尝试为每个图创建一个标题。


6

我不确定第四页的旋转,但要得到并排的图形需要使用fig.show='hold'out.width='.45\\linewidth'

<<third, echo = FALSE, out.width="2in", fig.cap='Side by side',out.width='.45\\linewidth',fig.show='hold'>>=
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
ggplot(mtcars, aes(mpg, wt))+geom_point()+facet_grid(vs ~ am, margins=TRUE)
@

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