【问题标题】:How to save a Highcharter plot as an image on local disk?如何将 Highcharter 绘图保存为本地磁盘上的图像?
【发布时间】:2020-05-10 02:57:38
【问题描述】:
hc %>% 
  hc_add_series(name = "London", data = citytemp$london, type = "area") %>% 
  hc_rm_series(name = "New York")

我想将hc 导出为 png 或 jpg。这可以通过选择导出 - 另存为图像来完成,但我想通过代码来完成,因为我有多个要导出的图。我尝试了以下方法,但它返回了一个空白图像:

png('hc.png', width = 800,height = 400)
print(hc)
dev.off()

【问题讨论】:

    标签: r r-highcharter


    【解决方案1】:

    这应该可以通过 webshot 包实现(请参阅此处的问题:https://github.com/jbkunst/highcharter/issues/186

    library(webshot)
    library(highcharter)
    library(plyr)
    
    data("citytemp")
    
    plot <- highchart() %>% 
      hc_add_series(name = "London", data = citytemp$london, type = "area") %>% 
      hc_rm_series(name = "New York")
    
    htmlwidgets::saveWidget(widget = plot, file = "~/plot.html")
    setwd("~")
    webshot::webshot(url = "plot.html", 
                     file = "plot.png")
    
    

    【讨论】:

    • 效果很好!我注意到输出图像只显示了图表的一部分,所以我设置了delay=3,现在一切看起来都很好!
    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多