如何保存通常方法无法保存的图

时间:2019-06-29 12:56:55

标签: r plot graph

这可能是此问题的更一般版本(How to save ggrough chart as .png)。

我正在使用RStudio。通常,我可以按常规方式(png()jpeg()ggsave()保存绘图。

但是,我遇到了在RStudio Viewer中绘制图的情况,但是上述保存命令不起作用。

以下是remiotic页上https://timelyportfolio.github.io/remiotic/处的示例:

library(remiotic)
remiotic(
    # lines expected to be an array of arrays
    #  for now do it the really ugly way
    #  but should be able to fix this fairly easily
    data = list(
        list(
            group = "A",
            coordinates = lapply(0:10, function(x) list(x=x, y=runif(1)))
        )
    ),
    frame = "XYFrame",
    props = list(
        shape = "lines",
        xAccessor = "x",
        yAccessor = "y",
        xExtent = c(0, 10),
        yExtent = c(0, 1),
        lineStyle = list(stroke = "#629"),
        margin = list(
            top = 20,
            right = 40,
            bottom = 50,
            left = 50
        ),
        axes = list(
            list(orient = "left"),
            list(orient = "bottom")
        )
    ),
    width = "100%"
)

我尝试将jpeg("file")png("file")pdf("file")放在此代码之前,并将dev.off()放在此代码之后。前两个不会创建文件,第三个不会创建文件,但是由于它没有将绘图保存到其中,因此我无法打开该文件。我有一台Mac;如果我将x11()放在前面,它将打开新窗口,但会在RStudio Viewer中渲染图。

如何使用代码(而不是RStudio的导出按钮)保存这样的图形?

1 个答案:

答案 0 :(得分:2)

rg <- remiotic(...)
htmlwidgets::saveWidget(rg, "remplot.html")

或者Webshot软件包将允许捕获为png。

library(webshot)
webshot("remplot.html", "remplot.png")