如何使用代码将 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")
您甚至可以通过选择扩展名为 .png
、jpeg
或 .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")