从R中的Plotly导出PNG文件 [英] Exporting PNG files from Plotly in R

查看:721
本文介绍了从R中的Plotly导出PNG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用代码从R导出绘图图作为图像? (不使用图表上的导出按钮.)

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

例如,从Plotly网站上的以下代码创建此图表:

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)

如何将其另存为图像?

官方网站在python中有此材料,但我没有找到在R中类似.

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

推荐答案

有一个export功能,可让您保存图像而无需连接到plotly服务器.您可以在完整打包doc 中找到它:

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

您甚至可以通过将扩展名选择为.pngjpeg.pdf来更改输出的文件类型.

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

您还可以将图像保存在html文件中,该文件可让您通过htmlwidgets::saveWidget体验plotly的缩放或显示批注:

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

这篇关于从R中的Plotly导出PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