如何将使用ggplot2制作的绘图保存为SVG [英] How to save a plot made with ggplot2 as SVG

查看:1981
本文介绍了如何将使用ggplot2制作的绘图保存为SVG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存一个堆积区域图(可以找到代码示例,可以找到 ggplot2 制作为 SVG 的。尝试使用开罗包,但结果很糟糕。

I want to save a stacked area plot (Plot example with code can be found here) made with ggplot2 as SVG. Tried it with the Cairo package but the outcome is bad.

library(ggplot2)
library(grid)
library(Cairo)
...

#png(output_file, width=800, height=400)
Cairo(800,400,file=paste(output_file, ".svg", sep=""),type="svg",bg="transparent",pointsize=8, units="px",dpi=400)

gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

dev.off()


推荐答案

我建议看看本教程[pdf] ,如果你还没有。其实非常简单,不需要其他软件包而不是ggplot2。

I recommend to have a look to this tutorial[pdf], if you didn't already. Actually is very simple and no requires other package than ggplot2. Just use correctly ggsave.

下面是一个示例代码,我希望它能帮助你:

Here is a sample code that i hope it will help you:

    require("ggplot2")
#some sample data
    head(diamonds) 
#to see actually what will be plotted and compare 
    qplot(clarity, data=diamonds, fill=cut, geom="bar")
#save the plot in a variable image to be able to export to svg
    image=qplot(clarity, data=diamonds, fill=cut, geom="bar")
#This actually save the plot in a image
    ggsave(file="test.svg", plot=image, width=10, height=8)

希望它适合您。

这篇关于如何将使用ggplot2制作的绘图保存为SVG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