4 July 2014

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

Multiple Aesthetics

Showing more than two variables using aesthetics

  • Color
  • Shape
  • Size

Fuel Economy Dataset

library(ggplot2)
?mpg
head(mpg)
##   manufacturer model displ year cyl      trans drv cty hwy fl   class
## 1         audi    a4   1.8 1999   4   auto(l5)   f  18  29  p compact
## 2         audi    a4   1.8 1999   4 manual(m5)   f  21  29  p compact
## 3         audi    a4   2.0 2008   4 manual(m6)   f  20  31  p compact
## 4         audi    a4   2.0 2008   4   auto(av)   f  21  30  p compact
## 5         audi    a4   2.8 1999   6   auto(l5)   f  16  26  p compact
## 6         audi    a4   2.8 1999   6 manual(m5)   f  18  26  p compact

Fuel Economy Dataset

str(mpg)
## 'data.frame':    234 obs. of  11 variables:
##  $ manufacturer: Factor w/ 15 levels "audi","chevrolet",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ model       : Factor w/ 38 levels "4runner 4wd",..: 2 2 2 2 2 2 2 3 3 3 ...
##  $ displ       : num  1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int  1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int  4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : Factor w/ 10 levels "auto(av)","auto(l3)",..: 4 9 10 1 4 9 1 9 4 10 ...
##  $ drv         : Factor w/ 3 levels "4","f","r": 2 2 2 2 2 2 2 1 1 1 ...
##  $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : Factor w/ 5 levels "c","d","e","p",..: 4 4 4 4 4 4 4 4 4 4 ...
##  $ class       : Factor w/ 7 levels "2seater","compact",..: 2 2 2 2 2 2 2 2 2 2 ...

Scatterplot - Categorical vs. Continuous

ggplot(mpg, aes(class, hwy)) + geom_point()

plot of chunk mpg-scatter-cat

Scatterplot - Reorder

ggplot(mpg, aes(reorder(class, hwy), hwy)) + geom_point()

plot of chunk mpg-scatter-cat-reorder

Scatterplot - Reorder based on max

ggplot(mpg, aes(reorder(class, hwy, max), hwy)) + geom_point()

plot of chunk mpg-scatter-cat-reorder-max

Scatterplot - Add a smooth curve

ggplot(mpg, aes(reorder(class, hwy), hwy)) + geom_point() +
  geom_smooth(aes(group = 1), method = loess)

plot of chunk mpg-scatter-cat-smooth

Basic Scatterplot - Continuous vs Continuous

ggplot(mpg, aes(displ, hwy)) + geom_point()

plot of chunk mpg-scatter

Add variable: Color - Discrete

ggplot(mpg, aes(displ, hwy, colour = class)) + geom_point()

plot of chunk mpg-scatter-color-discrete

Add variable: Color - Continuous

ggplot(mpg, aes(displ, hwy, colour = cyl)) + geom_point()

plot of chunk mpg-scatter-color-continuous

Add variable: Size - Discrete

ggplot(mpg, aes(displ, hwy, size = drv)) + geom_point()

plot of chunk mpg-scatter-size-discrete

Add variable: Size - Continuous

ggplot(mpg, aes(displ, hwy, size = cyl)) + geom_point()

plot of chunk mpg-scatter-size-continuous

Add variable: Shape - Discrete

ggplot(mpg, aes(displ, hwy, shape = drv)) + geom_point()

plot of chunk mpg-scatter-shape-discrete

Add variable: Shape + Color

ggplot(mpg, aes(displ, hwy, color = class, shape = drv)) + geom_point()

plot of chunk mpg-scatter-shape-color

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

Facetting - Two options

facet_grid

2d grid, rows ~ cols, . for no split

facet_wrap

1d ribbon wrapped into 2d

Facetting - Wrap

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_wrap(~ cyl)

plot of chunk mpg-scatter-facet-wrap

Facetting - Grid - Row

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(. ~ cyl)

plot of chunk mpg-scatter-facet-row

Facetting - Grid - Column

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(drv ~ .)

plot of chunk mpg-scatter-facet-col

Facetting - Grid - Row & Column

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(drv ~ cyl)

plot of chunk mpg-scatter-facet-row-col

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

Using GGally

library(GGally)
mpg_select <- subset(mpg, select = c(displ, hwy, drv))

Matrix Views

ggpairs(mpg_select)

plot of chunk ggpairs

Matrix Views - Diagonal

ggpairs(mpg_select, diag = list(continuous = "bar", discrete = "bar"),
        axisLabels = 'show')

plot of chunk ggpairs-diag

Matrix Views - Upper & Lower

ggpairs(mpg_select, diag = list(continuous = "bar", discrete = "bar"),
        upper = list(continuous = "density", combo = "dot"),
        axisLabels = 'show')

plot of chunk ggpairs-upper

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

Parallel Coordinates

  • Plotting more than three variables on parallel lines
  • See the patterns
  • Order of the variables matter
  • Really effective in an interactive setup

Parallel Coordinates

ggparcoord(mpg, column = c(1:11), groupColumn = 4)

plot of chunk parcoord-iris

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

Download csv and load in Mondrian

write.csv(mpg, file = "mpg.csv", 
            quote = FALSE,
            row.names = FALSE)

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

scatterplot3d

library(scatterplot3d)
scatterplot3d(mpg$displ, mpg$cyl, mpg$hwy,
              highlight.3d = TRUE)

plot of chunk scatterplot3d

Multi Variable Visualization

  • Multiple Aesthetics
  • Facetting
  • Matrix View
  • Parallel Coordinates
  • Interaction - Linking / Brushing
  • 3d Static
  • 3d Interactive

rgl - 3d plot

library(rgl)
# plot3d(mpg$displ, mpg$hwy, mpg$cyl, type = "p", col = "red")

GGobi

  • Similiar to Mondrian but with 3D and Tours
  • Draws dotplots and scatterplots, barcharts, spineplots and histograms, parallel coordinate plots, scatterplot matrices
  • Links data points and lines between plots using persistent or transient brushing, and identification
  • Allows pan and zoom
  • Rotates data in 3D
  • Tour high-dimensional data using sequences of 1D, 2D and 2x1D projections augmented by manual control and automatic projection pursuit guidance
  • Acts as a high-dimensional drawing tool, by adding, moving, and drawing lines between points.

Contact