Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 )\\
\item Load Packages
<<>>=
library(ggplot2)
library(MASS)
@
\item Load Data
<<>>=
data(iris) # or read.csv/read.table/etc.
head(iris[,3:5],n=3)
@
\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.

share|improve this question
add comment

migrated from stackoverflow.com Feb 22 '11 at 19:15

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

5 Answers

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.

share|improve this answer
add comment

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}
share|improve this answer
add comment

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")
} 
share|improve this answer
add comment

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}
share|improve this answer
add comment

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}   
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.