如何将ggplot2的绘图保存为SVG

我想用ggplot2作为SVG保存一个堆积区域图(代码示例可以在这里find)。 尝试了开罗套餐,但结果是糟糕的。

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() 

如果你还没有,我build议看看这个教程[pdf] 。 其实很简单,不需要比ggplot2其他软件包。 只要使用正确的ggsave。

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

  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) 

希望对你有效。