Plot with ggplot2 and plotly within knitr reports

By Plotly

(This article was first published on Plotly, and kindly contributed to R-bloggers)

Plotly is a platform for making, editing, and sharing graphs. If you are used to making plots with ggplot2, you can call ggplotly() to make your plots interactive, web-based, and collaborative. For example, see plot.ly/~marianne2/166, shown below. Notice the hover text!

The “plotly” R package lets you use plotly with R. Want to try it out? Just copy and paste this R code and you can make a web-based, interactive plot with “ggplot2”.

install.packages("devtools")  # so we can install from GitHub
devtools::install_github("ropensci/plotly")  # plotly is part of rOpenSci
 
library(plotly)
 
py <- plotly(username="r_user_guide", key="mw5isa4yqp")  # open plotly connection
 
gg <- ggplot(cars) + geom_point(aes(speed, dist))
 
py$ggplotly(gg)

Plotly lets you embed your interactive plots in iframes, web pages, and RPubs using knitr. In this post, we’ll show you three examples. There are two ways you can embed a Plotly graph in your web reports from R:

  • You can publish your code, data, and and interactive plot all in one place;
  • You can make a plot, edit it with your team in the plotly GUI, and embed it in an iframe.

“knitr” is an R package that lets you generate reports dynamically. It is very popular in the R community. If you have never heard of it before, you can check out the official website and/or this minimal tutorial. If you use RStudio, you can create a new R Markdown document directly from the “New File” menu.

Select “Document” and give it a title. Click the “Knit HTML” button above your document and… boom! You get a gorgeous-looking report, that combines text, code, and plots.

Edit your .Rmd file as you please. In order to use “plotly” functions in a given code chunk, you will need to add the plotly=TRUE parameter. To embed the plotly plot, you also want to set parameter session="knitr" in the ggplotly() call (the default behaviour is that of an interactive session, which opens a new tab/window in your web browser with the corresponding plotly graph).

Click here to view (and copy-paste) our .Rmd file.

Clicking the “Knit HTML” button in RStudio will generate your updated “knitr” report (R code that has not changed is not recomputed, it got cached). Click “Open in Browser” in order to view your report in the web browser and see the embedded Plotly plot.

If you want to make your report public, you can do so via RPubs (click “Publish”/”Republish”). Our example report is published here: http://rpubs.com/plotly/knitr_plotly_example.

The link that says “Play with this data!” will open your plot on plotly’s website (https://plot.ly/~marianne2/164) where you can invite collaborators, and style your plot with our GUI. Viewers will be able to access your data, find code for your graph in Python, MATLAB and other languages, and comment on your work.

To leave a comment for the author, please follow the link and comment on his blog: Plotly.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.