Pretty R and ggplot2 plots
with Plotly and IPython

Introduction

Plotly lets you collaboratively make and share beautiful, web-based plots. You can collaborativley use R, ggplot2, and a GUI, along with and MATLAB, Python, and Excel. Here are some of your charting options:

I. World Bank plotting with ggplot2

Let's plot! You can copy and paste any of these examples into your R environment to follow along. Head over to our getting started page for more on installation.

In [1]:
local({r <- getOption("repos")  # You'll want to select the CRAN mirror you like.
       r["CRAN"] <- "http://cran.r-project.org" 
       options(repos=r)
})

Use Plotly's public sandbox (shown below), or sign-up. You can add your username and email and run this command: signup("new_username", "your_email@domain.com") to get a key. Let's make our first plot with a ggplot2 plot from the World Bank website.



We run the script, call py$ggplotly, and re-draw the ggplot2 figure in Plotly.

In [2]:
install.packages("devtools") # windows users install Rtools
library("devtools")
install_github("ropensci/plotly")  # Plotly is part of the super cool rOpenSci
 
library(plotly)  # install libraries we’ll be using
library(ggplot2)
library(reshape)
 
py <- plotly("ggplot2examples", "3gazttckd7")  # initiate plot object
 
library(WDI) # now we’ll make the plot from the blog post
library(ggplot2)
 
dat = WDI(indicator='NY.GNP.PCAP.CD', country=c('CL','HU','UY'), start=1960, end=2012) # Grab GNI per 
#  capita data for Chile, Hungary and Uruguay
 
wb <- ggplot(dat, aes(year, NY.GNP.PCAP.CD, color=country)) + geom_line() 
+     xlab('Year') + ylab('GDI per capita (Atlas Method USD)') 
+     labs(title <- "GNI Per Capita ($USD Atlas Method)")
 
py$ggplotly(wb)  # call Plotly

That produces an error, but that's ok: Plotly still graphs it and grabs the data. Grab the response from https://plot.ly/~ggplot2examples, make an iframe function, and add the URL for your graph to your iframe.

In [3]:
library(IRdisplay)
plotly_iframe <- function(url) {
    # set width and height from options or default square
    w <- "750"
    h <- "600"
    html <- paste("<center><iframe height=\"", h, "\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\"\n\t\t\t\tsrc=\"", 
        url, "\" width=\"", w, "\" frameBorder=\"0\"></iframe></center>", sep="")
    return(html)
}
In [4]:
display_html(plotly_iframe("https://plot.ly/~ggplot2examples/98"))

The data is stored with the plot. I forked the data and shared it: https://plot.ly/~MattSundquist/1343.

Below, you'll find graphs made with the same data using Plotly's Python API, matplotlib, and our web app. You can find all of these in another Notebook.



Take-away: You can edit the same graph and add data from any language or our web GUI. Collaboration and technical flexibility between languages, the GUI, and users of all types is what Plotly is all about.

