R visualization: ggplot2, googlevis, plotly, igraph Overview

50
Data Visualization in R Olga Scrivner ggplot2 googleVis plotly igraph Data Visualization in R Olga Scrivner

Transcript of R visualization: ggplot2, googlevis, plotly, igraph Overview

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraphData Visualization in R

    Olga Scrivner

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Outline

    1. ggplot2 (Hadley Wickham)

    2. googleVis

    3. plotly

    4. igraph

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    R Package - ggplot2

    I Advantages

    I flexible

    I plot specification at a level of abstraction

    I themes

    I Things you cannot do with ggplot2

    I 3D and interactive graphics

    I network graphs (nodes/edges)

    I Some resources:

    I http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html

    I Intro http://heather.cs.ucdavis.edu/~matloff/GGPlot2/GGPlot2Intro.pdf

    http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.htmlhttp://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.htmlhttp://heather.cs.ucdavis.edu/~matloff/GGPlot2/GGPlot2Intro.pdfhttp://heather.cs.ucdavis.edu/~matloff/GGPlot2/GGPlot2Intro.pdf

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Grammar of Graphics - Independent Blocks

    a) Object initialization

    I ggplot(data, mapping=aes() )

    b) Aesthetic mapping (aes)

    I Describes how variables in the data are mapped tovisual properties (aesthetics) of geoms

    I aes(x = mpg, y = wt)

    c) Geoms - geometric objects

    I geom_bar; geom_boxplot; geom_histogram

    I Plot must have at least one geom

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Materials

    1. Download: landdata-states.csv

    2. Script week4.r

    Materials are based on tutorialhttp://tutorials.iq.harvard.edu/R/Rgraphics/

    http://tutorials.iq.harvard.edu/R/Rgraphics/

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Basic Graphics - Histogram

    hist(mydata$Home.Value)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Ggplot

    1st Block: Object initialization

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Ggplot

    1st Block: Object initialization

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    AES(x,y)

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    AES(x,y)

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Points: Shape, Size, Color

    Shape: 25 symbolsa data frame with three columns:df

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Points: Shape, Size, Color

    Shape: 25 symbolsa data frame with three columns:df

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Points: Shape, Size, Color

    Shape: 25 symbolsa data frame with three columns:df

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Lines: geom_line

    0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash,5 = longdash, 6 = twodash

    df2

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Lines: geom_line

    0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash,5 = longdash, 6 = twodash

    df2

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Lines: geom_line

    0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash,5 = longdash, 6 = twodash

    df2

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Lines: geom_line

    I Change line type

    I Add size

    I Add color

    I f + geom_line(linetype = 3, size = 4, color = red)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Lines: geom_line

    I Change line type

    I Add size

    I Add color

    I f + geom_line(linetype = 3, size = 4, color = red)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Smoother: geom_smooth()

    Lets select HAWAII from mydata:

    mydata[State ==HI, ]

    attach(mydata)p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Conditions

    & means AND

    | means OR

    How would you extract two states HAWAII and DC frommydata? AND or OR?

    mydata[State == HI | State == DC ,]

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Conditions

    & means AND

    | means OR

    How would you extract two states HAWAII and DC frommydata? AND or OR?

    mydata[State == HI | State == DC ,]

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Conditions

    & means AND

    | means OR

    How would you extract two states HAWAII and DC frommydata? AND or OR?

    mydata[State == HI | State == DC ,]

    p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Scale

    I One scale per aesthetic (AES)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Theme

    I Handles non-data plot elements (axis, legend,background)

    I Built-in themesI theme_grey()I theme_classic()I theme_dark()I theme_light()

    Add to your last line:

    p + geom_point() + geom_smooth() + theme_classic()

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Theme

    I Handles non-data plot elements (axis, legend,background)

    I Built-in themesI theme_grey()I theme_classic()I theme_dark()I theme_light()

    Add to your last line:

    p + geom_point() + geom_smooth() + theme_classic()

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    What is googleVis?

    I Interface between R and the Google Charts API

    I Google Charts - interactive charts that can be embeddedinto web pages

    I Hans Rosling in TED talk 2006

    I Internet connection

    I Flash player is required for some charts

    https://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen

    https://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seenhttps://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Chart Types

    1. Motion Charts gvisMotionChart()2. Annotated time lines gvisTimeline()3. Maps gvisMap()4. Other (line, bubble, area etc)5. Output is HTML code with data and references to

    JavaScript

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Google Chart Structure

    Markus Gesmann and Diego de Castillo. 2017. Introduction togoogleVis 0.6.2

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Motion Chart

    demo(WorldBank)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Enable Flash Player - Chrome

    1. In address bar chrome:plugins2. Check the status: Enable

    3. Select Always allowed to run

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Motion Chart

    head(Fruits)Motion=gvisMotionChart(Fruits,idvar="Fruit",timevar="Year")

    plot(Motion)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    User Guide

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Plotly

    1. Plotly - a collaborative business intelligence platform

    2. Plotlys R library makes interactive graphs3. Plotly uses the htmlwidget framework that works in

    various contexts (R Markdown documents, shiny apps,inside RStudio, Jupiter Notebook) without an internetconnection

    Lavf56.36.100

    collaboration.mp4Media File (video/mp4)

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Resources

    Resources:I https://cpsievert.github.io/plotly_book/I https://plot.ly/r/I https://plot.ly/ggplot2/

    https://cpsievert.github.io/plotly_book/https://plot.ly/r/https://plot.ly/ggplot2/

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Ggplot Integration

    Plotly package provides ggplotly() converting a ggplotobject to a plotly object

    http://ropensci.github.io/plotly/ggplot2/

    http://ropensci.github.io/plotly/ggplot2/

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Usage

    Click on the bar graph!

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Usage - Export

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    ggplotly()

    Lets revisit mydata

    attach(mydata)p

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Publishing

    1. Select Publish HTML

    2. Select RStudio account or RPubs

    3. Publish!

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Two Approaches

    1. ggplotly() function

    I Converting ggplot objects into plotly objects

    I Requires data frames

    2. plot_ly() interface

    I Flexible interface to plotly.js

    I Can use data frames and matrices

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    plot_ly()

    head(volcano)x

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    plot_ly()

    head(volcano)x

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Standalone Web Page

    1. Save as Web page

    2. View 3D volcano web page

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    PCA and Cluster in plotly

    PCA: principal component analysis

    We are going to build this interactive plot (credits - JiviteshPoojary)

    More resources on PCA:https://tgmstat.wordpress.com/2013/11/28/computing-and-visualizing-pca-in-r/

    https://tgmstat.wordpress.com/2013/11/28/computing-and-visualizing-pca-in-r/https://tgmstat.wordpress.com/2013/11/28/computing-and-visualizing-pca-in-r/

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    PCA

    Download R code for PCA and Cluster from Canvas

    pcaCars

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    PCA - Cluster Code

    # add cluster to data frame of scorescarsDf

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    igraph - Network

    Canvas: igraph workshop material (NetSciX 2016 School ofCode Workshop)

    I Undirected graph with three edgesI Numbers are vertex IDsI Edges are: 1 2, 2 3, 3 1

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Graphs

    library(igraph)g1

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Graphs

    library(igraph)g1

  • DataVisualization in

    R

    Olga Scrivner

    ggplot2

    googleVis

    plotly

    igraph

    Credits

    https://community.uservoice.com/blog/data-visualization-best-practices/https://opr.princeton.edu/workshops/Downloads/2015Jan_ggplot2Koffman.pdf NetSciX 2016 School of CodeWorkshop

    https://community.uservoice.com/blog/data-visualization-best-practices/https://community.uservoice.com/blog/data-visualization-best-practices/https://opr.princeton.edu/workshops/Downloads/2015Jan_ggplot2Koffman.pdfhttps://opr.princeton.edu/workshops/Downloads/2015Jan_ggplot2Koffman.pdf

    ggplot2googleVisplotlyigraph