Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve text when saving ggplot2 as .svg?

Tags:

r

ggplot2

svg

knitr

I am trying to save a ggplot2 plot in svg format. I am not using the ggsave function because the plot is generated as part of a knitr document - the device I specify for plotting is 'svg'.

The problem is that text elements from the original plot appear as paths in the svg file, at least when opened in inkscape. The source code of the svg does not look like it contains any text either.

My plotting function is defined in a separate file:

## @knitr plot_histogram
ggplot(mainFrame[complete.cases(mainFrame),]) 
+ geom_boxplot(aes(x=source, y = pPfam, fill = source)) 
+ scale_y_continuous(limits = c(0,1)) 

In the knitr document, I call the function and save the image using the 'svg' device.

```{r plot_histogram, dev = 'svg', fig.width= 7, fig.height=4, fig.show='hold', fig.path="figure/summary"}
```

So I'm not sure how to tell the 'svg' device or ggplot2 that I want to preserve text when saving svg? I would also be happy to use another graphics device if that solves the problem.

Many thanks in advance.

like image 800
Mo Sander Avatar asked Jul 09 '13 18:07

Mo Sander


People also ask

Can R Save As SVG?

Second, in R you can save a plot as Scalable Vector Graphics (SVG) with the svg function. This function also allows you to modify the height, width and point size with the height , width and pointsize arguments, respectively, but also the anti-alias with the antialias argument.

How do I save Ggplot high quality?

You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.

Can you save Ggplot as a object?

ggsave() is a convenient function for saving a plot. It defaults to saving the last plot that you displayed, using the size of the current graphics device.

How do I save a Ggplot as PNG?

The png() method in R can be used to save a ggplot in the png format into the specified directory. The name of the plot along with the path name can be specified as an argument of the method.


1 Answers

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'.

like image 195
Yihui Xie Avatar answered Sep 23 '22 14:09

Yihui Xie