The R Notebook:
Collaborative R Online.
Interactive ggplot2.




Domino's new R Notebook lets you write, share, deploy, anad publish R Notebooks. Like this one. Plotly lets you make beautiful, interactive ggplot2 and R plots inside and outside Notebooks. This Notebook shows how it works. To learn more, see Domino On-Premise and Plotly Enterprise.


</a>


To execute this Notebook or build your own head to the Domino repo. Sign-up for Plotly or use our sandbox account.


1. Histograms
2. 3D Plots
3. Error Bars
4. Anscombe's Quartet
5. Heatmaps
6. Barchart
7. Boxplot
8. Dendrogram
9. Embedding: Knitr, Shiny, & HTML
10. Sharing & Exporting


1. Histograms

In [1]:
suppressMessages(library(devtools))
suppressMessages(install_github("ropensci/plotly"))
suppressMessages(library(ggplot2))
suppressMessages(library(plyr))
In [2]:
suppressMessages(library(plotly)) 
In [3]:
py <- plotly(username="r_user_guide", key="mw5isa4yqp")  # open plotly connection
In [4]:
dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(100,0,30)),yy = rep(letters[1:3],each = 100))
plot <- ggplot(dat, aes(x=xx, fill=yy)) + geom_histogram(alpha=0.2, position="identity")
In [5]:
plot 
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Out[5]:

In [6]:
suppressMessages(py$ggplotly(plot, session="notebook"))  # send to plotly, embed in Notebook

You can also copy and paste this code into your R terminal to make the same plot.




install.packages("devtools")  # so we can install from github
library("devtools")
install_github("ropensci/plotly")  # plotly is part of ropensci
library(plotly)
 
py <- plotly(username="r_user_guide", key="mw5isa4yqp")  # open plotly connection
 
dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(100,0,30)),yy = rep(letters[1:3],each = 100))
plot <- ggplot(dat, aes(x=xx, fill=yy)) + geom_histogram(alpha=0.2, position="identity")
 
py$ggplotly(plot)
</div>

2. 3D Plots

In [7]:
library(reshape)
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
 
# Basic plot
v <- ggplot(volcano3d, aes(x, y, z = z))
v + geom_tile(aes(fill = z)) + stat_contour()
Attaching package: 'reshape'

The following objects are masked from 'package:plyr':

    rename, round_any

Out[7]:

In [8]:
suppressMessages(py$ggplotly(session="notebook"))

We can convert our 2D plot to a 3D plot and re-embed.




</a>


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

3. Error Bars

In [11]:
set.seed(0815)
df <- data.frame(x =1:10,
                 F =runif(10,1,2),
                 L =runif(10,0,1),
                 U =runif(10,2,3))

ggplot(df, aes(x = x, y = F)) +
  geom_point() +
  geom_errorbar(aes(ymax = U, ymin = L))
Out[11]:

In [12]:
py$ggplotly(session="notebook")

4. Anscombe's Quartet

In [13]:
anscombe_m <- data.frame()
 
for(i in 1:4)
  anscombe_m <- rbind(anscombe_m, data.frame(set=i, x=anscombe[,i], y=anscombe[,i+4]))
In [14]:
quartet <- ggplot(anscombe_m, aes(x, y)) + geom_point() + facet_wrap(~set, ncol=2)
In [15]:
py$ggplotly(quartet, session="notebook")
In [16]:
display_html( plotly_iframe("https://plot.ly/~MattSundquist/2745"))  

5. Heatmaps

In [17]:
# Generate data
pp <- function (n,r=4) {
 x <- seq(-r*pi, r*pi, len=n)
 df <- expand.grid(x=x, y=x)
 df$r <- sqrt(df$x^2 + df$y^2)
 df$z <- cos(df$r^2)*exp(-df$r/6)
 df
}
p <- ggplot(pp(20), aes(x=x,y=y))

p + geom_tile(aes(fill=z))
Out[17]:

In [18]:
py$ggplotly(session="notebook")

6. Barcharts

In [19]:
df <- structure(c(106487, 495681, 1597442,
     2452577, 2065141, 2271925, 4735484, 3555352,
     8056040, 4321887, 2463194, 347566, 621147,
     1325727, 1123492, 800368, 761550, 1359737,
     1073726, 36, 53, 141, 41538, 64759, 124160,
     69942, 74862, 323543, 247236, 112059, 16595,
     37028, 153249, 427642, 1588178, 2738157,
     2795672, 2265696, 11951, 33424, 62469,
     74720, 166607, 404044, 426967, 38972, 361888,
     1143671, 1516716, 160037, 354804, 996944,
     1716374, 1982735, 3615225, 4486806, 3037122,
     17, 54, 55, 210, 312, 358, 857, 350, 7368,
     8443, 6286, 1750, 7367, 14092, 28954, 80779,
     176893, 354939, 446792, 33333, 69911, 53144,
     29169, 18005, 11704, 13363, 18028, 46547,
     14574, 8954, 2483, 14693, 25467, 25215,
     41254, 46237, 98263, 185986), .Dim = c(19,
     5), .Dimnames = list(c("1820-30", "1831-40",
     "1841-50", "1851-60", "1861-70", "1871-80",
     "1881-90", "1891-00", "1901-10", "1911-20",
     "1921-30", "1931-40", "1941-50", "1951-60",
     "1961-70", "1971-80", "1981-90", "1991-00",
     "2001-06"), c("Europe", "Asia", "Americas",
     "Africa", "Oceania")))
In [20]:
df.m <- melt(df)
In [21]:
df.m <- rename(df.m, c(X1 = "Period", X2 = "Region"))
In [22]:
a <- ggplot(df.m, aes(x = Period, y = value/1e+06,
     fill = Region))
In [23]:
b <- a + geom_bar(stat = "identity", position = "stack")
In [24]:
py$ggplotly(b, session="notebook")

7. Boxplots

In [25]:
set.seed(1234)
df <- data.frame(cond = factor( rep(c("A","B"), each=200) ), 
                   rating = c(rnorm(200),rnorm(200, mean=.8)))
In [26]:
box <- ggplot(df, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()
In [27]:
py$ggplotly(box, session="notebook")

8. Dendrogram

In [28]:
suppressMessages(devtools::install_github('talgalili/dendextend'))
In [29]:
suppressPackageStartupMessages(library(dendextend)) 
# Create a complex dend:
dend <- iris[1:30,-5] %>% dist %>% hclust %>% as.dendrogram %>%
   set("branches_k_color", k=3) %>% set("branches_lwd", c(1.5,1,1.5)) %>%
   set("branches_lty", c(1,1,3,1,1,2)) %>%
   set("labels_colors") %>% set("labels_cex", c(.9,1.2))
# plot the dend in usual "base" plotting engine:
# plot(dend)
# Now let's do it in ggplot2 :)
ggd1 <- as.ggdend(dend)
library(ggplot2)
ggplot(ggd1)
Out[29]:

In [30]:
ggd1 <- as.ggdend(dend)
library(ggplot2)
ggplot(ggd1)
Out[30]:

In [31]:
py$ggplotly(session="notebook")

9. Embedding: Knitr, Shiny, & HTML

Plotly graphs can be placed in iframes, knitr, or Shiny:


</a>

10. Sharing & Exporting

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