Data visualisation, interactive data analysis, statistical programming

BioInfoSummer 2015

Garth Tarr

Outline


  • Importance of visualising data
  • Interactive graphics in R
    • googleVis
    • htmlwidgets
    • ggvis
  • Shiny
  • Your turn

Motivation – Recall Anscombe's quartet

plot of chunk anscombe

An interactive future

It may not be long before presentations with static plots are as old fashioned as whipping out your overhead transparencies.

We are living in the future

Gratuitous kittens

We are living in the future

Why are our plots living in the past?

googleVis

Overview of googleVis

install.packages("googleVis")
require(googleVis)
demo(WorldBank, package = "googleVis")


  • Markus Gesmann created the googleVis package which accesses the Google Chart API directly from R

  • The functions of the package allow users to visualise data with the Google Chart Tools without uploading their data to Google

  • The output of googleVis functions is html code that contains the data and references to JavaScript functions hosted by Google


Using the Google Visualisation API with R, The R Journal, 3(2):40-44, December 2011 and googleVis package vignette

Key googleVis functions

Function Description
gvisAnnotatedTimeLine Annotated Time Line
gvisAnnotationChart Annotation Chart
gvisAreaChart Area Chart
gvisBarChart Bar Chart
gvisBubbleChart Bubble Chart
gvisCalendar Calendar Chart
gvisCandlestickChart Candlestick chart
gvisColumnChart Column Chart
gvisComboChart Combo Chart
gvisGauge Gauge
gvisGeoChart Geo Chart
gvisGeoMap Geo Map
gvisHistogram Histogram Chart
gvisIntensityMap Intensity Map
gvisLineChart Line Chart
gvisMap Maps
gvisMotionChart Motion Chart
gvisOrgChart Org Chart
gvisPieChart Pie Chart
gvisSankey Sankey Chart
gvisScatterChart Scatter Chart
gvisSteppedAreaChart Stepped Area Chart
gvisTable Table Chart
gvisTimeline Timeline Chart
gvisTreeMap Tree Map

An application of googleVis: the mplot package

Get it on GitHub

install.packages("devtools")
devtools::install_github("garthtarr/mplot")
require(mplot)

... or get it on CRAN

install.packages("mplot")
  • vis() for variable inclusion and model stability plots
  • af() for the adaptive fence
  • bglmnet() for bootstrapping glmnet
  • mplot() for an interactive Shiny interface

Tarr G, Mueller S and Welsh AH (2015). “mplot: An R package for graphical model stability and variable selection.” arXiv:1509.07583 [stat.ME].

Examples from the mplot package

require(mplot)
lm.d = lm(y ~ ., data = diabetes)
vis.d = vis(lm.d, B = 200)
plot(vis.d, which = "vip")

Examples from the mplot package

plot(vis.d, which = "boot", highlight = "ltg")

Examples from the mplot package

af.d = af(lm.d, B = 200, n.c = 100, c.max = 100)
plot(af.d)

Limitations of googleVis


  • Requires internet access to work
  • some charts require Flash
  • not very customisable

HTML widgets

JavaScript meets R

htmlwidgets is a package that lets you relatively easily link R with JavaScript.

Some examples:

  • networkD3: force directed networks, Sankey diagrams and tree networks
  • edgebundleR: hierarchical edge bundle plots
  • pairsD3: interactive scatter plot matrices
  • dygraphs: charting time-series data
  • leaflet: dynamic maps
  • DT: interactive HTML data tables
  • rpivotTable: build pivot tables to dynamically slice, dice and plot your data

What's so great about htmlwidgets?


  • most will work without internet access
  • customisable (sort of)
  • active community
  • if you're keen you can make your own... it's relatively easy to adapt any of the multitude of D3 examples

ggvis

pipes: a fundamental new operator in R

install.packages("magrittr")
require(magrittr)
awesome_data =
  raw_interesting_data %>%
  transform(somehow) %>%
  filter(the_good_parts) %>%
  finalize
require(magrittr)
x = rnorm(10)
x %>% max
## [1] 1.405986
max(x)
## [1] 1.405986
x %>% sort
##  [1] -0.97010061 -0.74849748 -0.60678533 -0.38610363 -0.03629047
##  [6]  0.01187292  0.42919359  0.65649122  0.78735912  1.40598620
sort(x)
##  [1] -0.97010061 -0.74849748 -0.60678533 -0.38610363 -0.03629047
##  [6]  0.01187292  0.42919359  0.65649122  0.78735912  1.40598620
x %>% sort %>% round(1)
##  [1] -1.0 -0.7 -0.6 -0.4  0.0  0.0  0.4  0.7  0.8  1.4
round(sort(x), 1)
##  [1] -1.0 -0.7 -0.6 -0.4  0.0  0.0  0.4  0.7  0.8  1.4
x %>% sort %>% round(1) %>% plot

