2012-02-13 137 views
3

我很努力地研究R Sweave报告。我在sweave pdf输出中遇到了一些ggplot维度的问题。我的代码:R,胶乳,Sweave,ggplot2 - 更改ggplot尺寸

\documentclass{report} 

\begin{document} 

demo demo demo demo demo demo demo demo demo demo demo demo 

\begin{figure}[h] 
\begin{center} 
<<echo=FALSE, fig=TRUE>>= 
require(ggplot2) 
df <- data.frame(a= c(1:10), b = c (10:1)) 

ggplot(data = df, aes(a, b)) + geom_line() 
@ 
    \caption{caption} 
\end{center} 
\end{figure} 

demo demo demo demo demo demo demo demo demo demo demo demo 

\end{document} 

现在我想控制pdf输出中的绘图宽度和高度尺寸。实际上我想保持高度相同,但使宽度与文本宽度相同。

谢谢你的时间。

+0

或许这些人会帮助? http://stackoverflow.com/q/3367393,http://stackoverflow.com/q/4495528,http://stackoverflow.com/q/5258901 – Aaron 2012-02-13 14:08:37

+0

谢谢,我试过了,但它不适用于我的代码。我现在开始认为我的乳胶安装中有某种错误。 – jeroen81 2012-02-13 14:13:20

+0

什么不起作用?有关改进问题的其他建议,请参阅http://stackoverflow.com/questions/how-to-ask。 – Aaron 2012-02-13 17:38:51

回答

1

看一看从AFLP包ggsave.latex()函数,它是可在R-Forge的

install.packages("AFLP", repos="http://R-Forge.R-project.org") 

那么你的Sweave文件,简化了这一

\documentclass{report} 

\begin{document} 

demo demo demo demo demo demo demo demo demo demo demo demo 

<<echo=FALSE, results = tex>>= 
require(ggplot2) 
ggsave.latex <- AFLP:::ggsave.latex 
df <- data.frame(a= c(1:10), b = c (10:1)) 
p <- ggplot(data = df, aes(a, b)) + geom_line() 
ggsave.latex(p, filename = "myplot.pdf", width = 2, height = 10, caption = "Your caption") 
@ 

demo demo demo demo demo demo demo demo demo demo demo demo 

\end{document} 
+0

这就是Sweave用户通常所做的事情:创建自己的函数或使用其他技巧将类似'\ includegraphics [width = ...] {}'的LaTeX代码写入带有块选项'results = tex'的文档中。如果这个块有其他类型的输出呢?例如'打印(LM)'。 Sweave真的应该添加选项来控制输出中的图的尺寸(我等待并等待,最后自己添加:https://github.com/yihui/knitr#readme) – 2012-02-13 19:52:38

+0

Knitr看起来不错。对于我的下一个项目,我一定会考虑使用knitr。 – jeroen81 2012-03-01 10:12:39

6

你可以试试使用knitr包。 (完全披露:我是此软件包的代码库的一个小贡献者),它允许您指定out.width,它控制着图的宽度。因此,您可以将您的代码块重写为

<<echo=FALSE, out.width = '0.9\\textwidth'>>= 
suppressMessages(require(ggplot2)) 
df <- data.frame(a= c(1:10), b = c (10:1)) 
ggplot(data = df, aes(a, b)) + geom_line() 
@ 
+1

这不能控制身高,对吧?使用Sweave,我们可以使用'\ setkeys {Gin} {width = 0.9 \ textwidth}',但是这当然适用于所有的数字。 – chl 2012-02-14 16:17:00

+0

您可以使用'\ SweaveOpts {out.width = 0.9 \ textwidth}'为所有数字设置全局。 – Ramnath 2012-02-14 19:37:12

4

非常感谢Ramnath。 FWIW,我贴在这里的完整代码:

\documentclass{report} 

\begin{document} 

demo demo demo demo demo demo demo demo demo demo demo demo 

\begin{figure}[h] 
<<echo=FALSE, fig.align='center', out.width='0.9\\textwidth', message=FALSE>>= 
require(ggplot2) 
df <- data.frame(a= c(1:10), b = c (10:1)) 
ggplot(data = df, aes(a, b)) + geom_line() 
@ 
    \caption{caption} 
\end{figure} 

demo demo demo demo demo demo demo demo demo demo demo demo 

\end{document} 

我添加的选项fig.align='center'居中图(你不需要使用乳液,有时可能集中排列在文档中意外的东西\begin{center}\end{center}),和message=FALSE来抑制来自require(ggplot2)的消息。

您可以通过编译文件:

library(knitr) 
knit('your_document.Rnw') 
+0

您也可以使用'knit2pdf'直接编译为'pdf'。 – Ramnath 2012-02-13 19:50:20

+0

或者使用Rstudio,这也支持一次编译成pdf ...... – 2012-02-13 20:09:54