如何保存与GGPLOT2作出SVG的阴谋(How to save a plot made with

2019-07-31 07:19发布

我要救一个堆叠面积图(与代码的关系的例子可以发现这里有GGPLOT2SVG制造)。 与开罗包尝试过,但结果是不好的。

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

Answer 1:

其实很简单,没有要求其他包装比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)

希望对你有效。



文章来源: How to save a plot made with ggplot2 as SVG
标签: r svg ggplot2