How can I save plotly graphs in high quality?

我的梦境 提交于 2020-08-04 05:53:13

问题


Every graph that I make in plotly looks great up until the moment I save it, so the image looks kind of matte, if it makes sense, just really bad quality. Does anyone know how to save it in high quality? You can use this basic chart as an example.

library(plotly)

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')

Thank you!


回答1:


At least in Plotly v4.7.1+ you can export the graph as SVG (since being a vector graphic format this is pretty much highest quality possible).

To do so you need to have the package Rselenium installed. Then you can use Plotly's export function as follows (in this example using Chrome as Selenium driver):

if ( !require(RSelenium) ) {
  install.packages("RSelenium", repos = "https://cloud.r-project.org/")
}

p %>%
  export(file = "filename.svg",
         selenium = RSelenium::rsDriver(browser = "chrome"))

This will download the chart as filename.svg to your default download directory (~/Downloads on many Linux distros or %USERPROFILE%\Downloads on Windows). If you want to download the SVG to the current R working directory instead, you need to pass several options to the Selenium driver (Chrome in this example):

p %>%
  export(file = "filename.svg",
         selenium = RSelenium::rsDriver(browser = "chrome",
                                        extraCapabilities = list(
                                          chromeOptions = 
                                            list(prefs = list(
                                              "profile.default_content_settings.popups" = 0L,
                                              "download.prompt_for_download" = FALSE,
                                              "download.default_directory" = getwd())
                                            ))))

A more sophisticated custom export function which allows to set the width and height of the exported SVG (and much more incl. optional conversion to PDF and PNG) can be found in this Gist: https://gist.github.com/salim-b/32c4370cee4ac0a3fbfef13a9ce98458




回答2:


If, like me, you're trying to use Plotly in Offline mode, I recommend the following procedure:

First, save as SVG:

fname = "/home/me/output"
plot(fig, filename=fname, image='svg', image_filename=fname)

Then open the HTML (named output.html) in your browser, and it will prompt you to save the SVG (this is a frustrating step, but there's currently no way around it with Plotly).

Then, use cairosvg to convert the SVG to PNG using better scaling. See my other answer here. Can I specify scaling when using the cairosvg module inside of python




回答3:


The answer provided by @Peter Gaultney produced an error for my box plot.

These two methods might work in more circumstances and improve the image quality using htmlwidgets::onRender().

Option 1. As @chinsoon12 suggests, save as SVG. This code will open a web browser then make the browser download the image. Note that setting the viewer to null will stop the RStudio viewer pane from working, so you'll need to save it, save the plot image, and then restore it.

library(htmlwidgets)

# Save viewer settings (e.g. RStudio viewer pane)
op <- options()

# Set viewer to web browser
options(viewer = NULL)

# Use web browser to save image
p %>% htmlwidgets::onRender(
  "function(el, x) {
  var gd = document.getElementById(el.id); 
  Plotly.downloadImage(gd, {format: 'svg', width: 600, height: 800, filename: 'plot'});
  }"
 )

# Restore viewer to old setting (e.g. RStudio)
options(viewer = op$viewer)

Option 2. You can save as PNG and specify a higher resolution. You should probably increase the line thickness, fonts, etc. for this method.

library(htmlwidgets)

# Save viewer settings (e.g. RStudio viewer pane)
op <- options()

# Set viewer to web browser
options(viewer = NULL)

# Use web browser to save image
p %>% htmlwidgets::onRender(
  "function(el, x) {
  var gd = document.getElementById(el.id); 
  Plotly.downloadImage(gd, {format: 'png', width: 1200, height: 1600, filename: 'plot'});
  }"
 )

# Restore viewer to old setting (e.g. RStudio)
options(viewer = op$viewer)



回答4:


If you dive down into the plotly js library, you'll find a "scale" parameter, which is easy enough to use. The following would produce a 1200 x 1600 png.

Plotly.downloadImage(gd, {scale: 2, width: 600, height: 800})


来源:https://stackoverflow.com/questions/43355444/how-can-i-save-plotly-graphs-in-high-quality

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