6 June 2014

Interactive Visualization Examples (Not in R!)

Pindecode

An interactive map for Indian Pincode.

Explorable Explanation

An audio processing example which allows for exploration in parametric, symbolic, diagramatic and visual ways.

Devices

The smartphone, tablets and PCs installed base as interactive barchart and mosaic charts.

What is Interaction?

Conceptual Model <—> Real World

Reduce the Gulf of Execution & Gulf of Evaluation between these two.

Gulf of Execution

The difference between the user’s intentions and the allowable actions.

Gulf of Evaluation

The amount of effort that the person must exert to interpret the state of the system and to determine how well the expectations and intentions have been met.

Reference: The Design of Everyday Things - Don Norman

Interaction Techniques for EDA

  • Navigation: Pan, Zoom, Scale, Rotate
  • Selection / Annotation: View specification e.g. map data to visual variables
  • Filtering: Highlighting, Brushing & Linking e.g. selecting a subset of data
  • Sorting: Conditional Panels / Facets
  • Dynamic Queries: Extract Data

Explore binwidth interactively?

library(ggplot2)
ggplot(diamonds, aes(price)) + geom_histogram(binwidth = 500)

plot of chunk binwidth

Find the outliers?

ggplot(diamonds, aes(carat, price)) +
  geom_point()

plot of chunk outlier

Static vs. Interactive Visualization in R

Static Graphics

  • Strong inbuilt plot package - graphics
  • Mature package options - grid, lattice, ggplot2
  • Can all be run in basic R
  • Easily installed through CRAN

Interactive Graphics

  • Limited (nearly no!) inbuilt support
  • Packages in varying (early) stages of development
  • Require integration with additional GUI
  • Not all easy installed

Interactive Graphics Options in R

Within R - identify()

Use pointing to identify points in a scatter plot and put label them.

# Identify point in a scatterplot 
attach(mtcars)
plot(mpg, wt) 
identify(x = mpg, y = wt, n = 3, label = row.names(mtcars))

Within R - locator()

Used to read the position of the graphics cursor (when the mouse pressed). Those positions will be then plotted as points or joined by lines.

# Locate points on a scatterplot 
attach(mtcars)
plot(mpg, wt) 

# draw up to 3 points
locator(n = 3, type="p") 

# draw up to 4 points joined by lines
locator(n = 4, type="l")

manipulate in RStudio

Takes a plotting expression and a set of controls (e.g. slider, picker, checkbox, or button) to dynamically change values within the expression.

When a value is changed using its corresponding control the expression is automatically re-executed and the plot is redrawn.

library(ggplot2)
library(manipulate)
manipulate(ggplot(diamonds, aes(price)) + 
        geom_histogram(binwidth = bin), 
        bin=slider(40, 500))

Mondrian

A general purpose statistical data-visualization system written in JAVA. Available as R package called "iplots", though needs Java Gui for R (JGR) instead of RStudio for full functionality.

  • Works with tsv and csv files and loads data from R workspace.

  • Excellent interactive visualization techniques for data of almost any kind, including Categorical Data, Geographical Data and LARGE Data.

  • Fully linked plots, and offer many interactions and queries. Any case selected in a plot in Mondrian is highlighted in all other plots.

  • Currently implemented plots comprise Histograms, Boxplots y by x, Scatterplots, Barcharts, Mosaicplots, Missing Value Plots, Parallel Coordinates/Boxplots, SPLOMs and Maps.

Mondrian

Save the diamonds dataframe

library(ggplot2)
data(diamonds)
str(diamonds)
save.image(file="diamonds.RData")

Download and install Mondrian from http://rosuda.org/mondrian/

Load the diamonds.RData file and start exploring

Shiny

Builds interactive web applications from R.

Each Shiny App has two components:

  • User Interface Script (ui.R): Controls the layout and apperance of the script
  • Server Script (server.R): Containes the reactive control logic to recreate the output, when the input variables are changed
install.packages('shiny')
library(shiny)
runExample("01_hello")
runExample("05_sliders")

Any one with the files ui.R and server.R can run the visualization inside R Studio

Create your Shiny Projects - ui.R

# This is the server logic for a Shiny web application.

library(shiny)
shinyServer(function(input, output) {
  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')

  })
})

Create your Shiny Projects - server.R

# This is the user-interface definition of a Shiny web application.

library(shiny)
shinyUI(fluidPage(
  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins", "Number of bins:",
                  min = 1, max = 50, value = 30)),
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"))
  )
))

ShinyApps

You can share your shiny files and anyone can run it in RStudio.

To make it accesible to everyone without RStudio, you need a Shiny Server to host the files.

Free hosting (at the moment) at https://www.shinyapps.io/

Diamonds dataset hosted on shinyapps

Diamonds Explorer

rCharts

To create, customize and publish interactive javascript visualizations from R using a lattice style plotting interface

require(devtools)
install_github('rCharts', 'ramnathv')

library(rCharts)
hair_eye_male <- subset(as.data.frame(HairEyeColor), 
                        Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", 
            data = hair_eye_male, type = "multiBarChart")
n1$print("chart3")

Quick Start Guide

GoogleVis

Creates web pages with interactive charts based on R data frames, using the Google Chart Tools.

Display them either via the local R HTTP help server or within their own sites, without uploading the data to Google.

library(googleVis)
plot(gvisMotionChart(Fruits, "Fruit", "Year"))

## Can create the gapminder type graphs
## Caution: Takes a long time to load
demo(WorldBank)

Summary

  • Interactivity in R Graphics is rapidly developing. Keep track of developments across packages.

  • Good visualizations are task dependent. Design for the data and domain.

  • Think what interactions would be helpful to explore the data: Navigation, Selection / Annotation, Filtering, Sorting, Dynamic Queries