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

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

问题描述

如何使用代码将 Plotly 图表导出为 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 服务器.您可以在 plotly 包文档中找到它:

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天全站免登陆