Exporting PNG files from Plotly in R

杀马特。学长 韩版系。学妹 提交于 2019-11-26 21:20:41

问题


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)

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.


回答1:


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")



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/33959635/exporting-png-files-from-plotly-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!