Wednesday, January 12, 2011

R Code for googleVis Demo

# ------------------------------------------------------------------
# | PROGRAM NAME: googleVis_R
# | DATE: 1/12/11
# | CREATED BY: Matt Bogard
# | PROJECT FILE:
# |----------------------------------------------------------------
# | PURPOSE: Tutorial for creating Motion Charts in R with the GoogleVis package
# |
# |
# |
# |------------------------------------------------------------------
# | COMMENTS: See the following references for more details
# |
# | 1: http://blog.revolutionanalytics.com/2011/01/create-motion-charts-in-r-with-the-googlevis-package.html
# | 2: http://stackoverflow.com/questions/4646779/embedding-googlevis-charts-into-a-web-site/4649753#4649753
# | 3: http://cran.r-project.org/web/packages/googleVis/googleVis.pdf
# | 4: for more info on accessing the google API and data format requirements:
# | http://code.google.com/apis/visualization/documentation/gallery/motionchart.html#Data_Format
# |
# |
# |------------------------------------------------------------------
# | DATA USED: via google & iris data set
# |
# |
# |------------------------------------------------------------------
# | CONTENTS:
# |
# | PART 1: motion chart using googleVis data
# | PART 2: motion chart using your own data- this case
# | the well know iris data set (a default R data set)
# | PART 3:
# | PART 4:
# | PART 5:
# |
# |-----------------------------------------------------------------
# | UPDATES:
# |
# |
# ------------------------------------------------------------------


# *------------------------------------------------------------------*
# | set R working directory- this is where your data file will go
# | with the script for creating the visualization
# *------------------------------------------------------------------*


setwd("C:\\your directory\\R Data")


# *------------------------------------------------------------------*
# | install the googleVis package (as with any package, this only has
# | to be done forthe initial first use
# *------------------------------------------------------------------*

install.packages('googleVis')


# *------------------------------------------------------------------*
# | call the googleVis library
# *------------------------------------------------------------------*


library(googleVis)

# *-----------------------------------------------------------------*
# |
# |
# |
# | PART 1: motion chart using googleVis data
# |
# |
# |
# *------------------------------------------------------------------*


# *------------------------------------------------------------------*
# | create googelVis data object
# *------------------------------------------------------------------*


M <- gvisMotionChart(Fruits, "Fruit", "Year")


# *------------------------------------------------------------------*
# | look at data object- this includes the script that
# | will be used if you want to publish on your web page/blog
# *------------------------------------------------------------------*


print(M)

# *------------------------------------------------------------------*
# | plot the visualization-this command will open your default browser
# | and produce the visualization - this may not work depending on your
# | security and browser settings
# *------------------------------------------------------------------*

plot(M)


# *------------------------------------------------------------------*
# | create the data object that contains everything necessary to create the
# | chart on your web site/blog
# *------------------------------------------------------------------*


M$html$chart

# *------------------------------------------------------------------*
# | save the data object, which is an html file in your R
# | data directory
# *------------------------------------------------------------------*

cat(M$html$chart, file="tmp.html")

# from this point you can open the file in say, notepad++ and copy the
# script into your blog or web page and the motion chare will be functional



# *-----------------------------------------------------------------*
# |
# |
# |
# | PART 2: motion chart using your own data- this case
# | the well know iris data set (a default R data set)
# |
# |
# |
# *------------------------------------------------------------------*




# *------------------------------------------------------------------*
# | take a look at the data
# *------------------------------------------------------------------*

names(iris)
print(iris)


# simulate a time variable and add it to the data set

iris$time <- rep(1:50, 3)
names(iris)

# *------------------------------------------------------------------*
# | create googelVis data object
# *------------------------------------------------------------------*

r <- gvisMotionChart(iris, "Species", "time")

# *------------------------------------------------------------------*
# | look at data object- this includes the script that
# | will be used if you want to publish on your web page/blog
# *------------------------------------------------------------------*

names(r)
print(r)

# *------------------------------------------------------------------*
# | plot the visualization-this command will open your default browser
# | and produce the visualization - this may not work depending on your
# | security and browser settings
# *------------------------------------------------------------------*

plot(r)

# *------------------------------------------------------------------*
# | create the data object that contains everything necessary to create the
# | chart on your web site/blog
# *------------------------------------------------------------------*


r$html$chart

# *------------------------------------------------------------------*
# | save the data object, which is an html file in your R
# | data directory
# *------------------------------------------------------------------*

cat(r$html$chart, file="tmp2.html")

No comments:

Post a Comment