[英]Create ggplot2 plot in memory?
I'm trying to capture the result of a ggplot2 graphic creation in memory, to send it to a server. 我试图在内存中捕获ggplot2图形创建的结果,将其发送到服务器。 Has anyone a good idea how to solve that?
谁有个好主意如何解决?
My code looks currently like this: 我的代码看起来像这样:
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.
You can do this with the magick
package. 你可以使用
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
is now a raw vector of a png of your plot. figpng
现在是你的情节的一个原始向量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.