Just add '.r' to the end of Plotly URLS (e.g. https://plot.ly/~MattSundquist/1339.r) and you can see the plot object in Plotly's declarative syntax for R.

Here are your other options for the plot we just made:

  • https://plot.ly/~ggplot2examples/98.py

  • https://plot.ly/~ggplot2examples/98.m

  • https://plot.ly/~ggplot2examples/98.jl

  • https://plot.ly/~ggplot2examples/98.js

  • https://plot.ly/~ggplot2examples/98.json

  • https://plot.ly/~ggplot2examples/98.svg

  • https://plot.ly/~ggplot2examples/98.png

  • https://plot.ly/~ggplot2examples/98.pdf





  • In [5]:
    display_html(plotly_iframe("https://plot.ly/~MattSundquist/1339"))
    
    In [6]:
    str(dat) # Once published, you can list all objects and their attributes
    
    'data.frame':	159 obs. of  4 variables:
     $ iso2c         : chr  "CL" "CL" "CL" "CL" ...
     $ country       : chr  "Chile" "Chile" "Chile" "Chile" ...
     $ NY.GNP.PCAP.CD: num  14290 12290 10730 9940 10030 ...
     $ year          : num  2012 2011 2010 2009 2008 ...
    
    
    In [7]:
    py <- plotly("ggplot2examples", "3gazttckd7")  # Or you can use Plotly to call any figure
    figure <- py$get_figure("MattSundquist", 1339)
    str(figure)
    
    List of 2
     $ layout:List of 25
      ..$ boxmode      : chr "overlay"
      ..$ paper_bgcolor: chr "#fff"
      ..$ height       : num 547
      ..$ titlefont    :List of 3
      .. ..$ color : chr ""
      .. ..$ family: chr ""
      .. ..$ size  : num 0
      ..$ hovermode    : chr "x"
      ..$ font         :List of 3
      .. ..$ color : chr "rgb(67, 67, 67)"
      .. ..$ family: chr "'Open sans', verdana, arial, sans-serif"
      .. ..$ size  : num 12
      ..$ autosize     : logi TRUE
      ..$ title        : chr "GNI Per Capita ($USD Atlas Method)"
      ..$ plot_bgcolor : chr "rgba(245, 247, 247, 0.7)"
      ..$ dragmode     : chr "zoom"
      ..$ smith        : logi FALSE
      ..$ width        : num 1304
      ..$ bargap       : num 0.2
      ..$ xaxis        :List of 33
      .. ..$ showexponent  : chr "all"
      .. ..$ showticklabels: logi TRUE
      .. ..$ domain        : num [1:2] 0 1
      .. ..$ gridcolor     : chr "rgb(255, 255, 255)"
      .. ..$ linecolor     : chr "#444"
      .. ..$ mirror        : logi FALSE
      .. ..$ autotick      : logi TRUE
      .. ..$ linewidth     : num 1
      .. ..$ nticks        : num 0
      .. ..$ title         : chr "year"
      .. ..$ ticks         : chr ""
      .. ..$ rangemode     : chr "normal"
      .. ..$ overlaying    : logi FALSE
      .. ..$ zeroline      : logi FALSE
      .. ..$ type          : chr "linear"
      .. ..$ autorange     : logi TRUE
      .. ..$ zerolinewidth : num 1
      .. ..$ ticklen       : num 5
      .. ..$ titlefont     :List of 3
      .. .. ..$ color : chr ""
      .. .. ..$ family: chr ""
      .. .. ..$ size  : num 0
      .. ..$ tickcolor     : chr "#444"
      .. ..$ showline      : logi FALSE
      .. ..$ showgrid      : logi TRUE
      .. ..$ tickfont      :List of 3
      .. .. ..$ color : chr ""
      .. .. ..$ family: chr ""
      .. .. ..$ size  : num 0
      .. ..$ tickwidth     : num 1
      .. ..$ tick0         : num 0
      .. ..$ tickangle     : chr "auto"
      .. ..$ gridwidth     : num 1
      .. ..$ dtick         : num 10
      .. ..$ zerolinecolor : chr "#444"
      .. ..$ range         : num [1:2] 1960 2012
      .. ..$ position      : num 0
      .. ..$ anchor        : chr "y"
      .. ..$ exponentformat: chr "B"
      ..$ bargroupgap  : num 0
      ..$ hidesources  : logi FALSE
      ..$ showlegend   : logi TRUE
      ..$ separators   : chr ".,"
      ..$ barmode      : chr "group"
      ..$ boxgap       : num 0.3
      ..$ legend       :List of 9
      .. ..$ bordercolor: chr "#444"
      .. ..$ yanchor    : chr "auto"
      .. ..$ traceorder : chr "normal"
      .. ..$ xanchor    : chr "left"
      .. ..$ bgcolor    : chr "#fff"
      .. ..$ borderwidth: num 0
      .. ..$ y          : num 0.5
      .. ..$ x          : num 1.02
      .. ..$ font       :List of 3
      .. .. ..$ color : chr ""
      .. .. ..$ family: chr ""
      .. .. ..$ size  : num 0
      ..$ yaxis        :List of 33
      .. ..$ showexponent  : chr "all"
      .. ..$ showticklabels: logi TRUE
      .. ..$ domain        : num [1:2] 0 1
      .. ..$ gridcolor     : chr "rgb(255, 255, 255)"
      .. ..$ linecolor     : chr "#444"
      .. ..$ mirror        : logi FALSE
      .. ..$ autotick      : logi TRUE
      .. ..$ linewidth     : num 1
      .. ..$ nticks        : num 0
      .. ..$ title         : chr "NY.GNP.PCAP.CD"
      .. ..$ ticks         : chr ""
      .. ..$ rangemode     : chr "normal"
      .. ..$ overlaying    : logi FALSE
      .. ..$ zeroline      : logi FALSE
      .. ..$ type          : chr "log"
      .. ..$ autorange     : logi TRUE
      .. ..$ zerolinewidth : num 1
      .. ..$ ticklen       : num 5
      .. ..$ titlefont     :List of 3
      .. .. ..$ color : chr ""
      .. .. ..$ family: chr ""
      .. .. ..$ size  : num 0
      .. ..$ tickcolor     : chr "#444"
      .. ..$ showline      : logi FALSE
      .. ..$ showgrid      : logi TRUE
      .. ..$ tickfont      :List of 3
      .. .. ..$ color : chr ""
      .. .. ..$ family: chr ""
      .. .. ..$ size  : num 0
      .. ..$ tickwidth     : num 1
      .. ..$ tick0         : num 0
      .. ..$ tickangle     : chr "auto"
      .. ..$ gridwidth     : num 1
      .. ..$ dtick         : chr "D1"
      .. ..$ zerolinecolor : chr "#444"
      .. ..$ range         : num [1:2] 2.65 4.23
      .. ..$ position      : num 0
      .. ..$ anchor        : chr "x"
      .. ..$ exponentformat: chr "B"
      ..$ annotations  :List of 1
      .. ..$ :List of 22
      .. .. ..$ yanchor    : chr "auto"
      .. .. ..$ text       : chr "Source: <a href=\"http://blogs.worldbank.org/opendata/accessing-world-bank-data-apis-python-r-ruby-stata\">World Bank</a>"
      .. .. ..$ bordercolor: chr ""
      .. .. ..$ arrowsize  : num 1
      .. .. ..$ tag        : chr ""
      .. .. ..$ borderwidth: num 1
      .. .. ..$ ay         : num -28.3
      .. .. ..$ ax         : num -10
      .. .. ..$ font       :List of 3
      .. .. .. ..$ color : chr ""
      .. .. .. ..$ family: chr ""
      .. .. .. ..$ size  : num 0
      .. .. ..$ arrowcolor : chr ""
      .. .. ..$ xref       : chr "paper"
      .. .. ..$ arrowhead  : num 1
      .. .. ..$ bgcolor    : chr "rgba(0,0,0,0)"
      .. .. ..$ borderpad  : num 1
      .. .. ..$ showarrow  : logi FALSE
      .. .. ..$ opacity    : num 1
      .. .. ..$ xanchor    : chr "auto"
      .. .. ..$ arrowwidth : num 0
      .. .. ..$ yref       : chr "paper"
      .. .. ..$ align      : chr "center"
      .. .. ..$ y          : num 0.0299
      .. .. ..$ x          : num 0.988
      ..$ boxgroupgap  : num 0.3
      ..$ margin       :List of 6
      .. ..$ b         : num 80
      .. ..$ l         : num 80
      .. ..$ r         : num 80
      .. ..$ pad       : num 0
      .. ..$ t         : num 100
      .. ..$ autoexpand: logi TRUE
     $ data  :List of 3
      ..$ :List of 12
      .. ..$ textfont: Named list()
      .. ..$ error_x : Named list()
      .. ..$ name    : chr "Chile"
      .. ..$ uid     : chr "5485f4"
      .. ..$ text    : NULL
      .. ..$ y       :List of 53
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : num 600
      .. .. ..$ : num 640
      .. .. ..$ : num 660
      .. .. ..$ : num 650
      .. .. ..$ : num 740
      .. .. ..$ : num 760
      .. .. ..$ : num 770
      .. .. ..$ : num 800
      .. .. ..$ : num 860
      .. .. ..$ : num 1020
      .. .. ..$ : num 1110
      .. .. ..$ : num 1320
      .. .. ..$ : num 1620
      .. .. ..$ : num 1120
      .. .. ..$ : num 980
      .. .. ..$ : num 1070
      .. .. ..$ : num 1320
      .. .. ..$ : num 1740
      .. .. ..$ : num 2240
      .. .. ..$ : num 2640
      .. .. ..$ : num 2190
      .. .. ..$ : num 1780
      .. .. ..$ : num 1600
      .. .. ..$ : num 1410
      .. .. ..$ : num 1410
      .. .. ..$ : num 1560
      .. .. ..$ : num 1820
      .. .. ..$ : num 2090
      .. .. ..$ : num 2240
      .. .. ..$ : num 2490
      .. .. ..$ : num 3020
      .. .. ..$ : num 3330
      .. .. ..$ : num 3610
      .. .. ..$ : num 4320
      .. .. ..$ : num 4930
      .. .. ..$ : num 5380
      .. .. ..$ : num 5250
      .. .. ..$ : num 4910
      .. .. ..$ : num 4920
      .. .. ..$ : num 4760
      .. .. ..$ : num 4550
      .. .. ..$ : num 4570
      .. .. ..$ : num 5230
      .. .. ..$ : num 6250
      .. .. ..$ : num 7260
      .. .. ..$ : num 8630
      .. .. ..$ : num 10020
      .. .. ..$ : num 9930
      .. .. ..$ : num 10720
      .. .. ..$ : num 12270
      .. .. ..$ : num 14310
      .. ..$ mode    : chr "lines"
      .. ..$ marker  :List of 1
      .. .. ..$ line: Named list()
      .. ..$ x       : num [1:53] 1960 1961 1962 1963 1964 ...
      .. ..$ line    :List of 3
      .. .. ..$ color: chr "rgb(31, 119, 180)"
      .. .. ..$ width: num 4
      .. .. ..$ shape: chr "spline"
      .. ..$ type    : chr "scatter"
      .. ..$ error_y : Named list()
      ..$ :List of 12
      .. ..$ textfont: Named list()
      .. ..$ error_x : Named list()
      .. ..$ name    : chr "Hungary"
      .. ..$ uid     : chr "cc0f76"
      .. ..$ text    : NULL
      .. ..$ y       :List of 53
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : num 540
      .. .. ..$ : num 590
      .. .. ..$ : num 670
      .. .. ..$ : num 830
      .. .. ..$ : num 1000
      .. .. ..$ : num 1150
      .. .. ..$ : num 1200
      .. .. ..$ : num 1330
      .. .. ..$ : num 1520
      .. .. ..$ : num 1770
      .. .. ..$ : num 2070
      .. .. ..$ : num 2200
      .. .. ..$ : num 2170
      .. .. ..$ : num 2010
      .. .. ..$ : num 1930
      .. .. ..$ : num 1860
      .. .. ..$ : num 2040
      .. .. ..$ : num 2400
      .. .. ..$ : num 2710
      .. .. ..$ : num 2770
      .. .. ..$ : num 2880
      .. .. ..$ : num 2740
      .. .. ..$ : num 3140
      .. .. ..$ : num 3630
      .. .. ..$ : num 4000
      .. .. ..$ : num 4220
      .. .. ..$ : num 4320
      .. .. ..$ : num 4370
      .. .. ..$ : num 4380
      .. .. ..$ : num 4460
      .. .. ..$ : num 4580
      .. .. ..$ : num 4720
      .. .. ..$ : num 5210
      .. .. ..$ : num 6550
      .. .. ..$ : num 8540
      .. .. ..$ : num 10220
      .. .. ..$ : num 11040
      .. .. ..$ : num 11510
      .. .. ..$ : num 12890
      .. .. ..$ : num 12980
      .. .. ..$ : num 12930
      .. .. ..$ : num 12900
      .. .. ..$ : num 12410
      .. ..$ mode    : chr "lines"
      .. ..$ marker  :List of 1
      .. .. ..$ line: Named list()
      .. ..$ x       : num [1:53] 1960 1961 1962 1963 1964 ...
      .. ..$ line    :List of 3
      .. .. ..$ color: chr "rgb(255, 127, 14)"
      .. .. ..$ width: num 4
      .. .. ..$ shape: chr "spline"
      .. ..$ type    : chr "scatter"
      .. ..$ error_y : Named list()
      ..$ :List of 12
      .. ..$ textfont: Named list()
      .. ..$ error_x : Named list()
      .. ..$ name    : chr "Uruguay"
      .. ..$ uid     : chr "fe96aa"
      .. ..$ text    : NULL
      .. ..$ y       :List of 53
      .. .. ..$ : NULL
      .. .. ..$ : NULL
      .. .. ..$ : num 580
      .. .. ..$ : num 610
      .. .. ..$ : num 660
      .. .. ..$ : num 680
      .. .. ..$ : num 720
      .. .. ..$ : num 640
      .. .. ..$ : num 610
      .. .. ..$ : num 670
      .. .. ..$ : num 820
      .. .. ..$ : num 850
      .. .. ..$ : num 870
      .. .. ..$ : num 1060
      .. .. ..$ : num 1370
      .. .. ..$ : num 1620
      .. .. ..$ : num 1490
      .. .. ..$ : num 1420
      .. .. ..$ : num 1630
      .. .. ..$ : num 2150
      .. .. ..$ : num 2870
      .. .. ..$ : num 3650
      .. .. ..$ : num 3290
      .. .. ..$ : num 2190
      .. .. ..$ : num 1740
      .. .. ..$ : num 1510
      .. .. ..$ : num 1780
      .. .. ..$ : num 2210
      .. .. ..$ : num 2600
      .. .. ..$ : num 2730
      .. .. ..$ : num 2840
      .. .. ..$ : num 3180
      .. .. ..$ : num 3830
      .. .. ..$ : num 4350
      .. .. ..$ : num 5040
      .. .. ..$ : num 5530
      .. .. ..$ : num 6160
      .. .. ..$ : num 6970
      .. .. ..$ : num 7240
      .. .. ..$ : num 7260
      .. .. ..$ : num 7050
      .. .. ..$ : num 6500
      .. .. ..$ : num 5140
      .. .. ..$ : num 4240
      .. .. ..$ : num 4130
      .. .. ..$ : num 4720
      .. .. ..$ : num 5380
      .. .. ..$ : num 6380
      .. .. ..$ : num 7690
      .. .. ..$ : num 8520
      .. .. ..$ : num 10110
      .. .. ..$ : num 11700
      .. .. ..$ : num 13580
      .. ..$ mode    : chr "lines"
      .. ..$ marker  :List of 1
      .. .. ..$ line: Named list()
      .. ..$ x       : num [1:53] 1960 1961 1962 1963 1964 ...
      .. ..$ line    :List of 3
      .. .. ..$ color: chr "rgb(44, 160, 44)"
      .. .. ..$ width: num 4
      .. .. ..$ shape: chr "spline"
      .. ..$ type    : chr "scatter"
      .. ..$ error_y : Named list()
    
    
    In [8]:
    figure$data[]  # or call the data so you can analyze it or bring data from any plot into your R environment 
    
    Out[8]:
    [[1]]
    [[1]]$textfont
    named list()
    
    [[1]]$error_x
    named list()
    
    [[1]]$name
    [1] "Chile"
    
    [[1]]$uid
    [1] "5485f4"
    
    [[1]]$text
    NULL
    
    [[1]]$y
    [[1]]$y[[1]]
    NULL
    
    [[1]]$y[[2]]
    NULL
    
    [[1]]$y[[3]]
    [1] 600
    
    [[1]]$y[[4]]
    [1] 640
    
    [[1]]$y[[5]]
    [1] 660
    
    [[1]]$y[[6]]
    [1] 650
    
    [[1]]$y[[7]]
    [1] 740
    
    [[1]]$y[[8]]
    [1] 760
    
    [[1]]$y[[9]]
    [1] 770
    
    [[1]]$y[[10]]
    [1] 800
    
    [[1]]$y[[11]]
    [1] 860
    
    [[1]]$y[[12]]
    [1] 1020
    
    [[1]]$y[[13]]
    [1] 1110
    
    [[1]]$y[[14]]
    [1] 1320
    
    [[1]]$y[[15]]
    [1] 1620
    
    [[1]]$y[[16]]
    [1] 1120
    
    [[1]]$y[[17]]
    [1] 980
    
    [[1]]$y[[18]]
    [1] 1070
    
    [[1]]$y[[19]]
    [1] 1320
    
    [[1]]$y[[20]]
    [1] 1740
    
    [[1]]$y[[21]]
    [1] 2240
    
    [[1]]$y[[22]]
    [1] 2640
    
    [[1]]$y[[23]]
    [1] 2190
    
    [[1]]$y[[24]]
    [1] 1780
    
    [[1]]$y[[25]]
    [1] 1600
    
    [[1]]$y[[26]]
    [1] 1410
    
    [[1]]$y[[27]]
    [1] 1410
    
    [[1]]$y[[28]]
    [1] 1560
    
    [[1]]$y[[29]]
    [1] 1820
    
    [[1]]$y[[30]]
    [1] 2090
    
    [[1]]$y[[31]]
    [1] 2240
    
    [[1]]$y[[32]]
    [1] 2490
    
    [[1]]$y[[33]]
    [1] 3020
    
    [[1]]$y[[34]]
    [1] 3330
    
    [[1]]$y[[35]]
    [1] 3610
    
    [[1]]$y[[36]]
    [1] 4320
    
    [[1]]$y[[37]]
    [1] 4930
    
    [[1]]$y[[38]]
    [1] 5380
    
    [[1]]$y[[39]]
    [1] 5250
    
    [[1]]$y[[40]]
    [1] 4910
    
    [[1]]$y[[41]]
    [1] 4920
    
    [[1]]$y[[42]]
    [1] 4760
    
    [[1]]$y[[43]]
    [1] 4550
    
    [[1]]$y[[44]]
    [1] 4570
    
    [[1]]$y[[45]]
    [1] 5230
    
    [[1]]$y[[46]]
    [1] 6250
    
    [[1]]$y[[47]]
    [1] 7260
    
    [[1]]$y[[48]]
    [1] 8630
    
    [[1]]$y[[49]]
    [1] 10020
    
    [[1]]$y[[50]]
    [1] 9930
    
    [[1]]$y[[51]]
    [1] 10720
    
    [[1]]$y[[52]]
    [1] 12270
    
    [[1]]$y[[53]]
    [1] 14310
    
    
    [[1]]$mode
    [1] "lines"
    
    [[1]]$marker
    [[1]]$marker$line
    named list()
    
    
    [[1]]$x
     [1] 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
    [16] 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    [31] 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
    [46] 2005 2006 2007 2008 2009 2010 2011 2012
    
    [[1]]$line
    [[1]]$line$color
    [1] "rgb(31, 119, 180)"
    
    [[1]]$line$width
    [1] 4
    
    [[1]]$line$shape
    [1] "spline"
    
    
    [[1]]$type
    [1] "scatter"
    
    [[1]]$error_y
    named list()
    
    
    [[2]]
    [[2]]$textfont
    named list()
    
    [[2]]$error_x
    named list()
    
    [[2]]$name
    [1] "Hungary"
    
    [[2]]$uid
    [1] "cc0f76"
    
    [[2]]$text
    NULL
    
    [[2]]$y
    [[2]]$y[[1]]
    NULL
    
    [[2]]$y[[2]]
    NULL
    
    [[2]]$y[[3]]
    NULL
    
    [[2]]$y[[4]]
    NULL
    
    [[2]]$y[[5]]
    NULL
    
    [[2]]$y[[6]]
    NULL
    
    [[2]]$y[[7]]
    NULL
    
    [[2]]$y[[8]]
    NULL
    
    [[2]]$y[[9]]
    NULL
    
    [[2]]$y[[10]]
    NULL
    
    [[2]]$y[[11]]
    [1] 540
    
    [[2]]$y[[12]]
    [1] 590
    
    [[2]]$y[[13]]
    [1] 670
    
    [[2]]$y[[14]]
    [1] 830
    
    [[2]]$y[[15]]
    [1] 1000
    
    [[2]]$y[[16]]
    [1] 1150
    
    [[2]]$y[[17]]
    [1] 1200
    
    [[2]]$y[[18]]
    [1] 1330
    
    [[2]]$y[[19]]
    [1] 1520
    
    [[2]]$y[[20]]
    [1] 1770
    
    [[2]]$y[[21]]
    [1] 2070
    
    [[2]]$y[[22]]
    [1] 2200
    
    [[2]]$y[[23]]
    [1] 2170
    
    [[2]]$y[[24]]
    [1] 2010
    
    [[2]]$y[[25]]
    [1] 1930
    
    [[2]]$y[[26]]
    [1] 1860
    
    [[2]]$y[[27]]
    [1] 2040
    
    [[2]]$y[[28]]
    [1] 2400
    
    [[2]]$y[[29]]
    [1] 2710
    
    [[2]]$y[[30]]
    [1] 2770
    
    [[2]]$y[[31]]
    [1] 2880
    
    [[2]]$y[[32]]
    [1] 2740
    
    [[2]]$y[[33]]
    [1] 3140
    
    [[2]]$y[[34]]
    [1] 3630
    
    [[2]]$y[[35]]
    [1] 4000
    
    [[2]]$y[[36]]
    [1] 4220
    
    [[2]]$y[[37]]
    [1] 4320
    
    [[2]]$y[[38]]
    [1] 4370
    
    [[2]]$y[[39]]
    [1] 4380
    
    [[2]]$y[[40]]
    [1] 4460
    
    [[2]]$y[[41]]
    [1] 4580
    
    [[2]]$y[[42]]
    [1] 4720
    
    [[2]]$y[[43]]
    [1] 5210
    
    [[2]]$y[[44]]
    [1] 6550
    
    [[2]]$y[[45]]
    [1] 8540
    
    [[2]]$y[[46]]
    [1] 10220
    
    [[2]]$y[[47]]
    [1] 11040
    
    [[2]]$y[[48]]
    [1] 11510
    
    [[2]]$y[[49]]
    [1] 12890
    
    [[2]]$y[[50]]
    [1] 12980
    
    [[2]]$y[[51]]
    [1] 12930
    
    [[2]]$y[[52]]
    [1] 12900
    
    [[2]]$y[[53]]
    [1] 12410
    
    
    [[2]]$mode
    [1] "lines"
    
    [[2]]$marker
    [[2]]$marker$line
    named list()
    
    
    [[2]]$x
     [1] 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
    [16] 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    [31] 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
    [46] 2005 2006 2007 2008 2009 2010 2011 2012
    
    [[2]]$line
    [[2]]$line$color
    [1] "rgb(255, 127, 14)"
    
    [[2]]$line$width
    [1] 4
    
    [[2]]$line$shape
    [1] "spline"
    
    
    [[2]]$type
    [1] "scatter"
    
    [[2]]$error_y
    named list()
    
    
    [[3]]
    [[3]]$textfont
    named list()
    
    [[3]]$error_x
    named list()
    
    [[3]]$name
    [1] "Uruguay"
    
    [[3]]$uid
    [1] "fe96aa"
    
    [[3]]$text
    NULL
    
    [[3]]$y
    [[3]]$y[[1]]
    NULL
    
    [[3]]$y[[2]]
    NULL
    
    [[3]]$y[[3]]
    [1] 580
    
    [[3]]$y[[4]]
    [1] 610
    
    [[3]]$y[[5]]
    [1] 660
    
    [[3]]$y[[6]]
    [1] 680
    
    [[3]]$y[[7]]
    [1] 720
    
    [[3]]$y[[8]]
    [1] 640
    
    [[3]]$y[[9]]
    [1] 610
    
    [[3]]$y[[10]]
    [1] 670
    
    [[3]]$y[[11]]
    [1] 820
    
    [[3]]$y[[12]]
    [1] 850
    
    [[3]]$y[[13]]
    [1] 870
    
    [[3]]$y[[14]]
    [1] 1060
    
    [[3]]$y[[15]]
    [1] 1370
    
    [[3]]$y[[16]]
    [1] 1620
    
    [[3]]$y[[17]]
    [1] 1490
    
    [[3]]$y[[18]]
    [1] 1420
    
    [[3]]$y[[19]]
    [1] 1630
    
    [[3]]$y[[20]]
    [1] 2150
    
    [[3]]$y[[21]]
    [1] 2870
    
    [[3]]$y[[22]]
    [1] 3650
    
    [[3]]$y[[23]]
    [1] 3290
    
    [[3]]$y[[24]]
    [1] 2190
    
    [[3]]$y[[25]]
    [1] 1740
    
    [[3]]$y[[26]]
    [1] 1510
    
    [[3]]$y[[27]]
    [1] 1780
    
    [[3]]$y[[28]]
    [1] 2210
    
    [[3]]$y[[29]]
    [1] 2600
    
    [[3]]$y[[30]]
    [1] 2730
    
    [[3]]$y[[31]]
    [1] 2840
    
    [[3]]$y[[32]]
    [1] 3180
    
    [[3]]$y[[33]]
    [1] 3830
    
    [[3]]$y[[34]]
    [1] 4350
    
    [[3]]$y[[35]]
    [1] 5040
    
    [[3]]$y[[36]]
    [1] 5530
    
    [[3]]$y[[37]]
    [1] 6160
    
    [[3]]$y[[38]]
    [1] 6970
    
    [[3]]$y[[39]]
    [1] 7240
    
    [[3]]$y[[40]]
    [1] 7260
    
    [[3]]$y[[41]]
    [1] 7050
    
    [[3]]$y[[42]]
    [1] 6500
    
    [[3]]$y[[43]]
    [1] 5140
    
    [[3]]$y[[44]]
    [1] 4240
    
    [[3]]$y[[45]]
    [1] 4130
    
    [[3]]$y[[46]]
    [1] 4720
    
    [[3]]$y[[47]]
    [1] 5380
    
    [[3]]$y[[48]]
    [1] 6380
    
    [[3]]$y[[49]]
    [1] 7690
    
    [[3]]$y[[50]]
    [1] 8520
    
    [[3]]$y[[51]]
    [1] 10110
    
    [[3]]$y[[52]]
    [1] 11700
    
    [[3]]$y[[53]]
    [1] 13580
    
    
    [[3]]$mode
    [1] "lines"
    
    [[3]]$marker
    [[3]]$marker$line
    named list()
    
    
    [[3]]$x
     [1] 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
    [16] 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    [31] 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
    [46] 2005 2006 2007 2008 2009 2010 2011 2012
    
    [[3]]$line
    [[3]]$line$color
    [1] "rgb(44, 160, 44)"
    
    [[3]]$line$width
    [1] 4
    
    [[3]]$line$shape
    [1] "spline"
    
    
    [[3]]$type
    [1] "scatter"
    
    [[3]]$error_y
    named list()
    
    
    

    "The Secret History" mentions the Iliad's notion of "light shining from a dead star." Graphs are like that: a brief flash of light and brilliance from a dead dataset. Why is the data dead? Because we'll never see or access the data used in plots, especially if it's in another language. Or the data is buried in an email, desktop folder, or file. Plotly solves that problem by saving graphs with their related data, scripts, and JSON structure.

    Keeping your data and code around has other benefits; one study in current biology found that over 90 percent of data from papers published over the past 20 years was not available. Making your graph in Plotly ensures your data, plot, revision history, and comments will always be together and accessible. Sharing one unified entity can also be useful for publication or sharing on figshare.

    II. Facets with ggplot2

    Facets in ggplot2 are awesome. Try zooming in on one of the subplots here by clicking and dragging; check out how plots with a shared axis will also zoom for you.

    In [9]:
    p <- ggplot(diamonds, aes(x=carat, y=price))
    p <- p + geom_point(aes(colour=clarity)) + facet_grid(cut ~ color, margins=TRUE)
    py$ggplotly(p)
    
    In [10]:
    display_html(plotly_iframe("https://plot.ly/~MattSundquist/1630"))  # edited in GUI
    

    III. Collaboration and flexibility with Plotly

    People often come to a visualization with data then decide on the best visualization. As Homer Adkins once quiped:

    Basic research is like shooting an arrow into the air, and where it lands, painting a target.

    Once you have your graph in Plotly you don't need to re-code your graph (though you can). Plotly detects which graph types fit with your data. Explore like a boss. Plotly gives you comprehensive, delightful collaboration online and from our APIs. You can also use Plotly for your data exploration and analysis, collaborating with users at any level to make 3D plots and more.







    IV. Bar charts in ggplot2 and Plotly

    In [11]:
    require(reshape2)
    require(plyr)
    require(spdep)
    require(igraph)
    require(ggplot2)
    require(RColorBrewer)
    
    In [12]:
    wealth.dist <- structure(
      list(
        CA = c(0.054, 0.442, 0.349, 0.155),  # 2005, family
        FR = c(0.040, 0.440, 0.278, 0.242),  # 2010, adult
        IE = c(0.050, 0.390, 0.330, 0.230),  # 2001, household
        IT = c(0.115, 0.462, 0.301, 0.122),  # 2008, household
        GB = c(0.092, 0.465, 0.318, 0.125),  # 2008, household
        US = c(0.025, 0.265, 0.372, 0.338),  # 2007, family
        BE = c(0.135, 0.373, 0.300, 0.197)), # 1994, family   
      .Names = c("CA", "FR", "IE", "IT", "GB", "US", "BE"), 
      row.names = c("Bottom 50%", "Mid 40%", "Top 9%", "Elite 1%"), 
      class = "data.frame")
    
    wealth.dist <- wealth.dist[,order(wealth.dist[4,])] # order by share 1%
    
    t(wealth.dist*100) # print table
    
    Out[12]:
       Bottom 50% Mid 40% Top 9% Elite 1%
    IT       11.5    46.2   30.1     12.2
    GB        9.2    46.5   31.8     12.5
    CA        5.4    44.2   34.9     15.5
    BE       13.5    37.3   30.0     19.7
    IE        5.0    39.0   33.0     23.0
    FR        4.0    44.0   27.8     24.2
    US        2.5    26.5   37.2     33.8
    
    In [13]:
    wealth.dist$EQ <- c(0.5, 0.4, 0.09, 0.01)  # add perfectly equal country
    
    # restack data (one rown per combination)
    d <- melt(t(wealth.dist))
    names(d) <- c("country", "group", "value")
    d$group <- factor(d$group, levels = row.names(wealth.dist))
    d$country <- factor(d$country, levels = names(wealth.dist))
    
    # plot distributions in stacked barchart
    q <- ggplot(d, aes(country, value * 100, fill = group)) + 
        geom_bar(stat = "identity")
    q <- q + scale_fill_brewer(
        palette = "RdYlBu", 
        guide = guide_legend(reverse = TRUE, title = "Groups"))
    q <- q + opts(title = "National wealth ownership")
    q + scale_x_discrete(name = "Country") + 
        scale_y_continuous(name = "Percentage of wealth owned")
    py$ggplotly()
    
    Error: Use 'theme' instead. (Defunct; last used in version 0.9.1)
    
    Out[13]:
    
    
    In [14]:
    display_html(plotly_iframe("https://plot.ly/~ggplot2examples/220"))
    

    V. Scatter plots

    Let's try a scatter, straight from the ggplot2 docs.

    In [15]:
    dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
    d <- qplot(carat, price, data=dsamp, colour=clarity)
    py$ggplotly()
    
    In [16]:
    display_html(plotly_iframe("https://plot.ly/~ggplot2examples/42"))
    
    In [17]:
    display_html(plotly_iframe("https://plot.ly/~MattSundquist/1511"))  # then hide a few series and add a pair of fits. 
    

    VI. Embedding

    Your interactive plots can be embedded in iframes in web pages, blogs, Notebooks, and RPubs (see an rOpenSci post and accompanying source for instructions on embedding). Plotly is like your combined Instagram and GitHub for plots and data.

    Embed your interactive graphs in blogs or websites with this snippet:

    <iframe src="https://plot.ly/~MattSundquist/1334/650/550" width="650" height=550" frameBorder="0" seamless="seamless" scrolling="no"></iframe>
    

    Here's how it looks in the Washington Post:



    VII. Aspirations, roadmap, and getting in touch

    CP Snow, in "The Two Cultures" argues that the divide between sciences and humanities is a vast hindrance to societal advancement and solving problems. Plotly is about solving this problem: making technical tools and plotting available and artistic. Artistry meets technical firepower. Our hope is that by making the platform flexible and encompassing of all technologies and skill-levels, we can create a collaborative space for plotting at any level. Also, pretty graphs are just downright delightful.

    You can use ggplot2 to make bar, line, and scatter charts. More to come. You can then use Plotly's other tools to make those plus bubble, polar, contour, heatmaps, histograms, boxplots, 3D graphs, and streaming graphs. Or, use Plotly and $LaTeX$. Head over to our gallery to see more examples.

    Get in touch at feedback at plot dot ly, on GitHub, or @plotlygraphs.

    VIII. IPython installation and use



    This post is based on an IPython Notebook running R. IPython is part of Project Jupyter, a language agnostic interactive development environment. If you're reading here:

    nbviewer.ipython.org/gist/msund/403910de45e282d658fa

    you're seeing how coding using an IPython Notebook looks, minus the tools. You get Markdown, a GUI, reproducibility, and a beautiful Notebook (NB) that's easy to publish by posting it on GitHub. Here's what I'm looking at right now:


    You can install the IPython NB, R kernel, matplotlib, and ggplot2, from Docker. Docker is a rather mind-blowing Linux container project: “An open source project to pack, ship and run any application as a lightweight container.” You can build an application, ship it through Docker, and someone else can easily install it and the necessary dependencies. Here’s one for the R kernel, IPython, and matplotlib:

    https://github.com/alexander-daniel/IPython-matplotlylib-Dockerfile

    And another for the R kernel:

    https://github.com/alexander-daniel/IPython-R-Kernel-Plotly-Docker

    In [19]:
    # CSS styling within IPython notebook
    display_html(getURL("https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css"))