plot of chunk unnamed-chunk-11

plot(round(sort(x), 1))

plot of chunk unnamed-chunk-11

Similar to JavaScript's chaining:

d3.select("body")
    .append("p")
    .text("New paragraph!");

Compund assignment %<>%

x %<>% sort %>% round(1)
x
##  [1] -1.0 -0.7 -0.6 -0.4  0.0  0.0  0.4  0.7  0.8  1.4


Read more

ggvis

  • Web based incarnation of ggplot2 (svg or canvas)
  • Enables simple interactivity (e.g. tooltips)
  • Advanced interactivity requires an R instance in the background (e.g. using sliders to change parameters)


  • Not as simple as changing ggplot() to ggvis()
  • Under development - not quite production ready yet
  • It can't facet (yet)
  • Recently on twitter @hadleywickham commented "goal is for 2016 to be the year of ggvis"


Read more: ggvis.rstudio.com

Honourable mentions

rCharts: Interactive JS Charts from R

qtlcharts: Interactive charts for QTL data

Shiny: the democratisation of R

What is Shiny?

How did I get started with Shiny?


I wanted a way to interactively explore the results of a mega-simulation study from my PhD thesis.

Graphical newtork example

Try loading it from GitHub :

install.packages("huge")
shiny::runGitHub("robnetwork", "garthtarr")


Otherwise it's also hosted here: Shinyapps.io

Why is Shiny awesome?

  • No special HTML or JavaScript knowledge required
  • Share the power of R with non R users
  • Empower your collaborators to answer their own questions

Sharing your Shiny app

Shinyapps.io

There's a button in RStudio that lets you push it to their server.

GitHub

Host your app on GitHub and then use code like this:

shiny::runGitHub('robnetwork', 'garthtarr')

Host your own RStudio/Shiny server (more complicated)


More info

Shiny adds basic interactivity to static plots


  • Interactivity with base and ggplot2 graphics (e.g. clicking and brushing)


More info

Teach yourself Shiny

Time to get your hands dirty

Session Info

sessionInfo()
## R version 3.2.2 (2015-08-14)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.10.5 (Yosemite)
## 
## locale:
## [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] d3heatmap_0.6.1      xts_0.9-7            zoo_1.7-12          
##  [4] huge_1.2.7           lattice_0.20-33      Matrix_1.2-3        
##  [7] dygraphs_0.5         igraph_1.0.1         MASS_7.3-45         
## [10] networkD3_0.2.6      BiocInstaller_1.20.1 qtlcharts_0.5-25    
## [13] magrittr_1.5         ggplot2_1.0.1        tidyr_0.3.1         
## [16] dplyr_0.4.3          readr_0.2.2          knitcitations_1.0.7 
## [19] knitr_1.11           mplot_0.7.7          edgebundleR_0.1.2   
## [22] pairsD3_0.1.0        shiny_0.12.2         ggvis_0.4.2.9000    
## [25] rCharts_0.4.5       
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.2          lubridate_1.5.0      png_0.1-7           
##  [4] assertthat_0.1       digest_0.6.8         foreach_1.4.3       
##  [7] mime_0.4             R6_2.1.1             plyr_1.8.3          
## [10] evaluate_0.8         httr_1.0.0           lazyeval_0.1.10     
## [13] curl_0.9.4           whisker_0.3-2        googleVis_0.5.10    
## [16] proto_0.3-10         labeling_0.3         devtools_1.9.1      
## [19] RefManageR_0.8.63    stringr_1.0.0        htmlwidgets_0.5     
## [22] RCurl_1.95-4.7       munsell_0.4.2        httpuv_1.3.3        
## [25] base64enc_0.1-3      htmltools_0.2.6      codetools_0.2-14    
## [28] XML_3.98-1.3         bitops_1.0-6         grid_3.2.2          
## [31] jsonlite_0.9.19      xtable_1.8-0         gtable_0.1.2        
## [34] DBI_0.3.1            formatR_1.2.1        scales_0.3.0        
## [37] bibtex_0.4.0         stringi_1.0-1        reshape2_1.4.1      
## [40] rjson_0.2.15         RColorBrewer_1.1-2   iterators_1.0.8     
## [43] tools_3.2.2          RJSONIO_1.3-0        markdown_0.7.7      
## [46] rsconnect_0.4.1.4    parallel_3.2.2       yaml_2.1.13         
## [49] slidify_0.5          colorspace_1.2-6     shinydashboard_0.5.1
## [52] poirot_0.0.2         memoise_0.2.1