[R] Sweave Dynamic Graph Question

Yihui Xie xie at yihui.name
Sat Nov 20 02:43:54 CET 2010


This limitation actually comes from the LaTeX command
\includegraphics{}. It is not impossible to insert animations into
LaTeX, e.g. you can use the LaTeX package 'animate'.

The R package 'animation' has a wrapper saveLatex() which can help you
generate a PDF file containing animations, provided you have
appropriate tools installed (mainly pdflatex and Acrobat Reader). Here
is an example for your graphs:

library(animation)
saveLatex({
    library(zoo)
    library(fImport)
    IBM <- yahooSeries("IBM", from = "2000-01-01")
    IBM.Close <- IBM[, "IBM.Close"]
    rng = range(time(IBM.Close))
    Syr <- as.numeric(format(rng[1], "%Y"))
    Eyr <- as.numeric(format(rng[2], "%Y"))
    Smth <- as.numeric(format(rng[1], "%m"))
    for (yr in Syr:Eyr) {
        par(mfrow = c(4, 3))
        Temp1 <- IBM.Close[which(format(time(IBM.Close), "%Y") ==
            yr), ]
        Temp3 <- tapply(Temp1[, 1], as.yearmon(time(Temp1)),
            FUN = mean)
        for (i in Smth:length(Temp3)) {
            i <- ifelse(i < 10, paste(0, i, sep = ""), i)
            Date <- paste(i, yr, sep = "-")
            Temp2 <- IBM.Close[which(format(time(IBM.Close),
                "%m-%Y") == Date), ]
            plot(time(Temp2), Temp2, type = "l", main =
paste(factor(as.numeric(i),
                labels = month.name[as.numeric(i)]), yr, sep = "-"))
        }
    }
}, interval = 1, latex.filename = "IBM-2000-2010.tex")


I still need to modify this function a little bit to adapt to Sweave,
so that you can use the option 'results=tex' to insert animation
chunks in real-time in Sweave.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



On Fri, Nov 19, 2010 at 6:06 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 19 November 2010 at 13:13, cameron wrote:
> |
> | i have a time Series of IBM closing px from 1/1/2000 to today
> | I want to graph the time serie by dividing the graph by year and month
> | all the monthly graphs with the same year will go to one page.  so from
> | 1/1/2000 to 11/19/2010.  i will have
> | 11 pages, and each page will have 12 graphs (jan to dec) except for 2010.
> |
> | I am able to do it in R, but when i use sweave, I can only print the last
> | page.
> | any help would be greatly appreciated
>
> It's essentially an Sweave FAQ: you can only have one pdf (or ps) file per R
> 'snippet' so you have to rethink your code logic a little.
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list