Show Sidebar Hide Sidebar

Static Image Export in R

How to export plotly graphs as static images in R. Plotly supports png, svg, jpg, and pdf image export.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.5.6.9000'

Export Locally

Recent versions of the R package include an export() function, which does image export locally, but requires the webshot package:

if (!require("webshot")) install.packages("webshot")
tmpFile <- tempfile(fileext = ".png")
export(plot_ly(), file = tmpFile)
browseURL(tmpFile)

Export Using Your Plotly Account

Another option is to do image export through your plotly account.

First, you will require the development version of plotly, this can be installed using devtools::install_github("ropensci/plotly"). In addition, if you haven't already, let the R package know about your credentials.

Sys.setenv("plotly_username" = "YOUR USER NAME")
Sys.setenv("plotly_api_key" = "YOUR API KEY")

This option will export the image on plotly's servers and write the content to a local file "output.png" in your working directory.

library(plotly)
p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')
plotly_IMAGE(p, format = "png", out_file = "output.png")

Appending File Type to URL

You can also view the static version of any Plotly graph by appending .png, .pdf, .eps, or .svg to the end of the URL. For example, view the static image of https://plot.ly/~chris/1638 at https://plot.ly/~chris/1638.png. See Using Plotly with rmarkdown/knitr for a way to embed these links in rmarkdown/knitr (Rmd) files.

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.