How to Use Plot.ly and ggplotly with R Markdown

I. Plot.ly + R Markdown Example

In a previous post-Google Analytics, R & Plot.ly, I shared an example of how to create a heatmap throught Plot.ly using Google Analytics data.

Recently, the Plot.ly team has released another function called 'ggplotly'. It a super-simple-yet-amazing way to render a ggplot graph (built locally in R) in Plot.ly. But before I share how to do this, I'd like to also show in more detail how to built and embedd a Plot.ly plot as an iframe within a single page using RMarkdown and Plot.ly's R API. That way, you'll be able to more fully understand the process and be ready to customize this dashboard-style template for your own use.

I. Plot.ly + R Markdown Example

First, let's set the RMarkdown options and load all the packages we need. The nitty-gritty details behind what's needed and why are beyond the scope of this post. If you have any questions or need some further help, I'd be more than happy to lend a hand in any way I can. Just leave a comment below or shoot me an email at my Contact page

library("knitr")
install.packages("devtools")
install_github("plotly", "ropensci")
library("devtools")

Once we have all the R packages we need loaded, we can then setup our plot.ly R API connection:

# Load Plot.ly
library(plotly)
p <- plotly(username = "YOUR_USERNAME", key = "YOUR_PASSWORD")
# Create Dummy Data
x <- seq.Date(from = as.Date("2014-05-01"), to = as.Date("2014-05-20"), by = "day")
x <- as.character(as.Date(x, format = "%Y%m%d"), "%Y-%m-%d %H:%M:%S")
y1 <- c(15000, 20000, 25000, 22000, 30000, 32000, 29000, 30000, 26000, 29000, 
    15000, 20000, 25000, 22000, 30000, 32000, 29000, 30000, 26000, 29000)
y2 <- c(25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 
    25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000, 25000)

# Build Plotly Traces From Dummy Data
revenue <- list(x = x, y = y1, type = "bar", name = "Revenue")
target <- list(x = x, y = y2, type = "scatter", name = "Target")

# Build Plotly Response
response <- p$plotly(revenue, target, kwargs = list(filename = "Fun/Revenue (Target v Actual)", 
    fileopt = "overwrite"))

# Execute Call to Plot.ly API
url <- response$url
filename <- response$filename

# Built Plot.ly iframe
plotly_iframe <- paste("<iframe scrolling='no' seamless='seamless' src='", url, 
    "/800/600' width='800' height='600'></iframe>", sep = "")

Paste the following below the previous r code chunk (i.e. - add it to your R Markdown document in the inline r code syntax so it's rendered as html by knitr).

Then, paste the iframe code in between a pair of backticks outside of an r code chuck so it's rendered within as html within the RMarkdown file once knitted:

`r I(plotly_iframe)`

II. R Markdown, Plot.ly + ggplotly Example

In this second example, we will use the data from above (x, y1, y2) to first create a standard plot with ggplot2 then show how easy it is to re-create it with Plot.ly.

# Build Data Frame
df <- data.frame(x, y1, y2)
df$day <- seq(from = 1, to = 20, by = 1)
colnames(df) <- c("date", "revenue", "target", "day")

# Melt Data
library(reshape2)
df$date <- NULL
df.l <- melt(df, id.vars = c("day"))
colnames(df.l) <- c("day", "segment", "revenue")

# Load ggplot2 package
library(ggplot2)

# Build Plot
plot <- ggplot(df.l, aes(day, revenue, group = segment, color = segment)) + 
    geom_line()

# Show Plot
plot

plot of chunk unnamed-chunk-6

Send ggplot plot to Plot.ly using ggplotly. It seems too easy to be true, doesn't it? ;-)

p$ggplotly(plot)

I'd like to personally thank Matt Sundquist for inspiring this post and the Plot.ly team for their hardwork in putting together a great foundation for an open source data visualization and collaboration. I'd also like to thank all those involved and supporting the underlying packages and software required to make all this possible.

Once again, I'd be more than happy to lend a hand in any way I can. Just leave a comment below or shoot me an email at my Contact page

Thanks for stopping by,
Justin

P.S. - Check back soon for another post that details the use of this format in a real world e-commerce analysis example.

Credits:

http://ropensci.org/blog/2014/04/17/plotly/

https://plot.ly/team

http://stackoverflow.com/questions/10629416/is-there-an-r-markdown-equivalent-to-sexpr-in-sweave