Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, Latex, Sweave, ggplot2 - change ggplot dimensions

Cuurently I am working on a R Sweave report. And I am having some difficulties with the ggplot dimensions in the sweave pdf output. My code:

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

Now I want to control the plot width and height dimensions in the pdf output. actually I want to keep the height the same but make the width as the same as the text width.

Thanks for your time.

like image 823
jeroen81 Avatar asked Dec 10 '22 02:12

jeroen81


1 Answers

You could try using the knitr package. (Full Disclosure: I am a minor contributor to the codebase of this package), which allows you to specify out.width, which controls the width of the figure. So, you could rewrite your code chunk as

<<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()
@
like image 134
Ramnath Avatar answered Dec 11 '22 16:12

Ramnath