How to Save a Plot Made with Ggplot2 as Svg

How to save a plot made with ggplot2 as SVG

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.

how to ggsave() a grid.arranged plot as SVG

you probably can try to use patchwork package

https://patchwork.data-imaginist.com
instead of grid.arrange

Then you need to just use

c3=c1+c2
ggsave("~/Desktop/plotdm.svg")

This worked for me

Best

Save plot as SVG removing stroke on filled symbols

This seems like a bug in svglite:

Missing edges in svg file for some point characters when background color is set

First reported on ggplot2 github:

ggsave missing edges with some shapes in svg format

save multiple graphics in svg

Using a for loop to incorporate svg function to save graph in a svg format.

With your data, it could look like something like that. Of course, you have to associate each iteration of the loop to the correct dataset.

library(ggplot2)

for(i in 1:12)
{
p <- ggplot(data, aes(x=value)) +
geom_histogram()

svg(filename = "graph%03d.svg", width = 7, height = 7)
p
graphics.off()
}

Does it answer your question ?

Saving displayed plots using for loop as SVG

You are trying to save a loop. Try this:

library(corrplot)
data(cars)
res.plot <- cor(cars)
par(mfrow=c(3,2))

for (i in 1:6) {
svg(filename=paste0("Rplot_",Sys.time(),".svg"), width = 8.27,height = 11.69)
corrplot(res.plot)
dev.off()
}

Or all plots in the same panel:

library(corrplot)
res.plot <- cor(cars)
svg(filename=paste0("Rplot_",Sys.time(),".svg"), width = 8.27,height = 11.69)
par(mfrow=c(3,2))
for (i in 1:6) {
corrplot(res.plot)
}
dev.off()

How to preserve text when saving ggplot2 as .svg?

Neither the svg() device in base R nor the CairoSVG() device in the Cairo package supports this. All texts are turned into glyphs like

<symbol overflow="visible" id="glyph0-2">
<path style="stroke:none;" d="M 3.921875 -2.046875 L 3.921875 0.... "/>
</symbol>

I do not know why this has to be the case, and you may want to file a feature request to the r-devel mailing list. This question is not specific to ggplot2/knitr. It comes from the SVG device.

Update

The OP pointed out that the RSvgDevice actually works, and we can specify the device by:

my_svg <- function(file, width, height) {
library(RSvgDevice)
devSVG(file = file, width = width, height = height, bg = "white", fg = "black",
onefile = TRUE, xmlHeader = TRUE)
}

Then in knitr code chunks, use the option dev='my_svg'.

Exporting a graph in svg format, which is a `girafe` object (`ggiraph` package)

You can save the interactive plot containing the svg in a HTML file using htmltools::save_html(oggetto, "out.html")

out.html contains JavaScript e.g. ggiraphjs.min.js required to run the interactivity. Plain SVG files can display tooltips using the title attribute, but the orange point color on hover on both plots simultaneously seems to require JavaScript.



Related Topics



Leave a reply



Submit