[R] animated R plots
Yihui Xie
xie at yihui.name
Thu Dec 24 06:54:39 CET 2009
Hi,
saveSWF() (and other save*() functions) will only record the plots by
*high-level* plotting commands, and this is because most graphics
devices can only record high-level plots by default. In your animation
code, there is actually one plot generated, and the rest are produced
by low-level plotting commands like text() and points() - they will
not get recorded. So you have to draw the whole plot again by
plot(...) inside the loop. Here is a simple example:
x = runif(10)
y = runif(10)
# you can see the animation in the graphics window
# but only one image file is generated at last
plot(0:1, 0:1)
for (i in 1:10) {
points(x[i], y[i])
Sys.sleep(0.1)
}
# this is equivalent to the above animation
# but 10 image frames are generated in the end
for (i in 1:10) {
plot(0:1, 0:1)
points(x[1:i], y[1:i])
Sys.sleep(0.1)
}
Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA
On Wed, Dec 23, 2009 at 9:59 AM, Zd Gibbs <zd.gibbs at yahoo.com> wrote:
> Hi,
> I want to be able to save the following animated plot as a flash. There are ultimately 6 plots, but when I run this and save it as a flash all I get is the last (6th) plot, not all six different plots in order by "year". I’ve tested the flash commands and they work; so it has to be my function code. I am guessing that the problem has something to do with overlaying the plot points, but I don’t know how to rewrite it and keep the animated elements.
> I am running R version 2..9.2 on Windows Vista.
>
> Data looks like this:
>
> service usage amount year
> transport 11.33105 15.75016 2004
> transport 11.38398 15.82374 2005
> transport 11.44057 15.90239 2005
>
> Thanks!
>
> ZdGibbs (I can provide a subset of the data as .txt if necessary).
>
> #####order service year in ascending order and set frame options
> library(animation)
> library(lattice)
> service <- service[order(service$year), ]
> oopt = ani.options(interval = 1.0)
>
> ##### create the function
>
> Service <- function (...)
> {
>
> interval = ani.options("interval")
> index <- unique(service$year)
>
> plot(amount~usage, data=service, type="n", xlab = " ", ylab = " ", bty="l", ylim=c(0,500))
>
> legend("topright", legend=c("Transport", "Family", "Housing", "Substance Use", "Medical", "Mental Health"), pch=c(19,19,19,19,19,19), col=c("#7FC97F", "#BEAED4", "#FDC086", "#FFFF99", "#386CB0", "#F0027F"), bty="n", inset=.02)
>
> for (i in 1:length(index)) {
> rect(11.4, 425, 11.9, 500, col="white", border="red")
>
> year.service <- service[which(service$year==index[i]),]
>
> graphics <- year.service[year.service$service=="Graphics",]
>
> trans <- year.service[year.service$service=="transport",]
> fam <- year.service[year.service$service=="family",]
> hous <- year.service[year.service$service=="housing",]
> subuse <- year.service[year.service$service=="subuse",]
> med <- year.service[year.service$service=="genhealth",]
> menhlth <- year.service[year.service$service=="mental health",]
>
> text(mean(range(service$usage, na.rm=TRUE)), 455, index[i], col = rgb(0, 0, 0, 0.5), cex = 4)
> points(amount~usage, data=trans, pch=19, col="#7FC97F")
> points(amount~usage, data=fam, pch=19, col="#BEAED4")
> points(amount~usage, data=hous, pch=19, col="#FDC086")
> points(amount~usage, data=subuse, pch=19, col="#FFFF99")
> points(amount~usage, data=med, pch=19, col="#386CB0")
> points(amount~usage, data=menhlth, pch=19, col="#F0027F")
>
> Sys.sleep(interval)
>
> }
>
> }
> #####run animation
> Service()
>
> #####save as flash
> oopt = ani.options(nmax = 6, interval = 1.0)
> saveSWF(Service(), interval = 2.0, swfname="place.swf", dev = "pdf", filename="Pplot", fmt = "%03d", outdir = getwd(), swftools="C:/Program Files/SWFTools")
> ani.options(oopt)
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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