2012-12-28 63 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) 
@