至少在 Plotly v4.7.1+ 中,您可以将图形导出为 SVG(由于是矢量图形格式,因此质量几乎是最高的)。
为此,您需要拥有该包Rselenium
安装。然后你可以使用 Plotly 的export
函数如下(在本例中使用 Chrome 作为硒驱动程序 https://seleniumhq.github.io/docs/quick.html#webdriver):
if ( !require(RSelenium) ) {
install.packages("RSelenium", repos = "https://cloud.r-project.org/")
}
p %>%
export(file = "filename.svg",
selenium = RSelenium::rsDriver(browser = "chrome"))
这会将图表下载为filename.svg
到您的默认下载目录(~/Downloads
在许多 Linux 发行版上或%USERPROFILE%\Downloads
在 Windows 上)。如果您想将 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())
))))
可以在此要点中找到更复杂的自定义导出功能,该功能允许设置导出的 SVG 的宽度和高度(以及更多功能,包括可选转换为 PDF 和 PNG):https://gist.github.com/salim-b/32c4370cee4ac0a3fbfef13a9ce98458 https://gist.github.com/salim-b/32c4370cee4ac0a3fbfef13a9ce98458