Another options is to write a custom function that saves the plot to a file and 
generates the required latex code. Here is a rather simple function that 
creates jpeg files with the ggplot2-package. But you could easily modify it to 
work with other devices or other packages.

    exportJpeg <- function(p, filename, caption = NULL, label = NULL, height = 
7, width = 7, dpi = 72, pointsize = 12){
        cat("\\begin{figure}\n")
        cat("\\centering\n")
        cat("\\includegraphics[height=", hoogte, "in, width = ", breedte, 
"in]{", filename, ".jpg}\n", sep = "")
        cat("\\caption{", caption, "}\n", sep = "")
        cat("\\label{fig:", label, "}\n", sep = "")
        cat("\\end{figure}\n\n")
        ggsave(p, filename = paste(filename, ".jpg", sep = ""), device = jpeg, 
scale = 1, width = width * dpi, height = height * dpi, quality = 100, dpi = 
dpi, pointsize = pointsize)
    } 

Then your chunk would look like this (in pseudo code):

<<results = tex>>=
        create your data
        create the plot and store it in an object (e.g. p)
        exportJpeg(p, filename = "myplot")
@

Below is a more elaborate version of such function.

HTH,

Thierry

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")
}

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and 
Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology 
and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
thierry.onkel...@inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more than 
asking him to perform a post-mortem examination: he may be able to say what the 
experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure 
that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens 
Philipp Pagel
Verzonden: maandag 5 januari 2009 21:39
Aan: r-help@r-project.org
Onderwerp: Re: [R] Sweave data-figure coupling

On Mon, Jan 05, 2009 at 01:52:00PM -0600, Sebastian P. Luque wrote:
> <<binom-sim-fig, fig=TRUE, include=FALSE, height=3, echo=FALSE>>=
> layout(matrix(1:2, ncol=2)); par(mar=c(5, 4, 2, 1), cex=0.75)
> matplot(thetas, cbind(prior, lik, post), type="l", lty=c(2, 1, 1),
>         xlab="theta", ylab="probability density")
> lik <- dbinom(60, 100, thetas)
> lik.p <- prior * lik
> post <- lik.p / sum(lik.p)
> 
> matplot(thetas, cbind(prior, lik, post), type="l", lty=c(2, 1, 1),
>         xlab="theta", ylab="probability density")
> @
> 
> \includegraphics[width=\linewidth]{test-binom-sim-fig}
> 
> 
> \end{document}
> ---<--------------------cut here---------------end--------------------->---
> 
> If the embedded chunks are evaluated directly in an R session, two
> different panel data are produced by layout(), as expected.  However,
> when weaving and latexing the file produces, then the two panels show
> the same data, corresponding to the last data set.  This doesn't happen
> if the data are much simpler, say using matplot() with simple sequences
> of numbers.  Any ideas what is causing this and how to get around it?

I was very confused, too when I first had this happen to me. The
reason is that chunks with fig=TRUE are executed three times by
default: once for the eps file, the pdf and the default device.
Accordingly, whenever you modify data in such a code chunk, the
results change with each round of execution.

The solution to the problem is either 

 - separating the manipulations from the plotting in two code chunks 
 - or keeping the modified values in a separate variable instead of 
   modifying the original.

In your example you are modifying 'lik' and 'post'. Just call the
modified versions likMod and postMod or something like this and
your problem will go away.

cu
        Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://mips.gsf.de/staff/pagel

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to