Murta
Murta

Reputation: 2195

Exporting PNG files from Plotly in R

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.

Upvotes: 22

Views: 40303

Answers (5)

JaKu
JaKu

Reputation: 1166

There is an 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")

Upvotes: 16

frank
frank

Reputation: 3598

I wanted to warn other users, that orca changes text when saved.

from my terminal, I have:

enter image description here

I specified Corbal (Body) in my plot.

When I saved using orca(z,width = 8*96, height = 3*96, save_path), I got

enter image description here

when I saved using export(z, file = save_path ), I got:

enter image description here

You can tell that the font family is different. Very annoying

Upvotes: 0

Soumya Boral
Soumya Boral

Reputation: 1339

According to plotly the following should work :

1) Install Orca as described here : https://github.com/plotly/orca

2) You might need to restart your PC

Then run the following code :

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(...)
orca(p, file = "image.png")

Note : I found giving full path is raising a javascript error. So I just gave filename. The files are being saved in "C:/Users/user/Documents" in Windows.

Upvotes: 1

Eli Groves
Eli Groves

Reputation: 109

Since I am new, I'm unable to comment so I am posting this as a response. The export function is deprecated as of plotly v4.9.0. Instead, the orca function is the suggested method to export plotly objects as static images. Learn more here: orca function R documentation

Upvotes: 1

Murta
Murta

Reputation: 2195

In the Plotly docs in CRAN I discovered the function plotly_IMAGE.

Here is a example:

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

plotly_IMAGE(p, width = 500, height = 500, format = "png", scale = 2,
             out_file = "~/desktop/test.png")

UPDATE

plotly_IMAGE works using Plotly server. A local solution is welcome.

Upvotes: 8

Related Questions