• Example of googleVis charts on Iris Dataset

    The data analysis is done with my favorite language R. What is the shortest way to share nice (interactive?) graphics on the web? Here is an example of using Google Visualization API with R package googleVis. The dataset comes from the famous iris data.

    R function gvisTable creates a table of the iris data (the first five samples).

    Once the PCA model is computed with standard R functions, the statistical results can be visualized  with Google Visualization API functions, for example, gvisColumnChart, gvisAreaChart and gvisScatterChart.

    The variance captured by principal components.

    The PCA scores.

    The PCA loadings.

    R code

    The R code to generate the last figure contains the few lines.

    library(googleVis)
    library(pls)

    data(iris)
    mod <- prcomp(iris[, 1:4])

    rot <- data.frame(rownames(mod$rotation), mod$rotation)
    chart <- gvisAreaChart(rot, options=list(title="PCA Loadings",
    width=600, height=600))

    plot(chart)

    print(chart, 'chart')

    The graphics itself is stored in the variable chart. The function plot generates the figure locally on your computer and open it in the browser.  The function print outputs JavaScript code that can be embedded into a HTML web page on your web server.

    The way googleVis API creates a scatter plot is different from R, as the first variable must be the time.  An example on Google Code Playground shows the manner to overcome this restriction of the API and to convert the first time variable to NA values in R (further NULL values in JavaScript) when it is necessary. The following custom R code does the job.

    library(googleVis)
    library(pls)

    data(iris)

    X <- iris[, 1:4]
    Y <- iris[, 5]

    mod <- prcomp(X)

    dat <- matrix(NA, nrow = nrow(X), ncol = nlevels(Y) + 1)
    for(i in 1:nlevels(Y)) {
    y <- levels(Y)[i]
    ind <- which(Y == y)
    dat[ind, c(1, i+1)] <- mod$x[ind, pc]
    }
    colnames(dat) <- c("Time", levels(Y))

    dat <- as.data.frame(dat)
    dat <- round(dat, 2)

    scatter <- gvisScatterChart(dat)

    plot(scatter)

    References

Comments are closed.

-->