R Code for googleVis Demo

January 12, 2011
By

[This article was first published on Econometric Sense, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

# ——————————————————————
# | 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”)

To leave a comment for the author, please follow the link and comment on their blog: Econometric Sense.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)