suppressMessages(library(devtools))
suppressMessages(install_github("ropensci/plotly"))
suppressMessages(library(ggplot2))
suppressMessages(library(plyr))

suppressMessages(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")

plot 

suppressMessages(py$ggplotly(plot, session="notebook"))  # send to plotly, embed in Notebook

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()

suppressMessages(py$ggplotly(session="notebook"))

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)
}

display_html( plotly_iframe("https://plot.ly/~MattSundquist/2444"))

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))

py$ggplotly(session="notebook")

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]))

quartet <- ggplot(anscombe_m, aes(x, y)) + geom_point() + facet_wrap(~set, ncol=2)

py$ggplotly(quartet, session="notebook")

display_html( plotly_iframe("https://plot.ly/~MattSundquist/2745"))  

# 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))

py$ggplotly(session="notebook")

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")))

df.m <- melt(df)

df.m <- rename(df.m, c(X1 = "Period", X2 = "Region"))

a <- ggplot(df.m, aes(x = Period, y = value/1e+06,
     fill = Region))

b <- a + geom_bar(stat = "identity", position = "stack")

py$ggplotly(b, session="notebook")

set.seed(1234)
df <- data.frame(cond = factor( rep(c("A","B"), each=200) ), 
                   rating = c(rnorm(200),rnorm(200, mean=.8)))

box <- ggplot(df, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()

py$ggplotly(box, session="notebook")

suppressMessages(devtools::install_github('talgalili/dendextend'))

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)

ggd1 <- as.ggdend(dend)
library(ggplot2)
ggplot(ggd1)

py$ggplotly(session="notebook")

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