How to save a plot made with ggplot2 as SVG

Solution 1:

Saving a plot made with ggplot2 as an SVG is straightforward using the ggsave function.

However, additional package(s) beyond ggplot2 may be required, such as svglite.

Some sample code:

    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)

Hope it works for you.