我要救一个堆叠面积图(与代码的关系的例子可以发现,这里与制造)GGPLOT2为SVG.尝试使用开罗套餐,但结果很糟糕.
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()
Run Code Online (Sandbox Code Playgroud)
E10*_*00i 74
实际上非常简单,除了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)
Run Code Online (Sandbox Code Playgroud)
希望对你有效.