# How to get correct dimensions for a ggplot2 plot in beamer

I have a beamer presentation made with Sweave that I'm preparing and was wanting to place some ggplot2 graphs in but the aspect ratio does not seem to come out correct. This is what is in my Rnw file.

\begin{frame}[containsverbatim]
\frametitle{Basic Workflow}\begin{itemize}
\item Start R (Start > All Programs > R > R x64 2.12.0 )\\
<<>>=
library(ggplot2)
library(MASS)
@
<<>>=
@
\item Graph Data
<<>>=
qplot(Petal.Length,Petal.Width,color=Species, data=iris)->p
@
\end{itemize}\end{frame}
\begin{frame}{Basic Workflow, Cont.}
<<fig=T,echo=F>>=
show(p)
@
\end{frame}


what comes out is a graph with the bottom cut off that takes up the left half of the page.

-

## migrated from stackoverflow.comFeb 22 '11 at 19:15

This question came from our site for professional and enthusiast programmers.

I find this a big pain. I have arrived at doing something like

% one-column
\SweaveOpts{height=5,width=8,echo=FALSE}
\setkeys{Gin}{width=4in}

% two-column
\SweaveOpts{echo=FALSE,height=5,width=5}
\setkeys{Gin}{width=2.25in}


Unfortunately, I don't know of a nice way to incorporate this into a LaTeX command; if I pack these into \onecolumnfig{} or \twocolumnfig{} LaTeX macros, Sweave doesn't see them (because they don't get expanded until after Sweave has processed the file).

Duncan Murdoch has provided various bits of useful information on this topic on the R list:

here and here, for example (results of googling "murdoch sweave setkey")

etc.

edit: the answer now, I think, is "use knitr" instead ( http://yihui.name/knitr/ ) -- it allows an out.width="0.8\\textwidth" (for example) specification in the chunk options.

-

You can set explicit dimension arguments in the << ... >> part. Alternatively, you could also use the R snippet to create a pdf file and to then include the pdf with explicitly set dimensions.

Here is what I did in the gcbd paper/vignette:

\begin{figure}[t!]
\centering
<<loglogslopes,fig=TRUE,height=4.25,width=11>>=
figure_LogLogSlopes()
@
\caption{Comparison of slopes in log/log analysis of BLAS performance.}
\label{fig:slopeloglog}
\end{figure}

-
That worked but it did not like me using \textwidth which would have been cleaner. –  Andrew Redd Nov 15 '10 at 22:45
IIRC that is a LaTeX variable which R doesn't see once the snippet has been woven (or tangled?) out for processing. –  Dirk Eddelbuettel Nov 15 '10 at 22:47
After working with this solution for several slides. I found it good to note that the images are in fact scaled and the height and width only change the aspect ratio and how much scaling there is. –  Andrew Redd Nov 17 '10 at 17:21

I wrote a function that saves the plot to a file and writes the LaTeX code. Use it in a chunk with result = tex. Another benefit of this function is that you can display multiple figures within one chunk.

ggsave.latex <- function(..., caption = NULL, label = NULL, figure.placement = "hbt", floating = TRUE, caption.placement="bottom", latex.environments="center"){
ggsave(...)
cat("\n\n")
if(floating){
cat("\\begin{figure}[", figure.placement, "]\n", sep = "")
}
cat(" \\begin{", latex.environments,"}\n", sep = "")
if(!is.null(caption) && caption.placement == "top"){
cat(" \\caption{", caption, "}\n", sep = "")
}
args <- list(...)
if(is.null(args[["filename"]])){
if(is.null(args[["path"]])){
args[["path"]] <- ""
}
if(is.null(args[["plot"]])){
names(args)[which(names(args) == "")[1]] <- "plot"
}
args[["filename"]] <- paste(args[["path"]], digest.ggplot(args[["plot"]]), ".pdf", sep="")
}
if(is.null(args[["width"]])){
if(is.null(args[["height"]])){
cat("        \\includegraphics[height = 7in, width = 7in]{", args[["filename"]], "}\n", sep = "")
} else {
cat("        \\includegraphics[height = ", args[["height"]], "in, width = 7in]{", args[["filename"]], "}\n", sep = "")
}
} else {
if(is.null(args[["height"]])){
cat("        \\includegraphics[height = 7in, width = ", args[["width"]], "in]{", args[["filename"]], "}\n", sep = "")
} else {
cat("        \\includegraphics[height = ", args[["height"]], "in, width = ", args[["width"]], "in]{", args[["filename"]], "}\n", sep = "")
}
}
if(!is.null(caption) && caption.placement == "bottom"){
cat(" \\caption{", caption, "}\n", sep = "")
}
if(!is.null(label)){
cat(" \\label{", label, "}\n", sep = "")
}
cat(" \\end{", latex.environments,"}\n", sep = "")
if(floating){
cat("\\end{figure}\n")
}
cat("\n\n")
}

-

I did something similar recently, trying to create a poster using Sweave and Beamer. I ended up putting in my graph code with echo=false and include=FALSE, then manualy adding \includegraphics commands that could give widths in terms of \columnwidth or \textwidth, knowing that sweave would create the images and call them file-block where file is the sweave filename and block is the label of the sweave block that created it. I never found a better way to do it.

\begin{block}{What can R do ?}

<<label=fig1plot,include=FALSE, echo=FALSE>>=
print(qplot(data=datatable, y=y, x=type, geom="boxplot")+ geom_boxplot() + geom_jitter())
@

<<label=fig1, fig=TRUE, echo=FALSE, include=FALSE>>=
<<fig1plot>>
@

<<label=fig2plot,include=FALSE, echo=FALSE>>=
print(qplot(data=datatable, x=x, y=y, color=type))
@

<<label=fig2, fig=TRUE, echo=FALSE, include=FALSE>>=
<<fig2plot>>
@
\centering
\begin{tabularx}{\linewidth}
\includegraphics[width=0.45\columnwidth]{whyR-fig1}
&
\includegraphics[width=0.45\columnwidth]{whyR-fig2}
\\
\end{tabularx}
\end{block}

-

By default, Sweave stretches graphics to 80% of the text width; you can turn this off globally and have the pictures always be their original size by using the nogin option:

\documentclass[nogin]{article}

-