2012-12-28 64 views
14

过去,我使用RStudio创建ggplot2,然后将它们从RSudio中导出为PDF。这非常有效。使用knitr和RStudio将ggplot2输出嵌入到LaTeX 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} 

回答

9

我可以给你最有方式:

\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阅读器(预览,Mac OS X)。内置的RStudio PDF查看器在过去有一些渲染问题,所以我不使用它。

我不知道如何强制在横向页面上的数字低于文本。通常情况下,我会像之前的图一样使用浮动包,但它似乎不适用于横向。我建议你在tex.stackexchange.com上咨询那些人,因为它是相当特别的LaTeX。

不是fig.width,fig.heightout.width之间的相互作用。与两者一起玩,看看图像大小与图像中元素的缩放比例会发生什么变化。一个在创建时会影响实际的图形大小,另一个则会影响LaTeX文档中包含的图像缩放比例(我认为)。

另请注意,我在图形环境中并排使用了\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) 
@