When Wellington meets the “animation” package

The “animation” package is great for creating .gif files (of course, it also produces video and flash files thanks to Yihui Xie). By using this package, I would like to show you a nice spot in Wellington, NZ. At this spot, you can see west coast, east coast and South Island at the same time. If you are in NZ for the Rugby World Cup and can spare some time (20 mins bush walk), I highly recommend you to pay a visit.

Its address is:  -41.234164, 174.779841 on Google Map.

About these ads

About xingmowang

to be added
This entry was posted in R related. Bookmark the permalink.

4 Responses to When Wellington meets the “animation” package

  1. Yihui says:

    I’m glad you have had some fun with this package :)

    • xingmowang says:

      Hey man, it is an honor to have your comments here. Hope everything goes well at Iowa State Uni.

      BTW, though I did not attend Renmin Uni before, I used to live not far from there before, played lots of basketball there. Haha~~~~

  2. kay says:

    like it! …somewhere to view the R code?
    best,
    kay

  3. xingmowang says:

    ### The coe is a bit long.
    ### Basically, I have pictures got from Google map (files_map) and pictures of the view taken
    ### by my self.
    ### I did not use the GoogleMap package. Otherwise, you can get desired map
    ### automatically.
    ### Below is the code:

    #### 1. Load packages
    require(ReadImages)
    require(animation)
    require(jpeg)
    require(TeachingDemos)

    #### 2. Clean working space, set working directory and grad the names of pictures
    rm(list=ls())
    setwd(“xxxxxxxxx”) # set the working directory
    files <- dir()
    files <- files[grep(".jpg",files,ignore.case = T)]
    size <- dim(read.jpeg(files[1])) ## This is the size of the picture

    pos_map <- grep("map",files)
    files_map <- rev(files[pos_map])
    files_view <- files[-pos_map]
    fig_map<- readJPEG(files_map[10]) ## This is the one I used as the sub-plot

    ##### 3. Let's plot
    windows(800,600)
    saveGIF({
    ani.options(interval = 0.5)
    for(i in c(files_map,files_view,rev(files_view),rev(files_map))){
    if(grepl("map",i)){
    tmp <- readJPEG(i)
    par(mar=c(0,0,0,0))
    plot(c(1,800),c(1,600),type="n",
    bty="n",
    xaxt="n",
    yaxt="n",
    xlab="",
    ylab="")
    rasterImage(tmp,1,1,800,600)
    ani.pause()
    }else{ ### if the picture if view, put a sub-plot on it
    tmp <- readJPEG(i)
    par(mar=c(0,0,0,0))
    plot(c(1,800),c(1,600),type="n",
    bty="n",
    xaxt="n",
    yaxt="n",
    xlab="",
    ylab="")
    rasterImage(tmp,1,1,800,600)

    subp <- function(…){
    plot(c(1,800),
    c(1,600),
    type="n",
    bty="n",
    xaxt="n",
    yaxt="n",
    xlab="",
    ylab="")
    rasterImage(fig_map,1,1,800,600)
    }

    par(mar=c(0,0,0,0))
    subplot(subp(),
    x=800,
    y=600,
    vadj=1,
    hadj=1,
    size=c(3.5,3.5/(8/6)))
    ani.pause()
    }
    }
    },
    img.name = "norm_plot",
    convert="convert",
    movie.name="Wellington360.gif",
    interval = 0.5,
    ani.height = size[1],
    ani.width = size[2],
    outdir = "xxxxx/", # choose a directory you want to save the .gif
    clean=T
    )
    dev.off() ## shut down the graph device

    #### 4. Leave R
    q("yes")

    ################## Enjoy ! #####################

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s