在内存中创建ggplot2图表?

6
我正在尝试在内存中捕获ggplot2图形创建的结果,以便将其发送到服务器。有没有好的想法来解决这个问题?
我的代码目前看起来像这样:
data(mtcars)
x <- ggplot(mtcars, aes(x=mpg, y=hp)) +
  geom_point(shape=1)
print(x) # RStudio can capture the output, but I'm unable to do it.
ggsave(filename="a.jpg", plot=x) # not really a solution, need it not on disk, but as blob in memory.

3
什么类型的 blob?二进制位图?ggplot 列表?它将被哪个程序使用? - Mike Wise
@MikeWise 消费者是一个接受Python StringIO 结果的程序 - 因此它只是作为“test”读取的PNG内容。 - Christian Sauer
我不确定这是否有效,但也许您可以利用SQLite的BLOB类型——例如在此博客文章中所示。 - nrussell
将图形编码为base64字符串是否有用?然后可以将生成的字符字符串发送到其他地方。 - Peter
1个回答

1
你可以使用 magick 包来实现这个。
library(magick)
data(mtcars)
x <- ggplot(mtcars, aes(x=mpg, y=hp)) +
  geom_point(shape=1)
fig <- image_graph(width = 400, height=400, res=96)
print(x)
dev.off()
figpng <- image_write(fig, path=NULL, format="png")

"

figpng现在是您绘图的png原始向量。

"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接