Exporting PNG files from Plotly in R

Murta picture Murta · Nov 27, 2015 · Viewed 20.2k times · Source

How can I export a Plotly chart as a image from R using code? (Not using the export button on the chart).

For example, this code from the Plotly site, create this chart:

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
        mode = "markers", color = carat, size = carat)

plotly chart

How can I save it as a image?

The official site has this material in python, but I didn't find something similar in R.

Answer

JaKu picture JaKu · Feb 10, 2017

There is a export function which allows you to save images without the need to connect to plotly servers. You can find it in the plotly package doc:

p <- plot_ly(...)
export(p, file = "image.png")

You can even change the file type of output by selecting the extension as .png, jpeg or .pdf.

You can also save the image in html file which allows you the plotly experience like zooming or showing annotations by using htmlwidgets::saveWidget:

p <- plot_ly(...)
htmlwidgets::saveWidget(p, file = "image.html")