【问题标题】:How can I save plotly graphs in high quality?如何以高质量保存绘图?
【发布时间】:2023-04-10 02:25:01
【问题描述】:

在我保存之前,我在 plotly 中制作的每张图看起来都很棒,所以图像看起来有点无光泽,如果有意义的话,只是质量很差。有谁知道如何以高质量保存它?您可以使用此基本图表作为示例。

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

谢谢!

【问题讨论】:

  • 你究竟是如何“保存”这些地块的?
  • @MrFlick 使用导出>另存为图像>...
  • 如果使用rstudio导出,可以指定大小。让它变大意味着更多的像素
  • 我还尝试在 plotly 中使用“下载为 png”按钮,它看起来质量不错。您实际需要什么分辨率/文件类型?
  • 您可以通过键入 ?webshot 来查看 export 选项

标签: r plotly


【解决方案1】:

至少在 Plotly v4.7.1+ 中,您可以将图形导出为 SVG(因为是矢量图形格式,这几乎是最高质量的)。

为此,您需要安装包Rselenium。然后你可以使用 Plotly 的export 函数如下(在这个例子中使用 Chrome 作为Selenium driver):

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

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

这会将图表作为filename.svg 下载到您的默认下载目录(在许多Linux 发行版上为~/Downloads,在Windows 上为%USERPROFILE%\Downloads)。如果您想将 SVG 下载到当前的 R 工作目录,则需要将几个选项传递给 Selenium 驱动程序(本例中为 Chrome):

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

可以在此 Gist 中找到更复杂的自定义导出功能,该功能允许设置导出的 SVG 的宽度和高度(以及更多包括可选转换为 PDF 和 PNG):https://gist.github.com/salim-b/32c4370cee4ac0a3fbfef13a9ce98458

【讨论】:

  • 嗨,salim,很想试试你更高级的代码。它会在一个闪亮的应用程序中工作吗?其次,它可以作为一个包安装还是我只需要复制整个代码
  • 嗨,马克,1) 到目前为止,我还没有使用闪亮的经验,所以我无法判断。如果你想在闪亮的服务器上运行代码,你至少需要确保librsvg 的开发包用于PNG/PDF 转换是可用的。我不知道 Selenium 是否会按预期工作。我会说:试试看 :) 2) 不,它不能作为一个包安装(还)。构建一个包含我所有 Plotly 辅助函数的包在我的待办事项清单上,但到目前为止我找不到时间......?
  • 这需要付费订阅吗?就像在图表工作室上一样,它适用于 pdf、eps 和 svg 等格式?
  • > 这需要付费订阅吗?没有。今天还有一个dedicated Electron app 来创建 Plotly 图的静态图像。您可以使用plotly::orca() function。如果您出于任何原因仍然喜欢使用 Rselenium,请注意,建议您也 specify the correct chromever 以避免不时遇到错误。
【解决方案2】:

@Peter Gaultney 提供的答案导致我的箱线图出现错误。

这两种方法可能会在更多情况下工作并提高图像质量using htmlwidgets::onRender().

选项 1。正如@chinsoon12 建议的那样,另存为 SVG。此代码将打开一个网络浏览器,然后让浏览器下载图像。请注意,将查看器设置为 null 将使 RStudio 查看器窗格停止工作,因此您需要保存它、保存绘图图像和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)

选项 2。您可以另存为 PNG 并指定更高的分辨率。您可能应该为此方法增加线条粗细、字体等。

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)

【讨论】:

  • 我假设选项一会在渲染时立即保存?你知道如何通过一个有多个绘图的应用程序上的 r shiny 中的按钮来做到这一点吗?
  • 对不起,我不知道 R 闪亮。我在离线模式下使用此代码进行绘图,但我不知道如何将 SVG 保存在在线应用程序中。使用指向此问题的链接提出另一个问题。
【解决方案3】:

如果您像我一样尝试在离线模式下使用 Plotly,我推荐以下步骤:

首先,另存为 SVG:

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

然后在您的浏览器中打开 HTML(名为 output.html),它会提示您保存 SVG(这是一个令人沮丧的步骤,但目前无法使用 Plotly 解决它)。

然后,使用 cairosvg 使用更好的缩放比例将 SVG 转换为 PNG。在这里查看我的其他答案。 Can I specify scaling when using the cairosvg module inside of python

【讨论】:

    【解决方案4】:

    如果你深入了解 plotly js 库,你会发现一个“scale”参数,它很容易使用。以下将生成 1200 x 1600 png。

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

    【讨论】:

    • 函数在R中不存在
    猜你喜欢
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 2013-03-17
    • 2013-04-17
    • 2021-07-24
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多