Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save plotly graphs in high quality?

Tags:

r

plotly

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!

like image 211
LauraP Avatar asked Apr 11 '17 20:04

LauraP


People also ask

How do I save a plotly graph?

Save Your Plot Once you have your data and plot ready to go, click on SAVE on the left-hand side. Give your PLOT and DATA a filename and select the privacy setting.

How do you increase the size of a plotly graph?

Set automargin to True and Plotly will automatically increase the margin size to prevent ticklabels from being cut off or overlapping with axis titles.

Is Plotly Express a high level wrapper?

Plotly Express is a high-level wrapper for Plotly, which essentially means it does a lot of the things that you can do it Plotly with a much simpler syntax. It is pretty easy to use, and doesn't require connecting your file to Plotly or specifying that you want to work with Plotly offline.


2 Answers

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

like image 160
Salim B Avatar answered Oct 04 '22 21:10

Salim B


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)
like image 26
Kayle Sawyer Avatar answered Oct 04 '22 21:10

Kayle Sawyer