Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...