我试图在内存中捕获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.
Run Code Online (Sandbox Code Playgroud)
您可以使用该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")
Run Code Online (Sandbox Code Playgroud)
figpng
现在是你的绘图的 png 的原始向量。