Based on Interactive 2D & 3D Plots with Plotly and ggplot2.

Also see rOpenSci - Open Tools for Open Science.

Installation

Only do this if necessary.

if (!require(plotly)) {
  #install.packages("devtools")  # so we can install from github
  writeLines("Installing required package plotly.  You must have devtools installed already.")
  library("devtools")
  install_github("ropensci/plotly")  # plotly is part of ropensci
  library(plotly)
}
## Loading required package: plotly
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: RJSONIO
## Loading required package: ggplot2

Example

See http://rpubs.com/chriddyp/plotly-demo for knitr/RPubs example.
http://blog.revolutionanalytics.com/2014/09/how-to-publish-r-and-ggplot2-to-the-web.html
https://plot.ly/r/embedding-plotly-graphs-in-HTML/
http://stackoverflow.com/questions/25186022/embedding-plotly-output-in-r-markdown

Note session argument in py$ggplotly supports ‘interactive’, ‘notebook’, or ‘knitr’.
Do we set this explicitly or is it automatic?

py <- plotly(username=username, key=key)  # open plotly connection
 
# Generate data
library(reshape2) # for melt
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()

# Already created
# py$ggplotly()

Embed the created plotly plot.

Not working correctly yet.

library("knitr")
library("devtools")
url <- "https://plot.ly/~rseiter/3/y-vs-x/"
# plotly_iframe <- paste("<iframe scrolling='no' seamless='seamless' src='", url, 
#     "/800/600' width='800' height='600'></iframe>", sep = "")

plotly_iframe <- paste("<center><iframe scrolling='no' seamless='seamless' src='", url, 
                       "/650/800' width='650' height='800'></iframe></center>", sep = "")