Plot with ggplot2 and plotly within knitr reports

[This article was first published on Plotly, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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, 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
py <- plotly(username="r_user_guide", key="mw5isa4yqp")  # open plotly connection
gg <- ggplot(cars) + geom_point(aes(speed, dist))

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:

The link that says “Play with this data!” will open your plot on plotly’s website ( 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 their blog: Plotly. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)